您的位置:首页 > 编程语言 > Java开发

quartz定时器在springmvc中的用法

2016-06-06 11:03 826 查看
java定时器,spring定时器,quartz定时器的区别与用法,推荐 
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  quartz 定时任务