Spring @Scheduled 注解 [英] Spring @Scheduled annotation
本文介绍了Spring @Scheduled 注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何动态使用spring的@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屋!
查看全文