使用Spring调度程序中来自属性文件的cron值

huangapple go评论88阅读模式
英文:

Using cron value from properties file in spring scheduler

问题

我正在我的Spring Boot应用程序中使用Spring Scheduler。我已经在属性文件中定义了cron表达式:

batch.delete-job.cron=0 0 0 * * *

我在使用@Scheduled注解的方法中使用了这个值,如下所示:

@Scheduled(cron = "${batch.delete-job.cron}")
public void doBatchJob() {

}

我一直收到错误消息:

遇到无效的@Scheduled方法'remindBatchJob':Cron表达式必须由6个字段组成(在"${batch.delete-job.cron}"中找到1个)

如果我直接在注解中使用cron表达式,则可以正常工作。我做错了什么?

英文:

I am using the Spring Schedular in my Spring Boot Application. I have defined the cron expression a properties file:

batch.delete-job.cron=0 0 0 * * *

I am using this value in my method annoated with the @Scheduled Annotation as follows:

@Scheduled(cron = "${batch.delete-job.cron}")
public void doBatchJob() {

}

I keep on getting the error messsage:

Encountered invalid @Scheduled method 'doBatchJob': Cron expression must consist of 6 fields (found 1 in "${batch.delete-job.cron}")

If I use the cron expression directly in annotation, then it works. What am I doing wrong?

答案1

得分: 1

将以下内容添加到您的应用程序上下文中:

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

如果这不起作用,请尝试在两个位置(您的代码和应用程序属性文件中)删除连字符,并用点号分隔。例如:${batch.delete.job.cron}

英文:

Add this to your Application Context-

public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
       return new PropertySourcesPlaceholderConfigurer();
    }

If this dosent work try removing the hyphen in both the places(Your code and the application properties and seperate by .)
For Eg-${batch.delete.job.cron}

答案2

得分: 0

从属性名称中移除连字符没有产生任何区别,但我还是这样做了。解决问题的方法是在包含doBatchJob()方法的类上放置以下注解,引用了属性文件:

@PropertySource("classpath:/config.properties")
@EnableScheduling
@Component
public class DeleteBatch {

  @Scheduled(cron = "${batch.delete.job.cron}")
  public void doBatchJob() {

  }
}
英文:

Removing the hypen from the property name made no difference,but I did it anyway. What solved the problem was to put the following annotation referencing the properties file on my class containing the doBatchJob() method. The working solution looks as folllows:

@PropertySource("classpath:/config.properties")
@EnableScheduling
@Component
public class DeleteBatch {

  @Scheduled(cron = "${batch.delete.job.cron}")
  public void doBatchJob() {

  }
}

答案3

得分: 0

CRON是一个广泛使用的功能,但其中一个缺点是可读性差。如果非程序员需要设置定义调度的属性,这将是一项具有挑战性的任务。想象一下,如果你可以编写类似这样的内容:@Scheduled(fixedRate = 9h),以便为每9小时安排一次作业(或将"9h"移入属性文件中)。嗯,我编写了自己的功能,允许您做到这一点。在这里查看解释这里。这个功能在MgntUtils开源库中可用。该库可以作为Maven构件或从Git获得。使用这个功能需要编写比仅使用注释多一点的代码,但提供了用户友好的格式。因此,这是一个权衡的结果。

英文:

CRON is a widely used feature, but one of its disadvantages is poor readability. If a non-programmer needs to set a property that defines the scheduling it would be a challenging task. Imagine if you could write something like this: @Scheduled(fixedRate = 9h) for scheduling the job for every 9 hours (or moving "9h" into properties file). Well, I wrote my own feature that allows you to do just that. See the explanation here. This feature is available in MgntUtis open source library. The library is available as Maven artifact or from Git. Using this feature requires writing a bit more code then just using annotation, but provides a user-friendly format. So it is a trade-ff

答案4

得分: 0

对于那些使用

> application.yml

首先在 application.yml 文件中设置 cron 属性。

scheduler:
   cron: '0 0 0 * * *'
  • 注意:将 cron 表达式的值设置在单引号内。

然后可以将其作为 cron 的值用于 Scheduled 参数。

例如:

替代:

@Scheduled(cron = “0 0 0 * * *”)
public void scheduledJob() {
   System.out.println("已执行定时作业")
}

您可以使用:

@Scheduled(cron = “${scheduler.cron}”)
public void scheduledJob() {
   System.out.println("已执行定时作业")
}
英文:

For those of you, using

> application.yml

First set the cron property into application.yml file.

scheduler:
   cron: '0 0 0 * * *'
  • Note: Set the value of the cron expression within the single quote.

Then it can be used with as value for cron to the Scheduled argument.

For example:

Instead of:

@Scheduled(cron = “0 0 0 * * *”)
public void scheduledJob() {
   System.out.println("Scheduled job has been executed")
}

You can use:

@Scheduled(cron = “${scheduler.cron}”)
public void scheduledJob() {
   System.out.println("Scheduled job has been executed")
}

huangapple
  • 本文由 发表于 2020年8月25日 02:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63566709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定