首页 > 解决方案 > EJB @Schedule 注解设置动态值

问题描述

我在方法中添加了一个 @Schedule 注释。

@Schedule(minute = "1", hour = "*", info="Every 1 minute")
public void getStandingOrdersTimer() {
        
}

在这里,该方法将每小时每分钟运行一次。我想使用变量传递分钟数。无需在注释中对其进行硬编码。我怎样才能做到这一点?

标签: jakarta-eeejbschedule

解决方案


您需要使用 TimerService 创建一个 Timer。

@Singleton
@Startup
@LocalBean
public class TimerBean {

    @Resource
    TimerService timerService;

    @PostConstruct
    public void init(){
        timerService.createIntervalTimer(new Date(), getMinute() * 60000
                        new TimerConfig("MyTimer", false));
    }

    private int getMinute(){
        //here your code to get the minute value from somewhere
    }

    @Timeout
    public void getStandingOrdersTimer() {
        
    }
}

推荐阅读