Custom thread pools

更新时间:
复制 MD 格式

SOFARPC supports custom business thread pools. You can assign a dedicated thread pool to a specific service to isolate it from the main SOFARPC business thread pool. Multiple services can also share a single custom thread pool.

Note

SOFARPC requires the custom thread pool class to be com.alipay.sofa.rpc.server.UserThreadPool.

XML method

If you publish a service using XML, first define a bean for the thread pool with the class com.alipay.sofa.rpc.server.UserThreadPool. Then, assign this bean to the thread-pool-ref property of the <sofa:global-attrs> tag.

<bean id="helloService" class="com.alipay.sofa.rpc.quickstart.HelloService"/>

<!-- Define a custom thread pool -->
<bean id="customExecutor" class="com.alipay.sofa.rpc.server.UserThreadPool" init-method="init">
    <property name="corePoolSize" value="10"/>
    <property name="maximumPoolSize" value="10"/>
    <property name="queueSize" value="0"/>
</bean>

<sofa:service ref="helloService" interface="XXXService">
    <sofa:binding.bolt>
       <!-- Assign the thread pool to a Service -->
       <sofa:global-attrs thread-pool-ref="customExecutor"/>
    </sofa:binding.bolt>
</sofa:service>

Annotation method

If you publish a service using annotations, specify the custom thread pool bean in the userThreadPool property of the @SofaServiceBinding annotation.

@SofaService(bindings ={@SofaServiceBinding(bindingType ="bolt", userThreadPool ="customThreadPool")})
public class SampleServiceImpl implements SampleService{
}

API method in a Spring environment

If you publish a service using the API in a Spring environment, set the custom thread pool by calling the setUserThreadPool method of BoltBindingParam.

BoltBindingParam boltBindingParam =new BoltBindingParam();
boltBindingParam.setUserThreadPool(new UserThreadPool());

API method in a non-Spring environment

If you use the API in a non-Spring environment, set the custom thread pool as follows.

UserThreadPool threadPool =new UserThreadPool();
threadPool.setCorePoolSize(10);
threadPool.setMaximumPoolSize(100);
threadPool.setKeepAliveTime(200);
threadPool.setPrestartAllCoreThreads(false);
threadPool.setAllowCoreThreadTimeOut(false);
threadPool.setQueueSize(200);

UserThreadPoolManager.registerUserThread(ConfigUniqueNameGenerator.getUniqueName(providerConfig), threadPool);