I know that the use of instance variables within stateless session beans is a popular discussion topic and have already read some of them, but what i am specifically need from this topic, is the actual proper design of my application.
My enterprise application, consists of several stateless beans, which fire on specific events. On such events, i want a couple timers to fire as well and keep track of certain things (e.g. new data inserted in the database, specific to the original event).
@Stateless
public class SpecificFeedbackImpl implements SpecificFeedback {
@Resource
protected TimerService timerService;
//more injections here
public String name;
public String ip;
@Timeout
public void timeoutHandler(Timer timer) {
if (timer.getInfo().toString().startsWith(name)) {
//search db for data of event with identifier "name"
//if anything found, then send to "ip"
}
}
public void stopTimer() {
for (Object o : this.timerService.getTimers())
if (((Timer) o).getInfo().toString().startsWith(name)){
((Timer)o).cancel();
}
}
@Override
public void startTimer(long interval, String eventID, String serverIP){
this.name = eventID;
this.ip = serverIP;
stopTimer();
TimerConfig config = new TimerConfig();
config.setInfo(name);
config.setPersistent(false);
timerService.createIntervalTimer(interval, interval, config);
}
}
However, when i did a small test of manually calling multiple timers, with different intervals, eventIDs and serverIPs, i did not have the expected behavior from all timers (some of them never timed-out, etc.).
Is the above source appropriate for the required usage, or should i design it/implement it differently? If yes, could you please suggest a few options?