Every time I try to open a file at the @Timeout, Java returns Null Pointer Exception
@Singleton
public class EngineTrans {
@Resource
private TimerService timerService;
public void createProgrammaticalTimer() {
ScheduleExpression everyTenSeconds = new ScheduleExpression().second("*/15").minute("*").hour("4-20");
timerService.createCalendarTimer(everyTenSeconds, new TimerConfig(
"passed message " + new Date(), false));
}
@Timeout
public void handleTimer() {
System.out.println("timer received - contained message is: " + new Date());
File xmlFile = new File(FacesContext.getCurrentInstance().getExternalContext().getRealPath(""));
}
}
Any ideas?
There is no JSF context within an @Timeout method. Perform the getRealPath call in the createProgrammaticalTimer method, and then pass it to the @Timeout method via the first parameter of the TimerConfig constructor (the "info" parameter). If necessary, create an inner class to hold all the data you need to pass to the @Timeout method:
@Singleton
public class EngineTrans {
@Resource
private TimerService timerService;
private static class TimeoutData {
private final Date date = new Date();
private final String resourcePath;
...
}
public void createProgrammaticalTimer() {
...
String resourcePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("...");
TimeoutData timeoutData = new TimeoutData(resourcePath);
timerService.createCalendarTimer(everyTenSeconds, new TimerConfig(timeoutData, false));
}
@Timeout
public void handleTimer(Timer timer) {
TimeoutData timeoutData = (TimeoutData)timer.getInfo();
...
}
}