applicationContext-quartz.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "springserver" DispatcherServlet. default-autowire="byName" -->
<beans>
<!-- 任务 -->
<bean id="messageSenderTask" class="com.sillycat.admin.service.MessageSenderTask" />
<!-- 其他的配置和正常的sping配置文件一个意思 -->
<bean id="messageTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="messageSenderTask" />
</property>
<property name="targetMethod">
<value>sendMessage</value>
</property>
</bean>
<!--
<bean id="messageTaskScheduledTask" class="org.springframework.scheduling.quartz.CronTriggerBean">
-->
<!-- start the task 0:0:0 every day-->
<!-- Seconds Minutes Hours Day-of-Month Month Day-of-Week -->
<!--
<property name="cronExpression">
<value>0 0 0 * * ?</value>
-->
<!-- 这里指定运行的参数,现在指定是每天天的0点准时执行 -->
<!--
</property>
<property name="jobDetail">
<ref bean="messageTaskDetail" />
</property>
</bean>
-->
<bean id="messageTaskScheduledTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="startDelay">
<value>60000</value><!-- 服务启动一分钟后执行 -->
</property>
<property name="repeatInterval">
<value>60000</value><!-- 每隔1分钟执行一次 -->
</property>
<property name="jobDetail">
<ref bean="messageTaskDetail" />
</property>
</bean>
<bean id="SmsSendTaskFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="messageTaskScheduledTask" />
</list>
</property>
<!-- <property name="dataSource" ref="dataSource"/> -->
<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 = 10
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,不过HSQLDB不支持select * from locks for update 的语法
#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.oracle.OracleDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
使用cronExpression的语法:
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- - Application context definition for "springserver" DispatcherServlet. default-autowire="byName" -->
<beans>
<!-- 任务 -->
<bean id="messageSenderTask" class="com.sillycat.admin.service.MessageSenderTask" />
<!-- 其他的配置和正常的sping配置文件一个意思 -->
<bean id="messageTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="messageSenderTask" />
</property>
<property name="targetMethod">
<value>sendMessage</value>
</property>
</bean>
<!--
<bean id="messageTaskScheduledTask" class="org.springframework.scheduling.quartz.CronTriggerBean">
-->
<!-- start the task 0:0:0 every day-->
<!-- Seconds Minutes Hours Day-of-Month Month Day-of-Week -->
<!--
<property name="cronExpression">
<value>0 0 0 * * ?</value>
-->
<!-- 这里指定运行的参数,现在指定是每天天的0点准时执行 -->
<!--
</property>
<property name="jobDetail">
<ref bean="messageTaskDetail" />
</property>
</bean>
-->
<bean id="messageTaskScheduledTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="startDelay">
<value>60000</value><!-- 服务启动一分钟后执行 -->
</property>
<property name="repeatInterval">
<value>60000</value><!-- 每隔1分钟执行一次 -->
</property>
<property name="jobDetail">
<ref bean="messageTaskDetail" />
</property>
</bean>
<bean id="SmsSendTaskFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="messageTaskScheduledTask" />
</list>
</property>
<!-- <property name="dataSource" ref="dataSource"/> -->
<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 = 10
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,不过HSQLDB不支持select * from locks for update 的语法
#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.oracle.OracleDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
使用cronExpression的语法:
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
The '*' character is used to specify all values. For example, "*" in the minute field means "every minute".
“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".
“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。
The ',' character is used to specify additional values. For example "MON,WED,FRI" in the day-of-week field means "the days Monday, Wednesday, and Friday".
“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.