1

Is it possible to get the date/time that the @Schedule expired?

If there is a delay in running the @Schedule, then the actual expired time is in the past. e.g. A persistant timer will run missed @Schedule when the server is restarted after a shutdown.

Example Job I tried.

import java.util.Date;

import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timer;

@Stateless
public class SimpleJob {
  @Schedule(hour="*", minute = "*/1", second = "0", persistent = true) // Every 1 minute
  public void schedule(Timer timer) {
    System.out.println("Now: " + new Date());
    System.out.println("\tTime remaining: "+ timer.getTimeRemaining());
    System.out.println("\tNext Timeout: "+timer.getNextTimeout());
    System.out.println("\tInfo: "+timer.getInfo());
  }
}

When I restart the server after a shutdown I have missed the 10:34:00 and 10:35:00 @Schedule

Now: Wed Feb 04 10:35:05 EST 2015
    Time remaining: 54235
    Next Timeout: Wed Feb 04 10:36:00 EST 2015
Now: Wed Feb 04 10:35:05 EST 2015
    Time remaining: 54233
    Next Timeout: Wed Feb 04 10:36:00 EST 2015
    Info: null

It correctly runs the missed @Schedule, but I cannot tell when each one was meant to run (which is important to my code).

Iain
  • 582
  • 5
  • 12
  • Which application server are you using? It seems unusual to me that both timer expirations report the same 'Next Timeout'. I would have expected the first to report 10:35 and the second to report 10:36. – Brett Kail Feb 05 '15 at 14:40
  • I'm using JBOSS 7.0.40 – Iain Feb 08 '15 at 21:56
  • possible duplicate of [Java EE 7: Get @Schedule date when is set to persistent = true](http://stackoverflow.com/questions/25929097/java-ee-7-get-schedule-date-when-is-set-to-persistent-true) – Iain Feb 08 '15 at 22:08

2 Answers2

1

Unfortunately, there is no way to tell what time the current timer callback was scheduled for, it is only possible to use Timer.getNextTimeout() to determine the next scheduled time. It is surprising to me that getNextTimeout is reporting 10:36 for both in your example. I wonder if that is a defect?

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • I've realised this is a duplicate question. http://stackoverflow.com/questions/25929097/java-ee-7-get-schedule-date-when-is-set-to-persistent-true – Iain Feb 08 '15 at 22:09
0

You can get the ScheduleExpression from Timer. Now from this expression, you can get the date/time details.

From documentation:

timer.getSchedule(): Get the schedule expression corresponding to this timer. The timer must be a calendar-based timer. It may have been created automatically or programmatically.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • @Iain I haven't tried it. Once you get schedule expression from the timer, then from it you can get hour, minute etc. methods are available, have you tried that. – Nayan Wadekar Feb 09 '15 at 08:55
  • I don't think you are understanding the question. – Iain Feb 09 '15 at 09:50