0

我尝试在 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 未成功注入。任何人都可以对此有所了解吗?欣赏它!

4

1 回答 1

2

In your example, you are instantiating your own instance of BatchSubmissionTimer rather than allowing the container to provide it as an EJB, so the container does not have a chance to inject a value for the annotated timerService field. There are several ways to access it as an EJB, including lookup or injecting it, for example,

@EJB
BatchSubmissionTimer batchSubmissionTimer;
于 2018-06-22T20:36:15.167 回答