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 thethread-pool-ref
attribute of the<sofa:global-attrs>
tag.<bean id="helloService" class="com.alipay.sofa.rpc.quickstart.HelloService"/>
<! -- Customize a 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>
<! -- Set the thread pool for a service -->
<sofa:global-attrs thread-pool-ref="customExecutor"/>
</sofa:binding.bolt>
</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
.@SofaService(bindings = {@SofaServiceBinding(bindingType = "bolt", userThreadPool = "customThreadPool")})
public class SampleServiceImpl implements SampleService {
}
API:
configure with a Spring framework: When publishing a service by using an API in a Spring environment, you can call the
setUserThreadPool
method ofBoltBindingParam
to set a custom thread pool.BoltBindingParam boltBindingParam = new BoltBindingParam();
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:
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);