All Products
Search
Document Center

Custom Thread Pool

Last Updated: Aug 17, 2020

SOFARPC supports custom business thread pools. You can set an individual business thread pool, which is isolated from the business thread pool of SOFARPC, for a specified service. Multiple services can share an independent thread pool.

In SOFARPC, the type of a custom thread pool must be com.alipay.sofa.rpc.server.UserThreadPool.

Publish a service

There are 3 configuration ways as follows:

  • XML: When publishing a service through the XML mode, you can set a Bean for a thread pool whose class is com.alipay.sofa.rpc.server.UserThreadPool, and then configure it for the thread-pool-ref attribute of the <sofa:global-attrs> tag.

    1. <bean id="helloService" class="com.alipay.sofa.rpc.quickstart.HelloService"/>
    2. <! -- Customize a thread pool -->
    3. <bean id="customExecutor" class="com.alipay.sofa.rpc.server.UserThreadPool" init-method="init">
    4. <property name="corePoolSize" value="10" />
    5. <property name="maximumPoolSize" value="10" />
    6. <property name="queueSize" value="0" />
    7. </bean>
    8. <sofa:service ref="helloService" interface="XXXService">
    9. <sofa:binding.bolt>
    10. <! -- Set the thread pool for a service -->
    11. <sofa:global-attrs thread-pool-ref="customExecutor"/>
    12. </sofa:binding.bolt>
    13. </sofa:service>

    Note: a custom thread pool is set up for the HelloService.

  • Annotation: When publishing a service through the Annotation mode, you can set a Bean of a custom thread pool by setting the userThreadPool attribute of @SofaServiceBinding.

    1. @SofaService(bindings = {@SofaServiceBinding(bindingType = "bolt", userThreadPool = "customThreadPool")})
    2. public class SampleServiceImpl implements SampleService {
    3. }
  • API:

    • configure with a Spring framework: When publishing a service by using an API in a Spring environment, you can call the setUserThreadPool method of BoltBindingParam to set a custom thread pool.

      1. BoltBindingParam boltBindingParam = new BoltBindingParam();
      2. boltBindingParam.setUserThreadPool(new UserThreadPool());
    • configure without a Spring framework: When publishing a service by using an API in a non-Spring environment, you can set a custom thread pool in the following way:

      1. UserThreadPool threadPool = new UserThreadPool();
      2. threadPool.setCorePoolSize(10);
      3. threadPool.setMaximumPoolSize(100);
      4. threadPool.setKeepAliveTime(200);
      5. threadPool.setPrestartAllCoreThreads(false);
      6. threadPool.setAllowCoreThreadTimeOut(false);
      7. threadPool.setQueueSize(200);
      8. UserThreadPoolManager.registerUserThread(ConfigUniqueNameGenerator.getUniqueName(providerConfig), threadPool);