在timerservice的@Timeout打开一个文件

时间:2013-06-07 13:49:00

标签: glassfish-3 ejb-3.1

每当我尝试在@Timeout上打开文件时,Java都会返回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(""));
}
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

@Timeout方法中没有JSF上下文。在createProgrammaticalTimer方法中执行getRealPath调用,然后通过TimerConfig构造函数的第一个参数(“info”参数)将其传递给@Timeout方法。如有必要,创建一个内部类来保存传递给@Timeout方法所需的所有数据:

@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();
    ...
  }
}