quartz定时器在springmvc中的用法
2016-06-06 11:03
826 查看
java定时器,spring定时器,quartz定时器的区别与用法,推荐
http://blog.csdn.net/etttttss/article/details/7461371
下面主要介绍quartz定时器在springmvc中的用法:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-lazy-init="true">
<description>Quartz的本地Cron式执行任务配置</description>
<!-- Quartz本地Schduler:调度工厂 如果将lazy-init='false'那么容器启动就会执行调度程序
-->
<bean id="localQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<!-- Triggers集成 :配置定时任务列表-->
<property name="triggers">
<list>
<ref bean="CronTriggerBeanTest" />
</list>
</property>
<!-- Quartz配置 -->
<property name="quartzProperties">
<props>
<!-- Scheduler中线程池的大小:框架会自动初始化指定数量个workthread -->
<prop key="org.quartz.threadPool.threadCount">50</prop>
</props>
</property>
<!-- 启动时延期3秒开始任务 -->
<property name="startupDelay" value="3" />
</bean>
<!-- 调度触发器 -->
<bean id="CronTriggerBeanTest"class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SpringQtzJobMethodTest" />
<!-- 指定定时任务执行的时间cronExpression表达式的具体介绍见下篇文章-->
<property name="cronExpression" value="0 0 23 ? * *" />
</bean>
<bean id="SpringQtzJobMethodTest"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="SpringQtzJobTest" />
<!-- 要执行的方法名称 -->
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>
<!-- 定义Bean和Bean中的方法 -->
<bean id="SpringQtzJobTest"class="com.soft.framework.jobs.QuartzOrderSync" />
* 被Spring的Quartz MethodInvokingJobDetailFactoryBean定时执行的普通Spring Bean.
*/
public class QuartzOrderSync{
private static Logger logger = Logger.getLogger(QuartzOrderSyn.class);
@Resource
private OrdersyncService ordersyncService ;
@Resource
DbConfigMapper dbConfigMapper;
public void execute(){
logger.info("订单同步开始");
//从基础数据库中查询所有需要处理的数据库连接(上篇文章介绍了多数据源的使用)
try {
CustomerContextHolder.setContextType("portal1");
List<String> list = dbConfigMapper.selectAll();
//CountDownLatch 下文有介绍
CountDownLatch latch=new CountDownLatch(list.size());
//ExecutorService 下文有介绍 :创建一个可以容纳N个线程任务的线程池
final ExecutorService exec = Executors.newFixedThreadPool(list.size());
for(String idCompany : list){
if(StringUtils.isNotBlank(idCompany)){
//此处传递latch是为了计数
OrderThread accessThread = new OrderThread(idCompany,latch);
exec.submit(accessThread);
}else{
latch.countDown();
}
}
latch.await();//等待所有 完成工作,避免一个定时任务执行到一半,另一个定时任务又触发的情况
exec.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("订单同步结束");
}
class OrderThread implements Runnable{
protected final Logger logger = Logger.getLogger(getClass());
CountDownLatch latch;
private OrdersyncService ordersyncService = SpringContextUtil.getBean(OrdersyncService.class, "ordersyncService");
private String idCompany;
public OrderThread(){}
public OrderThread(String idCompany,CountDownLatch latch){
this.idCompany = idCompany;
this.latch=latch;
}
@Override
public void run() {
try {
//处理业务逻辑
ordersyncService.handleOrder(idCompany);
} catch (Exception e) {
e.printStackTrace();
} finally{
latch.countDown();
}
}
}
}
CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量。每当一个线程完成了自己的任务后,计数器的值就会减1。当计数器值到达0时,它表示所有的线程已经完成了任务,然后在闭锁上等待的线程就可以恢复执行任务。CountDownLatch通过构造函数传入一个初始计数值,调用者可以通过调用CounDownLatch对象的cutDown()方法,来使计数减1;如果调用对象上的await()方法,那么调用者就会一直阻塞在这里,直到别人通过cutDown方法,将计数减到0,才可以继续执行。
具体介绍参见:http://blog.csdn.net/suifeng3051/article/details/49443835
http://blog.csdn.net/etttttss/article/details/7461371
下面主要介绍quartz定时器在springmvc中的用法:
1、搭好springmvc环境,将quartz包引入,本次用的quartz-1.6.1.jar
2.定时任务配置文件applicationContext-quartz.xml
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-lazy-init="true">
<description>Quartz的本地Cron式执行任务配置</description>
<!-- Quartz本地Schduler:调度工厂 如果将lazy-init='false'那么容器启动就会执行调度程序
-->
<bean id="localQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<!-- Triggers集成 :配置定时任务列表-->
<property name="triggers">
<list>
<ref bean="CronTriggerBeanTest" />
</list>
</property>
<!-- Quartz配置 -->
<property name="quartzProperties">
<props>
<!-- Scheduler中线程池的大小:框架会自动初始化指定数量个workthread -->
<prop key="org.quartz.threadPool.threadCount">50</prop>
</props>
</property>
<!-- 启动时延期3秒开始任务 -->
<property name="startupDelay" value="3" />
</bean>
<!-- 调度触发器 -->
<bean id="CronTriggerBeanTest"class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SpringQtzJobMethodTest" />
<!-- 指定定时任务执行的时间cronExpression表达式的具体介绍见下篇文章-->
<property name="cronExpression" value="0 0 23 ? * *" />
</bean>
<bean id="SpringQtzJobMethodTest"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="SpringQtzJobTest" />
<!-- 要执行的方法名称 -->
<property name="targetMethod" value="execute" />
<property name="concurrent" value="false" />
</bean>
<!-- 定义Bean和Bean中的方法 -->
<bean id="SpringQtzJobTest"class="com.soft.framework.jobs.QuartzOrderSync" />
3.定时任务执行类
/*** 被Spring的Quartz MethodInvokingJobDetailFactoryBean定时执行的普通Spring Bean.
*/
public class QuartzOrderSync{
private static Logger logger = Logger.getLogger(QuartzOrderSyn.class);
@Resource
private OrdersyncService ordersyncService ;
@Resource
DbConfigMapper dbConfigMapper;
public void execute(){
logger.info("订单同步开始");
//从基础数据库中查询所有需要处理的数据库连接(上篇文章介绍了多数据源的使用)
try {
CustomerContextHolder.setContextType("portal1");
List<String> list = dbConfigMapper.selectAll();
//CountDownLatch 下文有介绍
CountDownLatch latch=new CountDownLatch(list.size());
//ExecutorService 下文有介绍 :创建一个可以容纳N个线程任务的线程池
final ExecutorService exec = Executors.newFixedThreadPool(list.size());
for(String idCompany : list){
if(StringUtils.isNotBlank(idCompany)){
//此处传递latch是为了计数
OrderThread accessThread = new OrderThread(idCompany,latch);
exec.submit(accessThread);
}else{
latch.countDown();
}
}
latch.await();//等待所有 完成工作,避免一个定时任务执行到一半,另一个定时任务又触发的情况
exec.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("订单同步结束");
}
class OrderThread implements Runnable{
protected final Logger logger = Logger.getLogger(getClass());
CountDownLatch latch;
private OrdersyncService ordersyncService = SpringContextUtil.getBean(OrdersyncService.class, "ordersyncService");
private String idCompany;
public OrderThread(){}
public OrderThread(String idCompany,CountDownLatch latch){
this.idCompany = idCompany;
this.latch=latch;
}
@Override
public void run() {
try {
//处理业务逻辑
ordersyncService.handleOrder(idCompany);
} catch (Exception e) {
e.printStackTrace();
} finally{
latch.countDown();
}
}
}
}
4.CountDownLatch的简介
CountDownLatch是在java1.5被引入的。CountDownLatch这个类能够使一个线程等待其他线程完成各自的工作后再执行。例如,应用程序的主线程希望在负责启动框架服务的线程已经启动所有的框架服务之后再执行。CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量。每当一个线程完成了自己的任务后,计数器的值就会减1。当计数器值到达0时,它表示所有的线程已经完成了任务,然后在闭锁上等待的线程就可以恢复执行任务。CountDownLatch通过构造函数传入一个初始计数值,调用者可以通过调用CounDownLatch对象的cutDown()方法,来使计数减1;如果调用对象上的await()方法,那么调用者就会一直阻塞在这里,直到别人通过cutDown方法,将计数减到0,才可以继续执行。
5.ExecutorService 的简介
接口 java.util.concurrent.ExecutorService 表述了异步执行的机制,并且可以让任务在后台执行。一个ExecutorService 实例因此特别像一个线程池。事实上,在 java.util.concurrent 包中的 ExecutorService 的实现就是一个线程池的实现。具体介绍参见:http://blog.csdn.net/suifeng3051/article/details/49443835
相关文章推荐
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- ruby中执行周期性任务(定时任务)的3种方法
- 使用mysql事件调度器定时删除binlog
- ASP中实现执行定时任务的方法
- 调试WordPress中定时任务的相关PHP脚本示例
- java定时任务的实现方式
- node.js中使用node-schedule实现定时任务实例
- 详解PHP实现执行定时任务
- php定时计划任务的实现方法详解
- PHP定时执行任务的3种方法详解
- 详解PHP执行定时任务的实现思路
- 在WordPress中使用wp-cron插件来设置定时任务
- Nodejs中读取中文文件编码问题、发送邮件和定时任务实例
- PHP中使用sleep函数实现定时任务实例分享
- yii框架通过控制台命令创建定时任务示例
- linux使用crontab实现PHP执行计划定时任务
- 在Java的Spring框架中配置Quartz的教程
- Java的作业调度类库Quartz基本使用指南
- 最流行的java后台框架spring quartz定时任务