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*
请帮帮我。 因为我想根据时区触发cron 。 TIA。
注释参数不能动态设置。 您可以通过编程方式执行此操作
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() { // } }