Tag: spring scheduled

Spring @Scheduled注释

如何动态地使用Spring的@Scheduled注释? CronTrigger(String expression, TimeZone timeZone) http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronTrigger.html#CronTrigger-java.lang.String-java.util.TimeZone- 由于我在数据库中有多个时区,我该如何动态传递它们? 我在我的代码中试过这个: TimeZone timezone = null; String timezone1 = null; public SchedulerBean(String timezone2) { this.timezone1 = timezone2; //constructor } @Scheduled(cron=”0 0 8 * * ?”, zone =timezone.getTimeZone(timezone1) ) //Error at this line public void sendQuestionNotif() { //……code } 这是我得到的错误 , *Type mismatch: cannot convert from TimeZone to String* 请帮帮我。 […]

在Spring Boot IntegrationTest上禁用@Schedule

如何在Spring Boot IntegrationTest上禁用计划自动启动? 谢谢。

使用Spring的Quartz作业和调度任务之间的区别?

我是Spring-boot(版本1.3.6)和Quartz的新手,我想知道使用Spring-scheduler创建任务有什么区别: @Scheduled(fixedRate = 40000) public void reportCurrentTime() { System.out.println(“Hello World”); } 和石英方式 : 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 在代码中: public class HelloJob implements […]

将外化值注入Spring注释

我一直在考虑在编译时评估注释值的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 } } 假设scheduling.properties位于类路径上,并包含属性键delayValue及其对应的long值。 现在,这段代码有明显的编译错误,因为我们试图为final变量赋值,但这是必须的,因为我们不能将变量赋值给注释值,除非它是static final 。 有没有办法解决这个问题? 我一直在考虑Spring的自定义注释,但根本问题仍然存在 – 如何将外化值分配给注释? 欢迎任何想法。 编辑:一个小的更新 […]

停止Spring计划执行,如果它在一段固定时间后挂起

我使用Spring Framework的Scheduled来安排我的工作使用cron每5分钟运行一次。 但有时我的工作无限期地等待外部资源,我不能把超时放在那里。 我不能使用fixedDelay作为先前的进程有时进入等待无限模式,我必须每隔5分钟刷新一次数据。 因此,我在Spring Framework的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() ); threadPoolExecutor.allowCoreThreadTimeOut(true); return threadPoolExecutor; }