Spring线程池开发实战

Spring线程池结合非Spring托管Bean


Spring-Config.xml

<bean id="syncLinkingDetectTaskProcess" class="cn.vobile.trackerspt.hatask.commontask.CommonTaskProcess" parent="abstractCommonTaskProcess">
    <property name="taskSize" value="100" />
    <property name="taskName" value="linking_detect_taisan_add_video" />
    <property name="fromTaskStatus" value="created" />
    <property name="toTaskStatus" value="started" />
    <property name="executor" ref="syncLinkingDetectTaskExecutor" />
    <property name="commonTaskHandler" ref="syncLinkingDetectTaskHandler" />
</bean>
    
<bean id="syncLinkingDetectTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="3" />
    <property name="maxPoolSize" value="5" />
    <property name="queueCapacity" value="200" />
</bean>

Java Code

private Executor            executor;

public void setExecutor(Executor executor) {
    this.executor = executor;
}

public void dealProcessDetail(ProcessControl pc) {
    Assert.notNull(taskName, "taskName must not be null");
    Assert.notNull(fromTaskStatus, "fromTaskStatus must not be null");
    Assert.notNull(toTaskStatus, "toTaskStatus must not be null");

    List<WorkingCommonTask> workingTasks = commonTaskService.getWorkingTasks(taskName, fromTaskStatus, taskSize);
    if (CollectionUtils.isEmpty(workingTasks)) {
        logger.info("No " + taskName + " task need to process: taskStatus=" + fromTaskStatus.getCode());
        return;
    }

    for (final WorkingCommonTask workingTask : workingTasks) {
        if (null == workingTask) {
            continue;
        }

        boolean isUpdateSuccess = commonTaskService.updateWorkingTaskStatus(workingTask.getId(), fromTaskStatus, toTaskStatus);
        if (!isUpdateSuccess) {
            logger.info("The " + taskName + " task has been processed: id=" + workingTask.getId() + ", fromTaskStatus=" + fromTaskStatus + ", toTaskStatus=" + toTaskStatus);
            continue;
        }

        try {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        commonTaskHandler.handle(workingTask.getId());
                    } catch (Exception e) {
                        logger.error(taskName + " task handle failed: " + workingTask, e);
                    }
                };
            });
        } catch (Exception e) {
            logger.error("Execute " + taskName + "  failed: " + workingTask, e);
            if (e instanceof TaskRejectedException) {
                break;
            }
        }
    }
}

下面是其他程序猿总结的Spring线程池开发经验,感觉挺不错的,值得学习:

http://blog.csdn.net/thjnemo/article/details/42241173

http://blog.csdn.net/majian_1987/article/details/16967285

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值