All Products
Search
Document Center

Enterprise Distributed Application Service:Configure a time-out period

Last Updated:Dec 18, 2023

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

Prerequisites

Background information

A time-out period must be configured for requests that are initiated for network calls. The default HSF time-out period is 3,000 ms. You can configure a time-out period for the provider and the consumer. By default, the time-out period that configured for the consumer takes precedence. If no time-out period is configured for the consumer, the time-out period that is configured for the provider is used. When you configure a time-out period for the provider, consider the total duration of service execution, serialization, and network communications. We recommend that you configure a default time-out period for each service on the provider. You can configure a time-out period for the consumer based on your business scenarios. For example, you can set the time-out period to a small value for frontend applications that need to return results to users at the earliest opportunity.

The following table describes the objects and scopes of related API operations. The operations are sorted by priority in descending order.

Priority

API

Object

Scope

0

com.taobao.hsf.util.RequestCtxUtil#setRequestTimeout

Consumer

Single calls

1

HSFApiConsumerBean#setMethodSpecials

Consumer

Methods

2

HSFApiConsumerBean#setClientTimeout

Consumer

API operations

3

defaultHsfClientTimeout

Consumer

All API operations

4

HSFApiProviderBean#setMethodSpecials

Provider

Methods

5

HSFApiProviderBean#setClientTimeout

Provider

API operations

Note

Consumer-specific configurations take precedence over provider-specific configurations, and method-specific configurations take precedence over API operation-specific configurations.

Configure a time-out period for the consumer

  • Use APIs to configure HSF services.

    Set the clientTimeout property of HSFApiConsumerBean. Unit: ms. The following code sets the time-out period to 1,000 ms for the API operation and 100 ms for the queryOrder method:

    HSFApiConsumerBean consumerBean = new HSFApiConsumerBean();
    // Configure the time-out period at the API operation level. 
    consumerBean.setClientTimeout(1000);
    MethodSpecial methodSpecial = new MethodSpecial();
    methodSpecial.setMethodName("queryOrder");
    // Configure the time-out period at the method level. This setting takes precedence over the time-out setting that is configured at the API operation level. 
    methodSpecial.setClientTimeout(100);
    consumerBean.setMethodSpecials(new MethodSpecial[]{methodSpecial});
  • Use Spring to configure HSF services.

    Spring Framework is a component that is widely used in applications. If you do not want to configure HSF services by using APIs, you can configure them by using Spring XML. The following XML configuration has the same effect as the preceding API configuration:

    <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>
  • Use annotations to configure HSF services.

    Spring Boot is widely used. You can use annotations to configure Spring beans, configure HSF services, and subscribe 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.

      In most cases, an HSF consumer is used in multiple places, but you do not need to use @HSFConsumer to mark every place where the HSF consumer is used. You need only to write a unified Config class and use @Autowired to directly inject the class when the HSF consumer needs to be used. The following annotation configuration has the same effect as the preceding API configuration:

       @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 the startup parameters.

    • Add System.setProperty("defaultHsfClientTimeout", "100") to the code.

Configure a time-out period for the provider

  • Use APIs to configure HSF services.

    Set the clientTimeout property of HSFApiProviderBean. Unit: ms. The following code provides an example on how to configure the property:

    HSFApiProviderBean providerBean = new HSFApiProviderBean();
    // Configure the time-out period at the API operation level. providerBean.setClientTimeout(1000);
    //xxx
    MethodSpecial methodSpecial = new MethodSpecial();
    methodSpecial.setMethodName("queryOrder");
    // Configure the time-out period at the method level. This setting takes precedence over the time-out setting that is configured at the API operation level. methodSpecial.setClientTimeout(100);
    providerBean.setMethodSpecials(new MethodSpecial[]{methodSpecial});
  • Use Spring to configure HSF services.

    Spring Framework is a component that is widely used in applications. If you do not want to configure HSF services by using APIs, you can configure them by using Spring XML. The following XML configuration has the same effect as the preceding API configuration:

    <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>
  • Use annotations to configure HSF services.

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

    @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);
        }
    }