Hi
I'd like to start a calendar scheduled job for 1 minute from now.
I was trying to assemble a cron expression like this, but instead, it's
generating
a lot of jobs (really)
looks like something really stupid
The code is as simple as this
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.ScheduleExpression;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
@Singleton
@Startup
public class TimerEJB {
@Resource
private TimerService timerService;
@Timeout
public void executeTimer(Timer timer) {
System.out.println(timer);
}
private String toString(ScheduleExpression schedule) {
return "MyScheduleExpression [getDayOfMonth()=" +
schedule.getDayOfMonth()
+ ", getDayOfWeek()=" + schedule.getDayOfWeek()
+ ", getEnd()=" + schedule.getEnd()
+ ", getHour()=" + schedule.getHour()
+ ", getMinute()=" + schedule.getMinute()
+ ", getMonth()=" + schedule.getMonth()
+ ", getSecond()=" + schedule.getSecond()
+ ", getStart()=" + schedule.getStart()
+ ", getYear()=" + schedule.getYear()
+ ", getTimezone()=" + schedule.getTimezone() + "]";
}
@PostConstruct
public void create() {
Calendar future = new GregorianCalendar();
future.setTimeInMillis(System.currentTimeMillis() + 60000);
int dayOfWeek = future.get(Calendar.DAY_OF_WEEK);
String dayOfWeekName = null;
switch (dayOfWeek) {
case Calendar.MONDAY:
dayOfWeekName = "Mon";
break;
case Calendar.TUESDAY:
dayOfWeekName = "Tue";
break;
case Calendar.WEDNESDAY:
dayOfWeekName = "Wed";
break;
case Calendar.THURSDAY:
dayOfWeekName = "Thu";
break;
case Calendar.FRIDAY:
dayOfWeekName = "Fri";
break;
case Calendar.SATURDAY:
dayOfWeekName = "Sat";
break;
case Calendar.SUNDAY:
dayOfWeekName = "Sun";
break;
}
String cronExp = String.format("%1$s %2$s %3$s %4$s %5$s %6$s %7$s",
future.get(Calendar.SECOND),
future.get(Calendar.MINUTE),
future.get(Calendar.HOUR_OF_DAY),
dayOfWeekName,
future.get(Calendar.DAY_OF_MONTH),
future.get(Calendar.MONTH) + 1,
future.get(Calendar.YEAR));
String[] cron = cronExp.split(" ");
String[] ss = new String[7];
for (int i = 0; i < cron.length; i++) {
ss[i] = cron[i];
}
ScheduleExpression exp = new ScheduleExpression();
exp.second(0);
exp.minute(ss[1]);
exp.hour(ss[2]);
exp.dayOfWeek(ss[3]);
exp.dayOfMonth(ss[4]);
exp.month(ss[5]);
exp.year(ss[6]);
System.out.println(toString(exp));
Timer t = this.timerService.createCalendarTimer(exp);
System.out.println("Created "+t);
}
}
output for this looks like this
MyScheduleExpression [getDayOfMonth()=28, getDayOfWeek()=Sat,
getEnd()=null, getHour()=16, getMinute()=53, getMonth()=12, getSecond()=0,
getStart()=null, getYear()=2013, getTimezone()=null]
Created org.apache.openejb.core.timer.TimerImpl@298a0c11
an after some seconds
org.apache.openejb.core.timer.TimerImpl@5f1e961a
org.apache.openejb.core.timer.TimerImpl@5f1e961a
org.apache.openejb.core.timer.TimerImpl@5f1e961a
org.apache.openejb.core.timer.TimerImpl@5f1e961a
org.apache.openejb.core.timer.TimerImpl@5f1e961a
org.apache.openejb.core.timer.TimerImpl@5f1e961a
org.apache.openejb.core.timer.TimerImpl@5f1e961a...
any help is welcome
TIA
Leo