Spring @Scheduled 注解 [英] Spring @Scheduled annotation

查看:39
本文介绍了Spring @Scheduled 注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态使用spring的@Scheduled注解?

How can I use @Scheduled annotation of spring dynamically?

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-

由于数据库中有多个时区,如何动态传递它们?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