2011年12月19日 星期一

Spring - applicationContext.xml , Set ThreadPoolExecutor to use RunTime.getRutime().availableProcessors() determine corePoolSize.

Spring 的記錄, ThreadPoolExecutor的corePoolSize 之前在測fork/join時看到ForkJoinPool裡面針對pool的設定是取決於cpu core size , 用的就是Runtime.getRuntime().availableProcessors(),然後在spring裡面的設定檔還滿麻煩的,查了一下才查到用法

靜態的Class instance如 Runtime r = Runtime.getRuntime();
在spring 裡可以這樣設
<bean id="runTime" class="java.lang.Runtime" factory-method="getRuntime"/>

然後int localMaxThread = r.availableProcessors();
我們可以這樣設定
<bean id="localMaxThread" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetObject">
   <ref bean="runTime"/>
  </property>
  <property name="targetMethod">
   <value>availableProcessors</value>
  </property>
 </bean>


最後完整的xml 設定例子如下

<bean id="limitedQueue" class="com.gfactor.emd.concurrent.LimitedQueue">
  <constructor-arg index="0">
   <value>1</value>
  </constructor-arg>
 </bean>

 <bean id="runTime" class="java.lang.Runtime" factory-method="getRuntime"/>

 <bean id="localMaxThread" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetObject">
   <ref bean="runTime"/>
  </property>
  <property name="targetMethod">
   <value>availableProcessors</value>
  </property>
 </bean>

 <bean id="fetchEmlMessagethreadPoolExecutor" class="java.util.concurrent.ThreadPoolExecutor">
  <constructor-arg type="int" ref="localMaxThread"></constructor-arg>
  <constructor-arg type="int" value="12"></constructor-arg>
  <constructor-arg type="long" value="300"></constructor-arg>
  <constructor-arg type="java.util.concurrent.TimeUnit" value="SECONDS" ></constructor-arg>
  <constructor-arg type="java.util.concurrent.BlockingQueue" ref="limitedQueue"></constructor-arg>
 </bean>

沒有留言:

張貼留言