我试图让我的代码按照固定的时间表执行,基于Spring cron表达式.我希望每天1:01:am执行代码.我尝试了下面的表达式,但这对我来说并没有起作用.这里的语法有什么问题?
@Scheduled(cron = "0 1 1 ? * *")
public void resetCache() {
// ...
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个具有每60秒执行一次的cron作业的应用程序.应用程序配置为在需要时扩展到多个实例.我只想每60秒在一个实例上执行任务(在任何节点上).开箱即用,我无法找到解决方案,我很惊讶以前没有多次询问过.我使用的是Spring 4.1.6.
<task:scheduled-tasks>
<task:scheduled ref="beanName" method="execute" cron="0/60 * * * * *"/>
</task:scheduled-tasks>
Run Code Online (Sandbox Code Playgroud) @Scheduled(fixedDelay = 5000)
public void myJob() {
Thread.sleep(12000);
}
Run Code Online (Sandbox Code Playgroud)
如果上一个例程尚未完成,如何防止此弹簧作业运行?
我一直在考虑在编译时评估注释值的Java特性,它似乎真的难以外化注释值.
但是,我不确定它是否真的不可能,所以我很感激任何建议或明确的答案.
更重要的是,我试图外化一个注释值来控制Spring中调度方法调用之间的延迟,例如:
public class SomeClass {
private Properties props;
private static final long delay = 0;
@PostConstruct
public void initializeBean() {
Resource resource = new ClassPathResource("scheduling.properties");
props = PropertiesLoaderUtils.loadProperties(resource);
delay = props.getProperties("delayValue");
}
@Scheduled(fixedDelay = delay)
public void someMethod(){
// perform something
}
}
Run Code Online (Sandbox Code Playgroud)
假设它scheduling.properties在classpath上并包含属性键delayValue及其对应的long值.
现在,这段代码有明显的编译错误,因为我们试图为final变量赋值,但这是强制性的,因为我们不能将变量赋值给注释值,除非它是static final.
有没有办法解决这个问题?我一直在考虑Spring的自定义注释,但根本问题仍然存在 - 如何将外化值分配给注释?
欢迎任何想法.
编辑:一个小的更新 - Quartz集成对于这个例子来说太过分了.我们只需要以亚分钟的分辨率进行定期执行即可.
我是Spring-boot(版本1.3.6)和Quartz的新手,我想知道使用Spring-scheduler创建任务有什么区别:
@Scheduled(fixedRate = 40000)
public void reportCurrentTime() {
System.out.println("Hello World");
}
Run Code Online (Sandbox Code Playgroud)
和石英方式:
0. Create sheduler.
1. Job which implements Job interface.
2. Create JobDetail which is instance of the job using the builder org.quartz.JobBuilder.newJob(MyJob.class)
3. Create a Triger
4. Finally set the job and the trigger to the scheduler
Run Code Online (Sandbox Code Playgroud)
在代码中:
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
System.err.println("Hello!");
}
}
Run Code Online (Sandbox Code Playgroud)
和sheduler:
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); …Run Code Online (Sandbox Code Playgroud) 如何从spring定制"@Scheduled"注释的异常处理?
我有cron作业将在服务器(Tomcat 6)中触发,当发生任何异常时,我需要做一些处理.
Spring版本3.2 Tomcat Server 6
如何在Spring Boot IntegrationTest上禁用计划自动启动?
谢谢.
我使用@ScheduledSpring框架中的注释来调用方法.但是我的设置中有多个节点,我不希望它们全部在同一时间运行.所以我想将一个随机值设置为初始延迟,以使它们相互抵消.
import org.springframework.scheduling.annotation.Scheduled;
@Scheduled(fixedRate = 600000, initialDelay = <random number between 0 and 10 minutes> )
Run Code Online (Sandbox Code Playgroud)
不幸的是,我只允许在这里使用常量表达式.还有其他方法吗?我想过使用Spring表达式语言.
我使用Spring Framework Scheduled来安排我的工作使用cron每5分钟运行一次.但有时我的工作无限期地等待外部资源,我不能把超时放在那里.我不能fixedDelay像以前的过程一样进入等待无限模式,我必须每隔5分钟刷新一次数据.
所以我在Spring框架中寻找任何一个选项来在它成功运行Scheduled之后停止该进程/线程fixed-time.
我发现下面的设置初始化ThreadPoolExecutor为120秒keepAliveTime,我把它放在@Configuration课堂上.任何人都能告诉我这项工作是否符合我的预期.
@Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
int coreThreads = 8;
int maxThreads = 20;
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
coreThreads, maxThreads, 120L,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()
);
threadPoolExecutor.allowCoreThreadTimeOut(true);
return threadPoolExecutor;
}
Run Code Online (Sandbox Code Playgroud) 我在Spring Boot(ver 1.4.2)应用程序中有很多计划任务,并希望使用一个处理程序捕获它们的所有异常,就像使用@ExceptionHandler注释的普通控制器一样.由于线程的原因,此方法不适用于使用@Scheduled注释定义的任务:
@Component
public class UpdateJob {
@Transactional
@Scheduled(cron = "0 1 0 * * *")
public void runUpdateUsers() {
userService.updateUsers();
}
@ExceptionHandler
public void handle(Exception e) {
// some more logic here
logger.error(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
@ExceptionHandler不适用于@Scheduled方法(事实证明它并不意味着).相反,Spring Boot使用它自己的LoggingErrorHandler:
2016-12-08 15:49:20.016 ERROR 23119 --- [pool-7-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
Run Code Online (Sandbox Code Playgroud)
我可以以某种方式替换或提供计划任务的默认异常处理程序吗?或者它是否有意义(并且可能)切换到PropagatingErrorHandler,据我所知,它会进一步传播错误?有没有其他方法可以仅使用Java配置(没有XML)来实现目标?
这不是这个问题的重复,因为它明确要求基于Java配置而不是XML的解决方案(因此在没有任何XML配置的情况下将其合并到Spring Boot项目中是不错的).
还有一些答案演示了如何从头开始配置TaskScheduler.例如,此答案要求您还定义池大小,最大池大小,队列容量.这是一个解决方案,也需要非常广泛的配置.文档说明了如何配置其他方面,但没有说明如何指定错误处理.但是Java配置所需的最小工作量是什么,这样我才能最大限度地保留Spring Boot默认值(线程池,执行器配置等).