参考一:
https://blog.csdn.net/qq_35010942/article/details/84640173
使用spring3.1 以注解的形式配置定时任务
当我想把定时任务的时间放入配置文件里去,然后动态获取的时候,总是报
cron expression must consist of 6 fields (found 1 in ${job.schedule})
百思不得其解
通过google在下面这个网址下找到解决办法
https://stackoverflow.com/questions/36418882/scheduled-throwing-exception-for-cron-expression
我的定时任务类:
/**
*
*/
package com.dounion.bas.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.dounion.bas.business.BasSyndataBusiness;
/**
* @author Administrator
*
*/
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
@PropertySource("classpath:/bas.properties")
public class TimerTask_bas{
/** 注入行动Dao类 **/
@Autowired
private BasSyndataBusiness basSyndataBusiness;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
/**
*
* author: kangyu
* 2018-11-26 下午5:32:29
* @Description: // 查询同步日志表发送给库点(每隔1分钟触发一次)
*/
@Scheduled(cron = "${job.schedule}")
public void sendDataToLibraryPoint() {
basSyndataBusiness.sendDataToLibraryPoint();
}
@Scheduled(cron = "${job.schedule}")
public void dealDataInRedis() {
basSyndataBusiness.dealDataInRedis();
}
}
TimerTask_bas类主要加了以下几行才解决如上bug
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
配置文件bas.properties是这么写的job.schedule = 0 */1 * * * ?
。application.xml
主要代码:
<context:annotation-config />
<context:component-scan base-package="com.dounion" />
<aop:aspectj-autoproxy proxy-target-class="true" />
参考二:https://cnsyear.com/posts/61da5c6d.html
Spring默认定时@Scheduled不支持L关键字
每个月的最后一天10:15”执行定时任务,@Scheduled(cron = “0 15 10 L * ?”),它在初始化Spring上下文时出现了错误:
Caused by: java.lang.IllegalStateException: Encountered invalid
@Scheduled method 'execute': For input string: "L"
原因是不支持L,W,C特殊字符
- 解决方案:
首先,为每个可能的最后几天调用调度程序(28,29,30,31)。然后,在函数块内用if块检查这是否是最后一个日期。如果是,则执行预期的任务。
@Scheduled(cron = "0 15 10 28-31 * ?")
public void execute() {
final Calendar c = Calendar.getInstance();
if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
//是最后一天
System.out.println("信息员等级升降级。。。。");
}
}
参考链接:
https://stackoverflow.com/questions/54415868/spring-schedule-last-day-of-month-not-working
https://stackoverflow.com/questions/31445395/cron-expression-is-not-working-for-last-day-of-the-month
https://stackoverflow.com/questions/18079289/workaround-for-cronsequencegenerator-last-day-of-month?noredirect=1&lq=
1