Among the API operations of HSF applications, the key API operations are related to creating provider beans and consumer beans.

Background information

Four operation classes are provided by use case.
  • com.taobao.hsf.app.api.util.HSFApiProviderBean: creates provider beans by using the API programming method.
  • com.taobao.hsf.app.api.util.HSFApiConsumerBean: creates consumer beans by using the API programming method.
  • com.taobao.hsf.app.spring.util.HSFSpringProviderBean: creates provider beans by using the Spring configuration method.
  • com.taobao.hsf.app.spring.util.HSFSpringConsumerBean: creates consumer beans by using the Spring configuration method.

The configuration property of HSFSpringXxxBean corresponds to the setter method of HSFApiXxxBean. The following content describes the four API operation classes from the aspects of provider beans and consumer beans.

ProviderBean

  • API programming method - HSFApiProviderBean

    You can publish HSF services by configuring and initializing com.taobao.hsf.app.api.util.HSFApiProviderBean.

    The com.taobao.hsf.app.api.util.HSFApiProviderBean configuration and initialization process is implemented only once for the same service. We recommend that you cache the HSFApiProviderBean object because the object is complex.

    • Sample code
      // Instantiate and configure a provider bean.
      HSFApiProviderBean hsfApiProviderBean = new HSFApiProviderBean();
      hsfApiProviderBean.setServiceInterface("com.taobao.hsf.test.HelloWorldService");
      hsfApiProviderBean.setTarget(target); // target indicates the implementation object of the operation that is specified by serviceInterface. hsfApiProviderBean.setServiceVersion("1.0.0");
      hsfApiProviderBean.setServiceGroup("HSF");
      
      // Initialize the provider bean for service publishing. hsfApiProviderBean.init();
    • Configurable property table

      In addition to the properties specified in the preceding sample code, HSFApiProviderBean includes many other configurable properties. You can use the corresponding setter methods to configure these properties.

      Property Type Required Default value Description
      serviceInterface String Yes N/A The operation that is used to provide an HSF service. The consumer subscribes to the service by using this property.
      target Object Yes N/A The service implementation object of the operation that is specified by serviceInterface.
      serviceVersion String No 1.0.0 The version number of the service. The consumer subscribes to the service by using this property.
      serviceGroup String No HSF The group of the service. The consumer subscribes to the service by using this property.
      serviceDesc String No null The description of the service for easy management.
      clientTimeout int No 3000 The response time-out period. Unit: millisecond. If the provider returns no response in the time-out period, HSFTimeOutException is thrown.
      methodSpecials MethodSpecial[] No N/A The response time-out period for some methods in the service. Specify the MethodSpecial.methodName parameter as a method name and specify the MethodSpecial.clientTimout parameter as a time-out period for the current method. This property takes precedence over clientTimeout for the current provider.
      preferSerializeType String No hessian2 The serialization method for the service request parameters and the service response result for HSF2. Valid values: java, hessian, hessian2, json, and kryo.
      corePoolSize STRING whose value is an integer No 0 The separate thread pool for the service, and the minimum number of active threads. By default, the public thread pool of the HSF provider is used if this property is not specified.
      maxPoolSize STRING whose value is an integer No 0 The separate thread pool for the service, and the maximum number of active threads. By default, the public thread pool of the HSF provider is used if this property is not specified.
  • Spring configuration method - HSFSpringProviderBean

    You can publish HSF services by configuring a bean of the class com.taobao.hsf.app.spring.util.HSFSpringProviderBean in the Spring profile.

    Sample code

    <bean id="helloWorldService" class="com.taobao.hsf.test.HelloWorldService" />
    
    <bean class="com.taobao.hsf.app.spring.util.HSFSpringProviderBean" init-method="init">
        <!-- [Required] Specify the operation that is used to externally provide an HSF service. -->
        <property name="serviceInterface" value="com.taobao.hsf.test.HelloWorldService" />
    
        <!-- [Required] Specify the service implementation object of the operation that is specified by serviceInterface. To be specific, the object is the Spring bean ID of the HSF service to be published. -->
        <property name="target" ref="helloWorldService" />
    
        <!-- [Optional] Specify the version number of the service. The default value is 1.0.0. -->
        <property name="serviceVersion" value="1.0.0" />
    
        <!-- [Optional] Specify the group to which the service belongs. The default value is HSF. -->
        <property name="serviceGroup" value="HSF" />
    
        <!-- [Optional] Specify the description of the service for easy management. The default value is null. -->
        <property name="serviceDesc" value="HelloWorldService providered by HSF" />
    
        <!-- [Optional] Specify the response time-out period. Unit: millisecond. If the provider returns no response in the specified period, HSFTimeOutException is thrown. -->
        <!-- The default value is 3000 ms. -->
        <property name="clientTimeout" value="3000"/>
    
        <!-- [Optional] Specify the response time-out period for some methods in the service. This property takes precedence over clientTimeout. -->
        <!-- Specify the MethodSpecial.methodName parameter as a method name and specify the MethodSpecial.clientTimout parameter as a time-out period for the current method. -->
        <property name="methodSpecials">
            <list>
                <bean class="com.taobao.hsf.model.metadata.MethodSpecial">
                    <property name="methodName" value="sum" />
                    <property name="clientTimeout" value="2000" />
                </bean>
            </list>
        </property>
    
        <!-- [Optional] Specify the serialization method for the request parameters and the response result of the service. Valid values: java, hessian, hessian2, json, and kryo. -->
        <!-- The default value is hessian2. -->
        <property name="preferSerializeType" value="hessian2"/>
    
        <!-- [Optional] Configure a separate thread pool for the service and specify the minimum number of active threads. By default, the public thread pool of the HSF provider is used if this property is not specified. -->
        <property name="corePoolSize" value="10"/>
    
        <!-- [Optional] Configure a separate thread pool for the service and specify the maximum number of active threads. By default, the public thread pool of the HSF provider is used if this property is not specified. -->
        <property name="maxPoolSize" value="60"/>
    </bean>

