All Products
Search
Document Center

Timeout Configuration

Last Updated: Aug 17, 2020

Under the BOLT communication protocol, the timeout period in SOFARPC is 3s by default. You can set the timeout period in the service or method configuration when referencing a service. The timeout period is set in milliseconds (ms).

Service-level configuration

To set the timeout period in the service configuration when publishing a service, you can set the timeout parameter to the corresponding value. There are 3 configuration ways as follows:

  • XML: When you reference a service through the XML mode, set the timeout attribute of <sofa:global-attrs> in the <sofa:binding.bolt> tag.
    1. <sofa:reference interface="com.example.demo.SampleService" id="sampleService">
    2. <sofa:binding.bolt>
    3. <sofa:global-attrs timeout="2000"/>
    4. </sofa:binding.bolt>
    5. </sofa:reference>
  • Annotation: When you reference a service through the Annotation mode, set the timeout attribute of @SofaReferenceBinding.
    1. @SofaReference(binding = @SofaReferenceBinding(bindingType = "bolt", timeout = 2000))
    2. private SampleService sampleService;
  • API:
    • configure as a Spring project: When you reference a service in a Spring or Spring Boot environment, set the timeout attribute of BoltBindingParam.
      1. BoltBindingParam boltBindingParam = new BoltBindingParam();
      2. boltBindingParam.setTimeout(2000)
    • configure without a Spring framework: When you reference a service by using a raw API of SOFARPC in a non-Spring environment, set the timeout attribute of ConsumerConfig.
      1. ConsumerConfig<SampleService> consumerConfig = new ConsumerConfig<SampleService>()
      2. .setInterfaceId(SampleService.class.getName())
      3. .setRegistry(registryConfig)
      4. .setProtocol("bolt")
      5. .setTimeout(2000);

Method-level configuration

The timeout configuration in method level has a higher priority than that at service level.The absence of configuration at method level will make the configuration at service level effective. There are 3 configuration ways as follows:

  • XML: When you reference a service through the XML mode, set the timeout attribute of the <sofa:method> tag.

    1. <sofa:reference interface="com.example.demo.SampleService" id="sampleService">
    2. <sofa:binding.bolt>
    3. <sofa:method name="hello" timeout="2000"/>
    4. </sofa:binding.bolt>
    5. </sofa:reference>
  • Annotation: Currently, you cannot set the timeout period at the method level in the Annotation mode.

  • API:

    • configure as a Spring project: When you reference a service in a Spring or Spring Boot environment, set the timeout attribute of RpcBindingMethodInfo.

      1. BoltBindingParam boltBindingParam = new BoltBindingParam();
      2. RpcBindingMethodInfo rpcBindingMethodInfo = new RpcBindingMethodInfo();
      3. rpcBindingMethodInfo.setName("hello");
      4. rpcBindingMethodInfo.setTimeout(2000);
      5. List<RpcBindingMethodInfo> rpcBindingMethodInfos = new ArrayList<>();
      6. rpcBindingMethodInfos.add(rpcBindingMethodInfo);
      7. boltBindingParam.setMethodInfos(rpcBindingMethodInfos);
    • configure without a Spring framework: When you reference a service by using a raw API of SOFARPC in a non-Spring environment, set the timeout attribute of MethodConfig.

      1. MethodConfig methodConfig = new MethodConfig();
      2. methodConfig.setName("hello");
      3. methodConfig.setTimeout(2000);
      4. List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
      5. methodConfigs.add(methodConfig);
      6. ConsumerConfig<SampleService> consumerConfig = new ConsumerConfig<SampleService>()
      7. .setInterfaceId(SampleService.class.getName())
      8. .setRegistry(registryConfig)
      9. .setProtocol("bolt")
      10. .setMethods(methodConfigs);