我尝试在 IBM Liberty Profile 18.0.0.1 中使用 EJB 编程计时器。这是我的 service.xml:
<feature>ejbLite-3.2</feature>
......
<ejbContainer>
<timerService nonPersistentMaxRetries="3" nonPersistentRetryInterval="10" />
</ejbContainer>
这是我的基本代码片段。
@Stateless
public class BatchSubmissionTimer {
private static final Logger LOGGER =
Logger.getLogger(BatchSubmissionTimer.class.getName());
@Resource
TimerService timerService;
private Date lastProgrammaticTimeout;
public void setTimer(long intervalDuration) {
LOGGER.info("Setting a programmatic timeout for "
+ intervalDuration + " milliseconds from now.");
Timer timer = timerService.createTimer(intervalDuration,
"Created new programmatic timer");
}
@Timeout
public void programmaticTimeout(Timer timer) {
this.setLastProgrammaticTimeout(new Date());
LOGGER.info("Programmatic timeout occurred.");
}
public String getLastProgrammaticTimeout() {
if (lastProgrammaticTimeout != null) {
return lastProgrammaticTimeout.toString();
} else {
return "never";
}
}
public void setLastProgrammaticTimeout(Date lastTimeout) {
this.lastProgrammaticTimeout = lastTimeout;
}
}
这是我的客户调用计时器的方式:
BatchSubmissionTimer batchSubmissionTimer = new BatchSubmissionTimer();
batchSubmissionTimer.setTimer(5000);
但是,我在注入的 TimerService 上遇到了一个非指针错误。TimerService 未成功注入。任何人都可以对此有所了解吗?欣赏它!