Nihhaar
Nihhaar

Reputation: 199

Timer.equals() not working in TomEE

I am developing a webapp (.war file), but now I need to implement timerservice, so I am using ejb's timer service. I am storing a object with on of the variables being timerhandle of the timer created, in a local file. There will be many objects appended to same file as you create new timers. When the timeout occurs, I open the local file and finding the write object using equals method:

public void programmaticTimeout(Timer timer) {

try(FileInputStream fin = new FileInputStream(db);
            ObjectInputStream ois = new ObjectInputStream(fin);){
            while((temp = (Rules)ois.readObject()) != null){
                System.out.println(temp.getName());
                if(timer.equals(temp.getTimerHandle().getTimer())){
                    System.out.println("Time handler found!");
                    break;
                }
            }
        } catch(Exception e){
        }
}

But this is resulting in always choosing the first object. Can anyone explain how to resolve this?

EDIT : As suggested by @Romain Manni-Bucau, I am using getInfo() method as follows:

public TimerHandle setTimer(ScheduleExpression schedule, Rules rule) {
    Timer timer = timerService.createCalendarTimer(schedule, new TimerConfig(rule, true));
    return timer.getHandle();
}

@Timeout
public void programmaticTimeout(Timer timer) {
    System.out.println("Programmatic timeout fired!");
    this.setLastProgrammaticTimeout(new Date());
    Rules temp = (Rules)timer.getInfo();
        if(temp.getType().equals("campaigns"))
            try {
                handleCampaigns(temp);
            } catch (Exception e) {
                e.printStackTrace();
            }

}

Now, I've implemented web page (and corresponding servlet) for setting new rules by choosing time and frequency of the event. As new rules are created the servlet makes call to:

setTimer(schedule,newrule);

Now, if I created multiple rules (multiple timers with corresponding events), only the first timer is getting fired but with the event of the last timer. I don't know why is this happening. Am I doing wrong? I've switched from TomCat to TomEE for the timeservice. Should I add any dependencies to pom.xml for this shift?

Screenshot: https://postimg.org/image/9jde59nep/

Thank you.

Upvotes: 0

Views: 141

Answers (1)

Romain Manni-Bucau
Romain Manni-Bucau

Reputation: 3422

Why don't you use getInfo() to match the right timer?

Upvotes: 0

Related Questions