質問

I used EJB Timer task to perform my periodic service query to database and send the update to client within specific period. This period is defined by client.
I pass the client request to the ejb timer stateless session bean. The request is stored as a instance variable.

When I call timer task through JNDI I set the client request to this instance variable and its available before timerService.createTimer() call. But when the @Timeout happen the instance variable is null.

I need this client request to perform DB query to find request parameters.

@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;


 }

I'm new to EJB. Some thing wrong the way I do it? or how can I keep the request variable available until I cancel/stop the timer

役に立ちましたか?

解決

You can try below code, no need to create a separate instance variable.

Provide the object that you would like to receive in timeout method while creating timer.

timerService.createTimer(firstDate, request); 

Then, fetch the object in timeout method which was passed at timer creation.

(IVEFRequest)arg0.getInfo();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top