ConsumerBean

  • API programming method - HSFApiConsumerBean

    You can subscribe to HSF services by configuring and initializing com.taobao.hsf.app.api.util.HSFApiConsumerBean.

    The com.taobao.hsf.app.api.util.HSFApiConsumerBean configuration and initialization process is implemented only once for the same service.

    We recommend that you cache the HSFApiConsumerBean object and the retrieved HSF agent because the object size is large.
    Note In the HSF, HSFApiConsumerBean caches service configuration. If a subscribed service is configured for multiple times, only the first configuration takes effect.
    • Sample code
      // Instantiate and configure a consumer bean.
      HSFApiConsumerBean hsfApiConsumerBean = new HSFApiConsumerBean();
      hsfApiConsumerBean.setInterfaceName("com.taobao.hsf.test.HelloWorldService");
      hsfApiConsumerBean.setVersion("1.0.0");
      hsfApiConsumerBean.setGroup("HSF");
      
      // Initialize the consumer bean for service subscription.
      // true indicates waiting for address push (time-out period: 3000 ms). The default value is false and indicates an asynchronous call.
      hsfApiConsumerBean.init(true);
      
      // Retrieve the HSF agent. HelloWorldService helloWorldService = (HelloWorldService) hsfApiConsumerBean.getObject();
      
      // Initiate an HSF call. String helloStr = helloWorldService.sayHello("Li Lei");
    • Configurable property table

      In addition to the properties specified in the preceding sample code, HSFApiConsumerBean includes a number of other configurable properties. You can use the corresponding setter methods to configure these properties.

      Property Type Required Default value Description
      interfaceName String Yes N/A The name of the API for service subscription. The consumer subscribes to the service by using this property.
      version String Yes N/A The version number of the subscribed service. The consumer subscribes to the service by using this property.
      group String Yes N/A The group of the subscribed service. The consumer subscribes to the service by using this property.
      clientTimeout int No N/A The request time-out period. Unit: millisecond. If the consumer receives no response from the provider, HSFTimeOutException is thrown. clientTimeout that is specified for the consumer takes precedence over clientTimeout that is specified for the provider. If clientTimeout is not specified for the consumer, clientTimeout that is specified for the provider is used during a remote service call.
      methodSpecials MethodSpecial[] No N/A The request time-out period for some methods in the service. Specify the MethodSpecial.methodName parameter as a method name and specify the MethodSpecial.clientTimout parameter as a time-out period for the method. This property takes precedence over clientTimeout for the current consumer.
      maxWaitTimeForCsAddress int No N/A The time for the synchronous wait for Config Server to push an address. Unit: millisecond. This property prevents HSFAddressNotFoundException from occurring when the service is called before the address is pushed. We recommend that you set this property to 5000 ms to meet the requirement for wait time.
      asyncallMethods List No null A list of methods to be asynchronously called. Each string in the list is in the following format:
      • name: the name of the method.
      • type: the type of the asynchronous call.
      • listener: the listener.
      In this list, the listener field takes effect only for asynchronous calls of the callback type. Valid values of the type field:
      • future: The Future method is used to retrieve the request execution result.
      • callback: After the remote service call is complete, HSF uses the response to call back the configured listener. This listener must implement HSFResponseCallback.
      proxyStyle String No jdk The agent mode of the service. Generally, this property does not need to be configured. To block the consumer bean, set this property to javassist.
  • Spring configuration method - HSFSpringConsumerBean

    To subscribe to services, you can configure a bean of the class com.taobao.hsf.app.api.util.HSFSpringConsumerBean in the Spring profile.

    Sample code

    <bean id="helloWorldService" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean">
        <!-- [Required] Specify the name of the operation for service subscription. -->
        <property name="interfaceName" value="com.taobao.hsf.test.HelloWorldService" />
    
        <!-- [Required] Specify the version number of the subscribed service. -->
        <property name="version" value="1.0.0" />
    
        <!-- [Required] Specify the group of the subscribed service. -->
        <property name="group" value="HSF" />
    
        <!-- [Optional] Specify the request time-out period in milliseconds. If the consumer receives no response from the provider, HSFTimeOutException is thrown. -->
        <!-- clientTimeout that is specified for the consumer takes precedence over clientTimeout that is specified for the provider. If clientTimeout is not specified for the consumer, clientTimeout that is specified for the provider is used during a remote service call.-->
        <property name="clientTimeout" value="3000" />
    
        <!-- [Optional] Specify the request time-out period for some methods in the service. This property takes precedence over clientTimeout for the consumer. -->
        <!-- Specify the MethodSpecial.methodName parameter as a method name and specify the MethodSpecial.clientTimout parameter as a time-out period for the current method. -->
        <property name="methodSpecials">
            <list>
                <bean class="com.taobao.hsf.model.metadata.MethodSpecial">
                    <property name="methodName" value="sum" />
                    <property name="clientTimeout" value="2000" />
                </bean>
            </list>
        </property>
    
        <!-- [Optional] Specify the time (unit: millisecond) to synchronously wait for Config Server to push an address. -->
        <!-- This prevents HSFAddressNotFoundException from occurring when the service is called before the address is pushed. -->
        <!-- We recommend that you set this property to 5000 ms to meet the requirement for wait time. -->
        <property name="maxWaitTimeForCsAddress"  value="5000"/>
    
        <!-- [Optional] Specify a list of methods to be asynchronously called. Each string in the list is in the following format: -->
        <!-- name: the name of the method. -->
        <!-- type: the type of the asynchronous call. -->
        <!-- listener: the listener. -->
        <!-- The listener field takes effect only for asynchronous calls of the callback type.-->
        <!-- Valid values of the type field: -->
        <!-- future: The Future method is used to retrieve the request execution result. -->
        <!-- callback: HSF uses the response to call back the configured listener. The listener must implement HSFResponseCallback. -->
        <property name="asyncallMethods">
            <list>
                <value>name:sayHello;type:callback;listener:com.taobao.hsf.test.service.HelloWorldServiceCallbackHandler</value>
            </list>
        </property>
    
        <!-- [Optional] Specify the proxy mode of the service. Generally, this property does not need to be configured. To block the consumer bean, set this property to javassist. -->
        <property name="proxyStyle" value="jdk" />
    </bean>