之前看了程序员杂志上关于spring 定时任务实现异步任务的文章,自己架了一套帮助实现一些费时的操作。
在实现中发现几个问题
1、定时任务中时间设置是这样的
<property name="delay" value="1000" />
<!-- 每次任务间隔 5秒-->
<property name="period" value="5000" />
在某些配置下某任务开始后还没执行完过了5秒,第二个任务又起来了。
这与我的设计冲突。我希望任务是执行完后等5秒再进行第二个任务。
最后发现这个类可以
<bean id="springScheduleExecutorTask"
class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<!-- 配置主任务 -->
<property name="runnable" ref="mainExecutor" />
<!-- 程序启动后延迟1秒执行第一次任务 -->
<property name="delay" value="1000" />
<!-- 每次任务间隔 5秒-->
<property name="period" value="5000" />
</bean>
2、在主任务中我把小任务分放给线程池操作,必须要等线程池完成工作后才能结束主任务run
如果起动线程池主任务run完毕,第二个任务5秒之后就运行了。我实现的代码是
//等待线程执行完毕
while(threadPool.getActiveCount() > 0){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
其实也可以采用观察者模式让线程池中任务发消息给主任务,主任务等待,
这个我之前有发给文章。 http://guoba6688-sina-com.iteye.com/blog/719972
附上所有代码
package com.my.task;
import java.util.TimerTask;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
*
* 主任务起动
*
* @author 锅巴
* @version 1.0 2010-7-29
*/
public class MainExecutor extends TimerTask {
private ThreadPoolTaskExecutor threadPool;
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("MainExecutor is start");
try{
Thread.sleep(5000);
}catch(Exception e){
e.printStackTrace();
}
threadPool.execute(new MyTask(10));
threadPool.execute(new MyTask(10));
threadPool.execute(new MyTask(10));
//等待线程执行完毕
while(threadPool.getActiveCount() > 0){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("MainExecutor is end");
}
public ThreadPoolTaskExecutor getThreadPool() {
return threadPool;
}
public void setThreadPool(ThreadPoolTaskExecutor threadPool) {
this.threadPool = threadPool;
}
public static void main(String[] args) {
new ClassPathXmlApplicationContext(new String[]{"task.context.xml"});
}
}
package com.my.task;
public class MyTask implements Runnable{
private int count;
public MyTask(int count){
this.count = count;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<count; i++){
System.out.println(Thread.currentThread().getName() + " : " + i);
}
System.out.println(Thread.currentThread().getName() + " end ");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="springScheduledExecutorFactoryBean"
class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">
<property name="scheduledExecutorTasks">
<list>
<ref bean="springScheduleExecutorTask" />
</list>
</property>
</bean>
<bean id="springScheduleExecutorTask"
class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<property name="runnable" ref="mainExecutor" />
<property name="delay" value="1000" />
<!-- 每次任务间隔 5秒-->
<property name="period" value="5000" />
</bean>
<!-- 主任务 负责扫描任务 将任务分配给线程完成 -->
<bean id="mainExecutor"
class="com.my.task.MainExecutor">
<property name="threadPool" ref="threadPool" />
</bean>
<!-- 异步线程池 -->
<bean id="threadPool"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="10" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="50" />
<!-- 队列最大长度 >=mainExecutor.maxSize -->
<property name="queueCapacity" value="1000" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="300" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
</property>
</bean>
</beans>
分享到:
相关推荐
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
Spring3.2.6定时任务+线程池,实现系统后台任务多线程处理
spring+springMVC+mybatis+quartz动态定时任务
shiro+spring+data+session+redis实现单点登录,这是一个不错的案例,学习和参考都是很不错的
基于spring boot + maven + opencv 实现的图像深度学习Demo项目,包含车牌识别、人脸识别、证件识别等功能,贯穿样本处理、模型训练、图像处理、对象检测、对象识别等技术点。 基于spring boot + maven + opencv ...
Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...
基于spring+spring mvc+mybatis框架整合实现超市货物管理系统(实现登入功能、mysql数据库的增删改查及分页显示)
SpringBoot+SpringSecurity+JWT+MybatisPlus实现基于注解的权限验证,可根据注解的格式不同,做到角色权限控制,角色加资源权限控制等,粒度比较细化。 @PreAuthorize("hasAnyRole('ADMIN','USER')"):具有admin或...
在spring+hibernate的框架中定时操作数据库,主要是拿到sessionFactory,不会出现no session 和transaction no-bound等问题,由sessionFactory完成对数据的操作,有些包是没有用的,有兴趣的可以自己删除掉
Spring+Spring MVC+Spring JDBC+MySql实现简单登录注册
Spring定时任务的几种实现,欢迎交流!
SpringMVC+Spring+Hibernate+Oracle 实现图书管理(CRUD) 一个简单的图书管理系统。
Spring3.x企业应用开发实战 中 Spring 定时任务源码(spring 三种定时任务的实现方式),Spring quartz java 定时器 定时任务 源码
源码实现Spring与Jersey的整合,通过Maven构建项目,通过与HTML前端结合,实现CRUD操作。
Spring+SpringMVC+Mybatis+Maven+bootstrap+ajax+jQuery整合开发简单的员工后台管理系统,免责声明,朋友分享,切勿商用
SpringMVC+Spring+Mybatis+Oracle 实现HR管理(CRUD)
Spring Boot+Maven+Spring Data JPA+apache Shiro+Easyui实现通用用户权限管理系统
本套源代码实现了Spring 和 mybatis 和 layui 集成情况下的基础操作,包括: 增删改查、分页、导入导出等功能。数据库为mysql,脚本在源代码根目录下,mybatis_crud.sql
springmvc+spring+mysql+maven定时任务
这是通过ssm+layui 实现的简单的对数据表格的增删改查,所有页面纯由layui实现。如果你是新手学习layui,那这个绝对会对你有很大的帮助; 如果你之前已经学过甚至已经使用过,那这个可以当做一个参考