将参数设置为ejb计时器任务以执行后端处理

时间:2014-02-04 09:45:01

标签: java ejb-3.1 timertask

我使用EJB Timer任务对database执行定期服务查询,并在特定时间段内将更新发送给客户端。此期间由客户定义。
我将客户request传递给ejb timer stateless session beanrequest存储为instance variable

当我通过JNDI调用计时器任务时,我将客户端请求设置为此instance variable,并在timerService.createTimer()调用之前将其设置为可用。但是当@Timeout发生时,instance variablenull

我需要此客户端请求来执行数据库查询以查找请求参数。

@Stateless
public class PeriodicService implements TimerRemote{

   @Override
   public void initializeTimer(Date firstDate, long timeout, String info) throws RemoteException {
    try {   
             // below line show the request is not null.
        System.out.println("request in initializeTimer "+this.request);

       // create onetime service now.
        timerService.createTimer(firstDate, info); 
        log.info("Timer created at " +  firstDate + " with info: " + info);

    } catch (Exception e) {

        log.fatal("Exception after create timer : "+ e.toString()); 
        e.printStackTrace();
    }

  }

      @Timeout
  public void ejbTimeout(Timer arg0) {
    // below line show the requst as null
    log.debug("Request in ejbTimeout "+this.request);

  }

    public void setRequest(IVEFRequest request) {
       this.request = request;
   }


   private      IVEFRequest     request         = null;


 }

我是EJB的新手。我这样做有些不对劲吗?或者我如何在cancel/stop timer

之前保持请求变量可用

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码,无需创建单独的实例变量。

在创建计时器时提供您希望在超时方法中接收的对象。

timerService.createTimer(firstDate, request); 

然后,在定时器创建时传递的timeout方法中获取对象。

(IVEFRequest)arg0.getInfo();
相关问题