标签:任务调度 quartz maven spring 并发
最近在写一个任务调度程序,需要每隔几秒查询数据库,并取出数据做一些处理操作。使用到了Quartz任务调度框架。
Quartz包含几个重要的对象,分别为任务(Job),触发器(Trigger),调度器(Scheduler)
直接使用quartz实现一个简单的例子。
1.创建一个SimpleJob类,实现Job接口
public class SimpleJob implements Job{
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("执行了一个Job"+jobExecutionContext.getJobDetail().getDescription());
}
}
2.使用SimpleJob创建一个JobDetail实例
//创建一个JobDetail实例
JobBuilder builder=JobBuilder.newJob(SimpleJob.class);
builder.withIdentity("test","test");
builder.withDescription("my test");
JobDetail jobDetail=builder.build();
3.创建一个Trigger,在这里使用SimpleTrigger创建,从现在开始每隔1s执行一次。
//创建一个调度规则,1s中运行一次,现在开始
Trigger trigger = TriggerBuilder.newTrigger().withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(1)
.repeatForever()).
startNow().build();
4.创建一个Scheduler,并且把以上创建的Trigger和jobDetail注册进去,并且开始执行
try {
//获取一个调度器
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
//注册jobDetail,trigger到调度器
scheduler.scheduleJob(jobDetail,trigger);
//开始执行
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
1.实现两个Job
public class MyJob {
public void process(){
System.out.println("处理Job.."+Thread.currentThread().getId());
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void process1(){
System.out.println("处理Job1.."+Thread.currentThread().getId());
}
}
配置spring
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="myJob" class="org.yywang.spring.MyJob"></bean>
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob"></ref>
</property>
<property name="targetMethod">
<value>process</value>
</property>
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent">
<value>true</value>
</property>
</bean>
<bean id="myJobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob"></ref>
</property>
<property name="targetMethod">
<value>process1</value>
</property>
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent">
<value>true</value>
</property>
</bean>
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail"></ref>
</property>
<property name="cronExpression">
<value>0/2 * * * * ?</value>
</property>
</bean>
<bean id="myTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail1"></ref>
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"></ref>
<ref bean="myTrigger1"></ref>
</list>
</property>
<property name="autoStartup">
<value>true</value>
</property>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
</beans>
quartz.properties
#============================================================================
# 配置 Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
#============================================================================
# 配置执行线程池
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#============================================================================
# 配置 JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
#内存中JobStore, 服务器重启时执行记录会丢失
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#数据库中JobStore
#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
客户端初始化spring即可
new ClassPathXmlApplicationContext("classpath:spring_*.xml");
参考如下:
http://liuzidong.iteye.com/blog/1119773
http://www.iteye.com/topic/582119
http://blog.itpub.net/183473/viewspace-434672
<properties>
<org.springframework.version>3.2.3.RELEASE</org.springframework.version>
</properties>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.7</version>
</dependency>
http://www.ibm.com/developerworks/cn/java/j-quartz/index.html
http://www.blogjava.net/baoyaer/articles/155645.html
http://www.blogjava.net/supercrsky/articles/199443.html
标签:任务调度 quartz maven spring 并发
原文地址:http://blog.csdn.net/wangyan9110/article/details/37992337