Java guy
Java guy

Reputation: 33

Non-persistence timerService injection fail on IBM Liberty Profile 18.0.0.1

I tried to use EJB programmatic timers with IBM Liberty Profile 18.0.0.1. Here is my service.xml:

<feature>ejbLite-3.2</feature>
......
<ejbContainer>
 <timerService nonPersistentMaxRetries="3" nonPersistentRetryInterval="10" />
</ejbContainer>

And here is my bare bone code snippet.

@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;
}

}

This is how my client invokes the timer:

BatchSubmissionTimer batchSubmissionTimer = new BatchSubmissionTimer();
batchSubmissionTimer.setTimer(5000);

However, I got a non-pointer error on injected TimerService. The TimerService wasn't injected successfully. Can anybody shed some lights on this? Appreciate it!

Upvotes: 0

Views: 164

Answers (1)

njr
njr

Reputation: 3484

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;

Upvotes: 2

Related Questions