gpt4 book ai didi

java - EJB 3.x 中@Schedule 方法的动态参数

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:14 26 4
gpt4 key购买 nike

我是 J2EE6 中 @Schedule 注释的新手

我想使用 EJB 3.x 和 Glassfish 3.1 运行作业。

javax.ejb.Schedule 对我们来说似乎是一个不错的选择,所以我们可以将自定义时间想象成这样:

@Singleton
public class CustomTimer {
@EJB
SettingsFacade settingsFacade;

@Schedule(second="someSecondParameter", minute="someMinuteParameter",hour="someHourParameter", persistent=false)
public void executeTimer(){
//Code executing something against database using the settingsFacade
}
}

在这里,我们希望从数据库中获取参数,因此每个月都会更改。有什么干净的解决方案吗?

最佳答案

@Singleton@Startuppublic class ScheduleTimerService {    @Resource private TimerService timerService;    public void setTimerService(TimerService timerService) {this.timerService = timerService; }    @PostConstruct    private void postConstruct() {        timerService.createCalendarTimer(createSchedule());    }    @Timeout    public void timerTimeout(Timer timer) {           Add your code here to be called when scheduling is reached...           in this example: 01h:30m every day ;-)    }    private ScheduleExpression createSchedule(){        ScheduleExpression expression = new ScheduleExpression();        expression.dayOfWeek("Sun,Mon,Tue,Wed,Thu,Fri,Sat");            expression.hour("01");        expression.minute("30");        return expression;    }}

关于java - EJB 3.x 中@Schedule 方法的动态参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15973661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com