Spring 定时器结合线程池

需求:Spring 定时器结合线程池处理工单

    a.定时扫库查出一定数量的需要处理的工单

    b.开启线程处理查出的工单

1,创建处理工单的task

@Component("AppWorkOrderTask")
@Scope("prototype")
public class AppWorkOrderTask implements Runnable {
    public static final String BEAN_NAME = "AppWorkOrderTask";


    @Resource
    private IAppWorkOrderService appWorkOrderService;

    private String appWorkOrderId;

    public void execute() {
      //处理工单逻辑代码
    }

    public String getAppWorkOrderId() {
        return appWorkOrderId;
    }

    public void setAppWorkOrderId(String appWorkOrderId) {
        this.appWorkOrderId = appWorkOrderId;
    }

    @Override
    public void run() {
        execute();
    }

}

 

2,创建扫库的定时器


public class AppWorkOrderScheduler extends TimerTask { private Logger logger = Logger.getLogger(AppWorkOrderScheduler.class); @Resource private IAppWorkOrderService appWorkOrderService; @Resource(name = "appWorkOrderTaskExecutor") private ThreadPoolTaskExecutor taskExecutor; @Override public void run() { //判断线程池容量 int size = taskExecutor.getMaxPoolSize() - taskExecutor.getActiveCount(); if (size > 0) {
        //找出工单 List
<String> appWorkOrderIds = appWorkOrderService.getAppWorkOrderList(20); logger.info("Find " + appWorkOrderIds.size() + " task(s) to execute..."); for (String appWorkOrderId : appWorkOrderIds) { AppWorkOrderTask task = newTask(appWorkOrderId); taskExecutor.execute(task); } } } private AppWorkOrderTask newTask(String appWorkOrderId) { AppWorkOrderTask task = (AppWorkOrderTask) BeanFactory.getInstance().getBeanByName(AppWorkOrderTask.BEAN_NAME);
      //从applicationContext 中获取 appWorkOrderTask 公共类就不再贴了 task.setAppWorkOrderId(appWorkOrderId);
return task; } }

3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
    default-autowire="byName" default-lazy-init="true">

    <!--应用工单异步线程执行器-->
    <bean id="appWorkOrderTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 线程池维护线程的最少数量 -->
        <property name="corePoolSize" value="10"/>
        <!-- 线程池维护线程的最大数量 -->
        <property name="maxPoolSize" value="20"/>
        <!-- 线程池所使用的缓冲队列 -->
        <property name="queueCapacity" value="20" />
    </bean>
    
    <bean class="org.springframework.scheduling.timer.TimerFactoryBean" lazy-init="false">
        <property name="scheduledTimerTasks">
            <list>
                <ref bean="appWorkOrderScheduledTimerTask" />
            </list>
        </property>
    </bean>
    
    <!-- Application WorkOrder Scheduler Timer -->
    <bean id="appWorkOrderSchedulerTask" class="com.test.timer.scheduler.AppWorkOrderScheduler" />
    <bean id="appWorkOrderScheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
         <!-- 运行过程中延时10000ms(10s) -->  
        <property name="delay" value="10000" />
         <!--  每隔90s运行一次 -->  
        <property name="period" value="90000" />
        <property name="timerTask" ref="appWorkOrderSchedulerTask" />
    </bean>

</beans>

 

posted @ 2014-10-25 11:47  china2k  阅读(1108)  评论(0编辑  收藏  举报