Spring @Scheduled注释 [英] Spring @Scheduled annotation
本文介绍了Spring @Scheduled注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以动态方式使用@Scheduled注释?
How can I use @Scheduled annotation of spring dynamically?
CronTrigger(String expression, TimeZone timeZone)
在数据库中有多个时区,我如何动态传递它们?
As I have multiple timeZones in database, how can I pass them dynamically?
我在我的代码中尝试过:
I tried this in my code:
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*
请帮助我。因为我想根据时区触发 cron 。 TIA。
Please help me. Because I want to trigger cron based on timezones. TIA.
推荐答案
注释参数不能动态设置。你可以通过编程方式,像这样
Annotation parameters cannot be set dynamically. You can do it programmatically, like this
class Scheduler implements Runnable {
public Scheduler(TaskScheduler scheduler, String timezone, String cron) {
scheduler.schedule(this, new CronTrigger(cron, TimeZone.getTimeZone(timezone)));
}
@Override
public void run() {
//
}
}
这篇关于Spring @Scheduled注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文