0

每次我尝试在@Timeout 打开文件时,Java 都会返回空指针异常

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

有任何想法吗?

4

1 回答 1

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();
    ...
  }
}
于 2013-06-07T14:57:17.513 回答