This topic describes how to configure a time-out period when developing High-Speed Service Framework (HSF) applications.

Background information

A time-out period must be configured for network call requests. The default HSF time-out period is 3,000 ms. You can configure a time-out period for the provider and consumer. If no time-out period is configured for the provider, the time-out period configured for the consumer is used. When configuring a time-out period for the provider, consider the total duration of service execution, serialization, and network communication. We recommend that you configure a default time-out period for each service of the provider. You can configure a time-out period for the consumer based on actual services. For example, you can set the time-out period to a small value for frontend applications that need to return results to users within a short time.

The following table lists the objects and scopes of related interfaces, which are sorted by priority.

Priority Interface Object Scope
0 com.taobao.hsf.util.RequestCtxUtil#setRequestTimeout Consumer Single call
1 HSFApiConsumerBean#setMethodSpecials Consumer Method
2 HSFApiConsumerBean#setClientTimeout Consumer Interface
3 -DdefaultHsfClientTimeout Consumer All interfaces
4 HSFApiProviderBean#setMethodSpecials Provider Method
5 HSFApiProviderBean#setClientTimeout Provider Interface
Note The consumer-specific configuration takes precedence over the provider-specific configuration and methods take precedence over interfaces.

Configure a time-out period for the consumer

  • Configure the time-out period for HSF services through API programming.

    Set the clientTimeout (in ms) property of HSFApiConsumerBean. The following code sets the time-out period to 1,000 ms for interfaces and to 100 ms for the queryOrder method.

    HSFApiConsumerBean consumerBean = new HSFApiConsumerBean();
    // Configure the time-out period at the interface level.
    consumerBean.setClientTimeout(1000);
    //xxx
    MethodSpecial methodSpecial = new MethodSpecial();
    methodSpecial.setMethodName("queryOrder");
    // Configure the time-out period at the method level, which takes precedence over that at the interface level.
    methodSpecial.setClientTimeout(100);
    consumerBean.setMethodSpecials(new MethodSpecial[]{methodSpecial});
  • Configure the time-out period for HSF services through Spring configuration.
    Spring is a framework widely used by applications. You can configure HSF services through Spring XML. The following XML configuration has the same effect as the preceding API programming:
    <bean id="CallHelloWorld" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean">
        ...
        <property name="clientTimeout" value="1000" />
        <property name="methodSpecials">
          <list>
            <bean class="com.taobao.hsf.model.metadata.MethodSpecial">
              <property name="methodName" value="queryOrder" />
              <property name="clientTimeout" value="100" />
            </bean>
          </list>
        </property>
        ...
    </bean>
  • Configure the time-out period for HSF services through annotations.
    Spring Boot is widely used. You can use annotations to configure Spring beans and also configure subscription to HSF services.
    1. Add the starter dependency to the project.
      <dependency>
          <groupId>com.alibaba.boot</groupId>
          <artifactId>pandora-hsf-spring-boot-starter</artifactId>
      </dependency>
    2. Add annotations in the code.

      An HSF consumer is used in multiple places, but you do not have to mark each place where it is used with @HSFConsumer. You only need to write the Config class and annotate other places with @Autowired. The following annotation configuration has the same effect as the preceding API programming:

       @HSFConsumer(clientTimeout = 1000, methodSpecials = @HSFConsumer.ConsumerMethodSpecial(methodName = "queryOrder", clientTimeout = "100"))
       private OderService orderService;
  • Configure a global time-out period for the consumer.
    • Add -DdefaultHsfClientTimeout=100 to startup parameters
    • Add System.setProperty("defaultHsfClientTimeout", “100”) to the code

Configure a time-out period for the provider

  • Configure the time-out period for HSF services through API programming.

    Set the clientTimeout (in ms) property of HSFApiProviderBean. The code is as follows.

    HSFApiProviderBean providerBean = new HSFApiProviderBean();
    // Configure the time-out period at the interface level.
    providerBean.setClientTimeout(1000);
    //xxx
    MethodSpecial methodSpecial = new MethodSpecial();
    methodSpecial.setMethodName("queryOrder");
    // Configure the time-out period at the method level, which takes precedence over that at the interface level.
    methodSpecial.setClientTimeout(100);
    providerBean.setMethodSpecials(new MethodSpecial[]{methodSpecial});
  • Configure the time-out period for HSF services through Spring configuration.

    Spring is a framework widely used by applications. You can configure HSF services through Spring XML. The following XML configuration has the same effect as the preceding API programming:

    <bean class="com.taobao.hsf.app.spring.util.HSFSpringProviderBean" init-method="init">
        ...
        <property name="clientTimeout" value="1000" />
        <property name="methodSpecials">
          <list>
            <bean class="com.taobao.hsf.model.metadata.MethodSpecial">
              <property name="methodName" value="queryOrder" />
              <property name="clientTimeout" value="2000" />
            </bean>
          </list>
        </property>
        ...
    </bean>
  • Configure the time-out period for HSF services through annotations

    The following annotation configuration has the same effect as the preceding API programming:

    @HSFProvider(serviceInterface = OrderService.class, clientTimeout = 3000)
    public class OrderServiceImpl implements OrderService {
        @Autowired
        private OrderDAO orderDAO;
    
        @Override
        public OrderModel queryOrder(Long id) {
            return orderDAO.queryOrder(id);
        }
    }