- 浏览: 607510 次
- 性别:
- 来自: 郑州
文章分类
最新评论
-
AlanKay:
Mart_dai 写道Hi 齐晓威_518,我想问问,在exc ...
java 后台 Excel 文件生成后转化为字节流 -
18335864773:
国内的pageoffice插件也可以生成excel。也可以用流 ...
java 后台 Excel 文件生成后转化为字节流 -
JAVA_CLASSm:
你好,请问这个还有源码吗?我刚开始接触这个,想要源码学习一下. ...
同一账号不能多地登录(限制同一账号同一时刻只能一个用户登录使用,向QQ一样) -
pangjinquan:
...
前台JS获取后台的Json数据, 动态创建table并填充数据--转自一位朋友 -
lvjun106:
这是增加删除列,,,,
JQuery自动为表格增加一列
SchedulerService 只有一个多态方法schedule,SchedulerServiceImpl实现SchedulerService接口,注入 org.quartz.Schedulert和org.quartz.JobDetail,schedule方法可以动态配置 org.quartz.CronExpression或org.quartz.SimpleTrigger调度时间。
五、实现自己的org.quartz.JobDetail
在上一步中SchedulerServiceImpl需要注入org.quartz.JobDetail,在以前的静态配置中
引用
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleService" />
<property name="targetMethod" value="testMethod" />
</bean>
中使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。在这里使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。会报
引用
Caused by: java.io.NotSerializableException: Unable to serialize JobDataMap for insertion into database because the value of property 'methodInvoker' is not serializable: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.serializeJobData(StdJDBCDelegate.java:3358)
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.insertJobDetail(StdJDBCDelegate.java:515)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1102)
... 11 more
异常,google了一下,没有找到解决方法。所以在这里不能使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。,不能pojo了,需要使用org.springframework.scheduling.quartz.JobDetailBean 和org.springframework.scheduling.quartz.QuartzJobBean实现自己的 QuartzJobBean,如下:
Java 代码
1. package com.sundoctor.example.service;
2.
3. import org.quartz.JobExecutionContext;
4. import org.quartz.JobExecutionException;
5. import org.quartz.Trigger;
6. import org.springframework.scheduling.quartz.QuartzJobBean;
7.
8. public class MyQuartzJobBean extends QuartzJobBean {
9.
10. private SimpleService simpleService;
11.
12. public void setSimpleService(SimpleService simpleService) {
13. this.simpleService = simpleService;
14. }
15.
16. @Override
17. protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
18. Trigger trigger = jobexecutioncontext.getTrigger();
19. String triggerName = trigger.getName();
20. simpleService.testMethod(triggerName);
21. }
22.
23. }
package com.sundoctor.example.service;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class MyQuartzJobBean extends QuartzJobBean {
private SimpleService simpleService;
public void setSimpleService(SimpleService simpleService) {
this.simpleService = simpleService;
}
@Override
protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
Trigger trigger = jobexecutioncontext.getTrigger();
String triggerName = trigger.getName();
simpleService.testMethod(triggerName);
}
}
MyQuartzJobBean继承org.springframework.scheduling.quartz.QuartzJobBean,注入的SimpleService如下:
Java 代码
1. package com.sundoctor.example.service;
2.
3. import java.io.Serializable;
4.
5. import org.slf4j.Logger;
6. import org.slf4j.LoggerFactory;
7. import org.springframework.stereotype.Service;
8.
9. @Service("simpleService")
10. public class SimpleService implements Serializable{
11.
12. private static final long serialVersionUID = 122323233244334343L;
13. private static final Logger logger = LoggerFactory.getLogger(SimpleService.class);
14.
15. public void testMethod(String triggerName){
16. //这里执行定时调度业务
17. logger.info(triggerName);
18. }
19.
20. public void testMethod2(){
21. logger.info("testMethod2");
22. }
23. }
package com.sundoctor.example.service;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service("simpleService")
public class SimpleService implements Serializable{
private static final long serialVersionUID = 122323233244334343L;
private static final Logger logger = LoggerFactory.getLogger(SimpleService.class);
public void testMethod(String triggerName){
//这里执行定时调度业务
logger.info(triggerName);
}
public void testMethod2(){
logger.info("testMethod2");
}
}
SimpleService主要执行定时调度业务,在这里我只是简单打印一下log日志。SimpleService需要实现 java.io.Serializable接口,否则会报
引用
Caused by: java.io.InvalidClassException: com.sundoctor.example.service.SimpleService; class invalid for deserialization
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:587)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
... 64 more
异常。
配置applicationContext-quartz.xml文件:
引用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.sundoctor.example.service.MyQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="simpleService">
<ref bean="simpleService" />
</entry>
</map>
</property>
</bean>
</beans>
quartzScheduler中没有了
引用
<property name="triggers">
<list>
...
</list>
/property>
配置,通过SchedulerService动态加入CronTrigger或SimpleTrigger。
在红色的
引用
<property name="jobDataAsMap">
<map>
<entry key="simpleService">
<ref bean="simpleService" />
</entry>
</map>
</property>
中需要注入调度业务类,否则会报空指指错误。
dataSource:项目中用到的数据源,里面包含了quartz用到的12张数据库表;
applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文;
configLocation:用于指明quartz的配置文件的位置,如果不用spring配置quartz的话,本身quartz是通过一个配置文件进行配置的,默认名称是quartz.properties,里面配置的参数在quartz的doc文档中都有介绍,可以调整quartz,我在项目中也用这个文件部分的配置了一些属性,代码如下:
引用
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 = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.useProperties = true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.maxMisfiresToHandleAtATime=1
这里面没有数据源相关的配置部分,采用spring注入datasource的方式已经进行了配置。
六、测试
运行如下测试类
Java 代码
1. package com.sundoctor.example.test;
2.
3. import java.text.ParseException;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. import org.springframework.context.ApplicationContext;
8. import org.springframework.context.support.ClassPathXmlApplicationContext;
9.
10. import com.sundoctor.quartz.service.SchedulerService;
11.
12. public class MainTest {
13.
14.
17. public static void main(String[] args) {
18. ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});
19. SchedulerService schedulerService = (SchedulerService)springContext.getBean("schedulerService");
20.
21. //执行业务逻辑...
22.
23. //设置调度任务
24. //每10秒中执行调试一次
25. schedulerService.schedule("0/10 * * ? * * *");
26.
27. Date startTime = parse("2009-06-01 22:16:00");
28. Date endTime = parse("2009-06-01 22:20:00");
29.
30. //2009-06-01 21:50:00开始执行调度
31. schedulerService.schedule(startTime);
32.
33. //2009-06-01 21:50:00开始执行调度,2009-06-01 21:55:00结束执行调试
34. //schedulerService.schedule(startTime,endTime);
35.
36. //2009-06-01 21:50:00开始执行调度,执行5次结束
37. //schedulerService.schedule(startTime,null,5);
38.
39. //2009-06-01 21:50:00开始执行调度,每隔20秒执行一次,执行5次结束
40. //schedulerService.schedule(startTime,null,5,20);
41.
42. //等等,查看 com.sundoctor.quartz.service.SchedulerService
43. }
44.
45. private static Date parse(String dateStr){
46. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
47. try {
48. return format.parse(dateStr);
49. } catch (ParseException e) {
50. throw new RuntimeException(e);
51. }
52. }
53.
54. }
package com.sundoctor.example.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sundoctor.quartz.service.SchedulerService;
public class MainTest {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});
SchedulerService schedulerService = (SchedulerService)springContext.getBean("schedulerService");
//执行业务逻辑...
//设置调度任务
//每10秒中执行调试一次
schedulerService.schedule("0/10 * * ? * * *");
Date startTime = parse("2009-06-01 22:16:00");
Date endTime = parse("2009-06-01 22:20:00");
//2009-06-01 21:50:00开始执行调度
schedulerService.schedule(startTime);
//2009-06-01 21:50:00开始执行调度,2009-06-01 21:55:00结束执行调试
//schedulerService.schedule(startTime,endTime);
//2009-06-01 21:50:00开始执行调度,执行5次结束
//schedulerService.schedule(startTime,null,5);
//2009-06-01 21:50:00开始执行调度,每隔20秒执行一次,执行5次结束
//schedulerService.schedule(startTime,null,5,20);
//等等,查看com.sundoctor.quartz.service.SchedulerService
}
private static Date parse(String dateStr){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return format.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
输出
引用
[2009-06-02 00:08:50]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:20]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:30]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:40]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:50]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:11:00]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:11:10]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
这样只是简单的将quartz trigger名称打印出来。
这样通过SchedulerService就可以动态配置调度时间。其实SchedulerService 还可扩展,比如可以注入多个JobDetail,调度不同的JobDetail。
五、实现自己的org.quartz.JobDetail
在上一步中SchedulerServiceImpl需要注入org.quartz.JobDetail,在以前的静态配置中
引用
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleService" />
<property name="targetMethod" value="testMethod" />
</bean>
中使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。在这里使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。会报
引用
Caused by: java.io.NotSerializableException: Unable to serialize JobDataMap for insertion into database because the value of property 'methodInvoker' is not serializable: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.serializeJobData(StdJDBCDelegate.java:3358)
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.insertJobDetail(StdJDBCDelegate.java:515)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1102)
... 11 more
异常,google了一下,没有找到解决方法。所以在这里不能使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。,不能pojo了,需要使用org.springframework.scheduling.quartz.JobDetailBean 和org.springframework.scheduling.quartz.QuartzJobBean实现自己的 QuartzJobBean,如下:
Java 代码
1. package com.sundoctor.example.service;
2.
3. import org.quartz.JobExecutionContext;
4. import org.quartz.JobExecutionException;
5. import org.quartz.Trigger;
6. import org.springframework.scheduling.quartz.QuartzJobBean;
7.
8. public class MyQuartzJobBean extends QuartzJobBean {
9.
10. private SimpleService simpleService;
11.
12. public void setSimpleService(SimpleService simpleService) {
13. this.simpleService = simpleService;
14. }
15.
16. @Override
17. protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
18. Trigger trigger = jobexecutioncontext.getTrigger();
19. String triggerName = trigger.getName();
20. simpleService.testMethod(triggerName);
21. }
22.
23. }
package com.sundoctor.example.service;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class MyQuartzJobBean extends QuartzJobBean {
private SimpleService simpleService;
public void setSimpleService(SimpleService simpleService) {
this.simpleService = simpleService;
}
@Override
protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
Trigger trigger = jobexecutioncontext.getTrigger();
String triggerName = trigger.getName();
simpleService.testMethod(triggerName);
}
}
MyQuartzJobBean继承org.springframework.scheduling.quartz.QuartzJobBean,注入的SimpleService如下:
Java 代码
1. package com.sundoctor.example.service;
2.
3. import java.io.Serializable;
4.
5. import org.slf4j.Logger;
6. import org.slf4j.LoggerFactory;
7. import org.springframework.stereotype.Service;
8.
9. @Service("simpleService")
10. public class SimpleService implements Serializable{
11.
12. private static final long serialVersionUID = 122323233244334343L;
13. private static final Logger logger = LoggerFactory.getLogger(SimpleService.class);
14.
15. public void testMethod(String triggerName){
16. //这里执行定时调度业务
17. logger.info(triggerName);
18. }
19.
20. public void testMethod2(){
21. logger.info("testMethod2");
22. }
23. }
package com.sundoctor.example.service;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service("simpleService")
public class SimpleService implements Serializable{
private static final long serialVersionUID = 122323233244334343L;
private static final Logger logger = LoggerFactory.getLogger(SimpleService.class);
public void testMethod(String triggerName){
//这里执行定时调度业务
logger.info(triggerName);
}
public void testMethod2(){
logger.info("testMethod2");
}
}
SimpleService主要执行定时调度业务,在这里我只是简单打印一下log日志。SimpleService需要实现 java.io.Serializable接口,否则会报
引用
Caused by: java.io.InvalidClassException: com.sundoctor.example.service.SimpleService; class invalid for deserialization
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:587)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
... 64 more
异常。
配置applicationContext-quartz.xml文件:
引用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.sundoctor.example.service.MyQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="simpleService">
<ref bean="simpleService" />
</entry>
</map>
</property>
</bean>
</beans>
quartzScheduler中没有了
引用
<property name="triggers">
<list>
...
</list>
/property>
配置,通过SchedulerService动态加入CronTrigger或SimpleTrigger。
在红色的
引用
<property name="jobDataAsMap">
<map>
<entry key="simpleService">
<ref bean="simpleService" />
</entry>
</map>
</property>
中需要注入调度业务类,否则会报空指指错误。
dataSource:项目中用到的数据源,里面包含了quartz用到的12张数据库表;
applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文;
configLocation:用于指明quartz的配置文件的位置,如果不用spring配置quartz的话,本身quartz是通过一个配置文件进行配置的,默认名称是quartz.properties,里面配置的参数在quartz的doc文档中都有介绍,可以调整quartz,我在项目中也用这个文件部分的配置了一些属性,代码如下:
引用
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 = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.useProperties = true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = false
org.quartz.jobStore.maxMisfiresToHandleAtATime=1
这里面没有数据源相关的配置部分,采用spring注入datasource的方式已经进行了配置。
六、测试
运行如下测试类
Java 代码
1. package com.sundoctor.example.test;
2.
3. import java.text.ParseException;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. import org.springframework.context.ApplicationContext;
8. import org.springframework.context.support.ClassPathXmlApplicationContext;
9.
10. import com.sundoctor.quartz.service.SchedulerService;
11.
12. public class MainTest {
13.
14.
17. public static void main(String[] args) {
18. ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});
19. SchedulerService schedulerService = (SchedulerService)springContext.getBean("schedulerService");
20.
21. //执行业务逻辑...
22.
23. //设置调度任务
24. //每10秒中执行调试一次
25. schedulerService.schedule("0/10 * * ? * * *");
26.
27. Date startTime = parse("2009-06-01 22:16:00");
28. Date endTime = parse("2009-06-01 22:20:00");
29.
30. //2009-06-01 21:50:00开始执行调度
31. schedulerService.schedule(startTime);
32.
33. //2009-06-01 21:50:00开始执行调度,2009-06-01 21:55:00结束执行调试
34. //schedulerService.schedule(startTime,endTime);
35.
36. //2009-06-01 21:50:00开始执行调度,执行5次结束
37. //schedulerService.schedule(startTime,null,5);
38.
39. //2009-06-01 21:50:00开始执行调度,每隔20秒执行一次,执行5次结束
40. //schedulerService.schedule(startTime,null,5,20);
41.
42. //等等,查看 com.sundoctor.quartz.service.SchedulerService
43. }
44.
45. private static Date parse(String dateStr){
46. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
47. try {
48. return format.parse(dateStr);
49. } catch (ParseException e) {
50. throw new RuntimeException(e);
51. }
52. }
53.
54. }
package com.sundoctor.example.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sundoctor.quartz.service.SchedulerService;
public class MainTest {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});
SchedulerService schedulerService = (SchedulerService)springContext.getBean("schedulerService");
//执行业务逻辑...
//设置调度任务
//每10秒中执行调试一次
schedulerService.schedule("0/10 * * ? * * *");
Date startTime = parse("2009-06-01 22:16:00");
Date endTime = parse("2009-06-01 22:20:00");
//2009-06-01 21:50:00开始执行调度
schedulerService.schedule(startTime);
//2009-06-01 21:50:00开始执行调度,2009-06-01 21:55:00结束执行调试
//schedulerService.schedule(startTime,endTime);
//2009-06-01 21:50:00开始执行调度,执行5次结束
//schedulerService.schedule(startTime,null,5);
//2009-06-01 21:50:00开始执行调度,每隔20秒执行一次,执行5次结束
//schedulerService.schedule(startTime,null,5,20);
//等等,查看com.sundoctor.quartz.service.SchedulerService
}
private static Date parse(String dateStr){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return format.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
输出
引用
[2009-06-02 00:08:50]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:20]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:30]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:40]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:50]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:11:00]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:11:10]INFO com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
这样只是简单的将quartz trigger名称打印出来。
这样通过SchedulerService就可以动态配置调度时间。其实SchedulerService 还可扩展,比如可以注入多个JobDetail,调度不同的JobDetail。
发表评论
-
Spring4.03+Hibernate4.3.5整合
2014-12-22 16:07 1286通过maven工具进行构建: 第一步: 第二步 ... -
springMVC详解以及注解说明
2014-09-17 22:46 716基于注释(Annotation)的配置有越来越流行的趋势,S ... -
java 的 文件、文件夹 的建立、删除、复制以及移动等功能 操作
2014-08-23 09:00 723package util; import java. ... -
浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
2014-02-20 21:29 646hibernate工作原理原理:1.读取并解析配置文件2. ... -
Spring + ehCache实现缓存-转贴
2014-01-07 22:08 615转贴Spring AOP+ehCache简单缓存系统解决方案 ... -
如何理解Spring对缓存的支持
2014-01-07 22:05 8391.Spring缓存机制的理解 在Spring缓存机制中, ... -
spring 整合 ehcache 实现dao缓存
2014-01-07 22:00 1002Ehcache在很多项目中都出现过,用法也比较简单。一般的加些 ... -
一个牛人对sping的比喻
2014-01-06 21:57 438大名鼎鼎的Spring框架 有人曾说2005年一片叫春之声 ... -
Spring声明式事务配置管理方法
2013-12-14 13:40 523环境配置 项目使用SSH架构,现在要添加Spring事务 ... -
Spring Quartz动态配置时间-重要的
2012-10-29 22:48 1660什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什 ... -
quartz 在spring 中配置服务 定时执行和循环执行事件
2012-10-29 22:24 1048uartz 可以在spring中配置 定时执行和循环执行事件、 ... -
Spring中Quartz的配置
2012-10-28 22:27 974jar包: quartz.1.6.0.jar ... -
Spring Quartz定时器 的动态调度
2012-10-28 22:00 1691众所周知spring 的Quartz定时器的功能非常强大,可以 ... -
Spring Quartz动态配置时间
2012-10-28 21:45 11543) 记录时间规则 我将时间规则存入数据库中,目 ...
相关推荐
Spring Quartz如何动态配置时间
本人花费多日时间,亲测有效。所需文件代码完整,只需导入常用开发IDE和mysql数据库即可正常使用。
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间、频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quartz与spring的组合...
Spring Quartz动态配置时间.rar
Quartz + spring简单配置多个任务调度 spring配置文件 简单配置
Quartz 是一个强大的企业级 Schedule 工具,也是目前最好的开源 Schedule 工具。实例中quartz在spring中的应用及quartz详细时间配置。
spring框架配置的时间调度插件,提供jar包 及 配置实例
Spring自带了多线程设置:quartz,在quartz的设置中,时间字段CronExpression的设置比较纠结,一个Cron表达式是一个由六至七个字段组成由空格分隔的字符串,其中6个字段是必须的而一个是可选的,文档详细讲解了这个...
Spring+Quartz 从数据库中获取定时任务和定时时间,动态实现对定时任务的增删改查,部署到tomcat即可看到定时任务执行效果。本人亲测,可用!
虽然可以通过属性文件(在属性文件中可以指定 JDBC 事务的数据源、全局作业和/或触发器侦听器、插件、线程池,以及更多)配置 Quartz,但它根本没有与应用程序服务器的上下文或引用集成在一起。结果就是作业不能访问...
下载导入就可以用,执行test类的main方法加载spring applicationContext.xml配置文件,启动Quartz定时器.修改spring配置文件的配置时间.详细看注释
传统Spring+Quartz实现的定时器一般都是通过配置文件中配置的,这就意味着如果我们修改了定时任务的时间,就得重新启动一下Tomcat,而且每次新建一个任务就得在spring中添加相关的配置信息,非常麻烦。基于上面的...
Quartz学习文档 Spring + Quartz配置详细实例 jar包 Quartz时间格式设置
NULL 博文链接:https://daoshud1.iteye.com/blog/1955099
NULL 博文链接:https://hzywy.iteye.com/blog/1647076
spring不能正常加载,由于第一次使用Quartz,我就是当时忽略了这一点,浪费了很多时间来调配置,其实配置一点没有错误。 3. 你会问为什么用spring后就不用 quartz.properties quartz-jobsxml这两个文件了么? ...
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz框架包含了调度器监听、作业和触发器监听。你可以配置作业和触发器监听为全局监听或者是特定于作业和触发器的监听...
quartz1.6.5 整合 Hibernate、Spring,本例支持同时运行多个不同的任务,每个任务动态调用的方法和时间都不一样; 附件中有支持各种数据库的脚本;本例支持Oracle10g数据库脚本;开发测试使用的IDE工具MyEclipse6.5;...
quartz1.6.5 整合 Hibernate、Spring,本例支持同时运行多个不同的任务,每个任务动态调用的方法和时间都不一样; 附件中有支持各种数据库的脚本;本例支持Oracle10g数据库脚本;开发测试使用的IDE工具MyEclipse6.5;...