jakarta-ee - EJB @Schedule 注解设置动态值
问题描述
我在方法中添加了一个 @Schedule 注释。
@Schedule(minute = "1", hour = "*", info="Every 1 minute")
public void getStandingOrdersTimer() {
}
在这里,该方法将每小时每分钟运行一次。我想使用变量传递分钟数。无需在注释中对其进行硬编码。我怎样才能做到这一点?
解决方案
您需要使用 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() {
}
}
推荐阅读
- java - PDFBox 返回丢失的后代字体字典
- python - 如何使用 statsmodels GLM fit_constrained 将输出约束为正
- javascript - 对话流如何从谷歌日历播放下一次可用的时间
- scala - java中Set的scala等价物是什么?
- c# - 使用模板动态生成代码
- sql-server - SQL DELETE 两次或 DELETE 与 OR 哪个性能更高?
- r - 带有列组的 pivot_longer
- c# - 如何使用 mstest 或 nunit 将 DataTable 用作我的测试用例的数据源?
- svn - 从 SVN 中的合并提交中恢复单个修订
- html - 很好地格式化输出并以 HTML 格式通过电子邮件发送