本文介绍springBatch 多线程两种配置方式:
本文原创,转载请标明出处,谢谢
1、第二种方式如下配置方式,reader、processor、writer都是多线程处理,处理线程数为10.但是今天我测试发现设置的线程数为n,通常只有n-1个线程在跑,
猜测少的那个线程可能是作为备用线程或者是任务调度线程。
<batch:job id="Job">
<batch:step id="Step">
<batch:tasklet transaction-manager="transactionManager" task-executor="taskExecutor">
<batch:chunk reader="itemReader" writer="itemWriter" processor="itemProcessor"
commit-interval="1" />
<batch:listeners>
<batch:listener ref="customStepListener" />
<batch:listener ref="customItemReaderListener" />
<batch:listener ref="customItemWriterListener" />
</batch:listeners>
</batch:tasklet>
</batch:step>
</batch:job><bean id="taskExecutor"2、第二种方式如下:可以指定read 或者 writer processor 为多线程处理
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="30" />
</bean><batch:job id="Job"> <batch:step id="Step"> <batch:tasklet transaction-manager="transactionManager" > <batch:chunk reader="itemReader" writer="itemWriter" processor="itemProcessor" commit-interval="1" /> <batch:listeners> <batch:listener ref="customStepListener" /> <batch:listener ref="customItemReaderListener" /> <batch:listener ref="customItemWriterListener" /> </batch:listeners> </batch:tasklet> </batch:step></batch:job><bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="10" /> <property name="maxPoolSize" value="30" /></bean>java:public class ItemProcessor implements ItemProcessor<String, String> { private TaskExecutor taskExecutor; public void setTaskExecutor(TaskExecutor taskExecutor) { this.taskExecutor = taskExecutor; } public String process(final String item) throws Exception { taskExecutor.execute(new Runnable() { public void run() { try { Thread current = Thread.currentThread(); System.out.println("@process: "+" "+current.getId()); System.out.println("@getName: "+current.getName()); System.out.println("@activeCount: "+current.activeCount()); System.out.println("@getId: "+current.getId()); System.out.println("@toString: "+current.toString()); } catch (Exception e) { System.out.println(e); } } }); return item; }}