4

如何动态使用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*

请帮我。因为我想根据timezones触发cron。TIA。

4

1 回答 1

5

注解参数不能动态设置。您可以像这样以编程方式执行此操作

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() {
        //
    }
}
于 2015-07-27T11:36:22.617 回答