分类:
2008-11-11 20:37:52
一、定时调度服务
1.xml文件 JobQuartz1.xml
xml version="1.0" encoding="UTF-8"?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">
<beans>
<bean id ="sjob" class="service.UserService"/>
<bean name="userJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>schedule.TestJobvalue>
property>
<property name="jobDataAsMap">
<map>
<entry key="service"><ref local="sjob"/>entry>
map>
property>
bean>
<bean id = "cron" class = "org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="userJob"/>
property>
<property name="cronExpression">
<value>0 0 15 * * ?value>
property>
bean>
<bean autowire = "no" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local ="cron"/>
list>
property>
bean>
beans>
2.服务类
package service;
public class UserService {
public void doService(){
System.out.println("User service started!");
}
}
3.调度类
package schedule;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import service.UserService;
public class TestJob extends QuartzJobBean {
private UserService service;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
// TODO Auto-generated method stub
service.doService();
}
}
4.测试类
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class QuartzTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Test start!");
ApplicationContext ctx = new ClassPathXmlApplicationContext("xml/JobQuartz1.xml");
// System.out.println("Test end!");
}
}
5.需要加入spring.jar quartz-all-1.6.0.jar log4j-1.2.14.jar commons-collections.jar jta.jar commons-logging.jar这几个包
6.log4j.properties内容
|
二、定时执行某方法
1.xml文件 JobQuartz.xml
xml version="1.0" encoding="UTF-8"?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">
<beans>
<bean id="job" class="test.TJob">bean>
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref local="job"/>
property>
<property name="targetMethod">
<value>doAuthvalue>
property>
bean>
<bean id = "cron" class = "org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
property>
<property name="cronExpression">
<value>0 0 15 * * ?value>
property>
bean>
<bean autowire = "no" class = "org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local ="cron"/>
list>
property>
bean>
beans>
2.要调度的类
package test;
public class TJob {
public void doAuth(){
System.out.println("Task starting...");
}
}
3.测试类
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class QuartzTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Test start!");
ApplicationContext ctx = new ClassPathXmlApplicationContext("xml/JobQuartz.xml");
// System.out.println("Test end!");
}
}
三、在web.xml载入applicationContext.xml
图-1
b. 配置web.xml
|
c. 配置applicationContext.xml
|
d. HiJob类
|
e. MyJob类
|
f. Client测试类
|
在Spring中集成Quartz极大地方便了任务调度功能的实现,并且通过SchedulerFactoryBean能够和Spring容器的生命周期关联,在Spring容器启动时,启动调度器。