Probably a silly question for someone with ejb experience...
I want to read and change the minute parameter dynamically for one of my EJB beans that uses the Java EE scheduler via the @Schedule
annotation. Anyone know how to do this at runtime as opposed to hardcoding it in the class like below? If i were to do it programatically could i still use the @Schedule
annotation?
@Schedule(dayOfWeek = "0-5", hour = "0/2", minute = "0/20", timezone = "America/Los_Angeles")
private void checkInventory() {
}
@Schedule
is for automatic timers created by the container during deployment.
On the other hand, you can use TimerService
which allows you to define at runtime when the @Timeout
method should be called.
This might be interesting material for your: The Java EE 6 Tutorial - Using the Timer Service.
EDIT: Just to make this answer more complete. If it's a matter of is it possible than the answer would be - yes, it is.
There is a way to "change the parameters" of automatic timer created with @Schedule
. Nevertheless it's quite extraordinary - it cancels the automatic timer and creates a similar programmatic one:
// Automatic timer - run every 5 seconds
// It's a automatic (@Schedule) and programmatic (@Timeout) timer timeout method
// at the same time (which is UNUSUAL)
@Schedule(hour = "*", minute = "*", second = "*/5")
@Timeout
public void myScheduler(Timer timer) {
// This might be checked basing on i.e. timer.getInfo().
firstSchedule = ...
if (!firstSchedule) {
// ordinary code for the scheduler
} else {
// Get actual schedule expression.
// It's the first invocation, so it's equal to the one in @Schedule(...)
ScheduleExpression expr = timer.getSchedule();
// Your new expression from now on
expr.second("*/7");
// timers is TimerService object injected using @Resource TimerService.
// Create new timer based on modified expression.
timers.createCalendarTimer(expr);
// Cancel the timer created with @Schedule annotation.
timer.cancel();
}
}
Once again - personally, I would never use such code and would never want to see anything like this in a real project :-) Either the timer is:
@Schedule
by hard-coding the values or by defining them in ejb-jar.xml,If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With