定时任务——xml配置
<bean name="quartzScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--<property name="dataSource" ref="dataSource"/>-->
<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
<property name="configLocation" value="classpath:quartz.properties"/>
<!--<property name="configLocation" value="classpath:quartz-jdbc.properties" />-->
<property name="startupDelay" value="300"/>
<property name="triggers">
<list>
<ref bean="heterogeneousOrgSystemTrigger"/>
</list>
</property>
</bean>
<!--与上面对应-->
<bean id="heterogeneousOrgSystemTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<value>com.epoch.bdp.credit.service.quartz.CreditQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<!--定时任务所在类,对象名-->
<entry key="targetObject" value="getMappingOrgInfoService"/>
<!--定时任务方法,方法名-->
<entry key="targetMethod" value="pushHeterogeneousOrgSystem"/>
</map>
</property>
<property name="description">
<value>描述</value>
</property>
<property name="group">
<value>组</value>
</property>
</bean>
</property>
<property name="cronExpression">
<value>0 40 23 * * ?</value> <!--每天执行一次-->
</property>
</bean>
定时任务——注解(springboot)
简直不能再简单了
直接上代码
@Component
@Configuration
@EnableScheduling
public class Sync {
//每天0点30开始同步
@SystemControllerLog(description = "每天0点30开始同步")
@Scheduled(cron = "0 30 0 * * ?")
public void syncTest()throws Exception {
SyncService.sync();
}
}
时间设置详解
特殊符号
- *是一个通配符,表示任何值,用在Minutes字段中表示每分钟。
- ?只可以用在day-of-month或者Day-of-Week字段中,用来表示不指定特殊的值。
- -用来表示一个范围,比如10-12用在Month中表示10到12月。
- ,用来表示附加的值,比如MON,WED,FRI在day-of-week字段中表示礼拜一和礼拜三和礼拜五。
- /用来表示增量,比如0/15用在Minutes字段中表示从0分开始0和15和30和45分。
- L只可以用在day-of-month或者Day-of-Week字段中,如果用在Day-of-month中,表示某个月的最后一天,1月则是表示31号,2月则表示28号(非闰年),如果用在Day-of-Week中表示礼拜六(数字7);但是如果L与数字组合在一起用在Day-of-month中,比如6L,则表示某个月的最后一个礼拜六;
<!--位置 时间域 允许值 特殊值-->
<!--1 秒 0-59 , - * /-->
<!--2 分钟 0-59 , - * /-->
<!--3 小时 0-23 , - * /-->
<!--4 日期 1-31 , - * ? / L W C-->
<!--5 月份 1-12 , - * /-->
<!--6 星期 1-7, - * ? / L C #-->
<!--7 年份(可选) 1-31 , - * /-->
<!--0 0 12 * * ? 每天12点运行-->
<!--0 15 10 ? * * 每天10:15运行-->
<!--0 15 10 * * ? 每天10:15运行-->
<!--0 15 10 * * ? * 每天10:15运行-->
<!--0 15 10 * * ? 2008 在2008年的每天10:15运行-->
<!--0 * 14 * * ? 每天14点到15点之间每分钟运行一次,开始于14:00,结束于14:59。-->
<!--0 0/5 14 * * ? 每天14点到15点每5分钟运行一次,开始于14:00,结束于14:55。-->
<!--0 0/5 14,18 * * ? 每天14点到15点每5分钟运行一次,此外每天18点到19点每5钟也运行一次。-->
<!--0 0-5 14 * * ? 每天14:00点到14:05,每分钟运行一次。-->
<!--0 10,44 14 ? 3 WED 3月每周三的14:10分到14:44,每分钟运行一次。-->
<!--0 15 10 ? * MON-FRI 每周一,二,三,四,五的10:15分运行。-->
<!--0 15 10 15 * ? 每月15日10:15分运行。-->
<!--0 15 10 L * ? 每月最后一天10:15分运行。-->
<!--0 15 10 ? * 6L 每月最后一个星期五10:15分运行。-->
<!--0 15 10 ? * 6L 2007-2009 在2007,2008,2009年每个月的最后一个星期五的10:15分运行。-->
<!--0 15 10 ? * 6#3 每月第三个星期五的10:15分运行。-->