需要的包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
spring中配置bean
<!--调度工厂-->
<bean id="myScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="userMonitorTriggers"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
<!--触发-->
<bean id="userMonitorTriggers"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="monitorUserBuyTimesJob">
</property>
<property name="cronExpression">
<!--<value>0 0 */4 * * ?</value>--> <!--每天四个小时查询一次-->
<value>0 */2 * * * ?</value>
</property>
</bean>
<!--任务详情-->
<bean id="monitorUserBuyTimesJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="userMonitorService"/>
<property name="targetMethod" value="monitorUserBuyTimes"/>
<property name="concurrent" value="false"/>
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
</bean>
<!--业务类-->
<bean id="userMonitorService" class="com.bjs.riskmng.monitor.service.impl.UserMonitorServiceImpl">
</bean>
Cron Expressions 轮询表达式(我翻译的,个人理解)
Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule. These sub-expression are separated with white-space, and represent
- Seconds
- Minutes
- Hours
- Day-of-Month
- Month
- Day-of-Week
- Year (optional field)
轮询表达式用来初始化调度器,轮询表达式是一个由七个空格分开的子表达式组成的字符串。它描述了调度详情。这些子表达式由空格分开,分表表示 “秒 分 小时 每个月份中哪一天 月份 每周的哪一天 年(可选项)”
Example Cron Expressions 轮询表达式举例
CronTrigger Example 1 - an expression to create a trigger that simply fires every 5 minutes
“0 0/5 * * * ?” 每5分钟执行一次
CronTrigger Example 2 - an expression to create a trigger that fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).
“10 0/5 * * * ?” 每五分钟的第十秒执行
CronTrigger Example 3 - an expression to create a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.
“0 30 10-13 ? * WED,FRI” 每天10点到13点种,每半个小时执行一次。
CronTrigger Example 4 - an expression to create a trigger that fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30
“0 0/30 8-9 5,20 * ?” 每个月的第5号和第20号,每天8点到9点,每半个小时执行一次【8:00,8:30,9:00,9:30】。(比较绕,静下心来好好看)
来源:oschina
链接:https://my.oschina.net/u/2494581/blog/806200