java Spring @Scheduled 注解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31651196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring @Scheduled annotation
提问by Chandz
How can I use @Scheduled annotation of spring dynamically?
如何动态使用 spring 的 @Scheduled 注释?
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
}
Here is the errorI am getting,
这是我得到的错误,
*Type mismatch: cannot convert from TimeZone to String*
Please help me. Because I want to trigger cronbased on timezones. TIA.
请帮我。因为我想根据timezones触发cron。TIA。
回答by Evgeniy Dorofeev
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() {
//
}
}