aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB Single Timer Bean Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB Single Timer Bean Implementation" Watch "EJB Single Timer Bean Implementation" New topic
Author

EJB Single Timer Bean Implementation

Yogi Mehta
Greenhorn

Joined: Jun 24, 2009
Posts: 5
Hi All, I wanna achieve the following functionality, please advice me how can I implementy it using EJB Timer Services:

I m sending a reauest from my application to some other application. If that request is taking more time than specified. Then that request should be cancelled.
avani vakharwala
Greenhorn

Joined: Jan 24, 2008
Posts: 6

Client code :

package client;

import one.Class;
import one.ClassHome;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class client {

public static void main (String[] args) {
InitialContext ctx;
try {
ctx = new InitialContext();
System.out.println(ctx);

ClassHome home=(ClassHome)ctx.lookup("SomeBean");
ClassTimer timer=home.create();
timer.calltimer(1000,3000, "nameoftimer");



}
catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


TimerCode -CallTimerBeancode

package one;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.ejb.*;

import javax.naming.*;
import javax.sql.*;


public class ClassTimerBean implements SessionBean,TimedObject,Serializable {
private static final long serialVersionUID = 1L;
SessionContext sessionContext;

String timerstatus=null;
public boolean checkstatus() throws RemoteException
{
boolean timerflag=false;

try {
TimerService timerService = sessionContext.getTimerService();
for (Object obj:timerService.getTimers())
{
Timer mytimer=(Timer)obj;
timerstatus=(mytimer.getInfo().toString());
if(timerstatus.equalsIgnoreCase("nameoftimer"))
{
timerflag=true;
}
}
} catch (Exception e) {
timerflag=false;
}

return messagetimerflag;
}
public void cancelTimer() throws RemoteException
{


TimerService timerService = sessionContext.getTimerService();
/**
* Identifying timer and canceling the timer
* */
for (Object obj:timerService.getTimers())
{

Timer mytimer=(Timer)obj;
try{
if(messagetimer.getInfo().equals("nameoftimer"));
{
mytimer.cancel();
}
}catch(Exception e){}



}




}
public void calltimer(long initial, long interval, Serializable s) throws RemoteException
{

TimerService timerService = sessionContext.getTimerService();

Timer timer = timerService.createTimer(initial, interval,s);

}
public void ejbCreate()throws EJBException,RemoteException
{
}
public void ejbActivate() throws EJBException,RemoteException {

}

public void ejbPassivate() throws EJBException ,RemoteException{

}

public void ejbRemove() throws EJBException,RemoteException {



}

public void setSessionContext(SessionContext arg0) throws EJBException,RemoteException
{
sessionContext=arg0;
// TODO Auto-generated method stub

}

public void ejbTimeout(Timer arg0) {

// business logic to cancel request

}

}


Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10051
    
163

"Java Yogi"

Please click on the "My Private Messages" link on top of this page, for a message from JavaRanch.


[My Blog] [JavaRanch Journal]
Yogi Mehta
Greenhorn

Joined: Jun 24, 2009
Posts: 5
Thanks for your help.. But I need something else...

The follwing operations in my code:

1) We are doing some process with DB in service method of EJB Timer bean.
2) We created a timer for 30 sec.
3) Suppose my First Process is taking 40 Sec
4) After 30 Sec timeOut() method will be called. Here whatever is wrtten get executed and then again control goes back to Service method and start execution from where it left.

But I don't want to go back in Service method again....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJB Single Timer Bean Implementation