aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes controlling ejbTimeout EJB 2.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "controlling ejbTimeout EJB 2.1" Watch "controlling ejbTimeout EJB 2.1" New topic
Author

controlling ejbTimeout EJB 2.1

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 382
    
    2
Hi,

I have written a Statelss session bean implementing TimedObject (EJB 2.1). In this bean the ejbTimeout method is called by the container automatically.

Can I have control on ejbTimeout method ? Can I execute it manually depending on some condition instead of leaving it to the container to invoke automatically ?

Below is the code
======================================================

public class MyTimerBean implements javax.ejb.SessionBean,TimedObject {
private SessionContext mySessionCtx;
private TimerHandle timerHandle = null;

/**
* getSessionContext
*/
public SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* ejbCreate
*/
public void ejbCreate() throws CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}


public void initializeTimer(Date firstDate,long timeout,String timerName)
{
try {
// Create Your Timer
TimerService ts = mySessionCtx.getTimerService();
Timer timer =
ts.createTimer(firstDate, timeout, timerName);
System.out.println("Timer created at " + new Date(System.currentTimeMillis()) +" with a timeout: " + timeout +
" and with info: " + timerName);

// Store the TimerHandle, which is seriablizable
// and which can be used
// to retrieve the timer values whenever required later.
// Class-level attribute:
timerHandle = timer.getHandle();


} catch (Exception e) {

System.out.println("Exception after create timer : "+
e.toString());

}
return;

}
public void ejbTimeout(Timer timer)
{
//Implement Your Business Logic Here
System.out.println("Hello World from: ");

System.out.println("ejbTimeout() called at: " +
new Date(System.currentTimeMillis()) +
" with info:");
System.out.println("Timer Info from ejbTimeout: "+timer.getInfo());

return;
}

public void cancelTimer(String timerName)
{
try
{
TimerService ts = mySessionCtx.getTimerService();
Collection timers = ts.getTimers();
Iterator it = timers.iterator();
while (it.hasNext())
{
Timer myTimer = (Timer) it.next();
if ((myTimer.getInfo().equals(timerName))) {
myTimer.cancel();
System.out.println("Successfully Cancelled " +
timerName);

}
}
}
catch (Exception e) {

System.out.println("Exception after cancelling timer : "+
e.toString());

}
return;
}

public void getTimerInfo()
{
if (timerHandle != null) {
Timer timer = timerHandle.getTimer();
// Get Timer Infomation
System.out.println("Timer Info : " +timer.getInfo());


}

}

}


Thanks and Regards
Ayub


SCEA part I,TOGAF Foundation
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Within the bean, probably yes (looks like any other method, and in the worst case, you can factor the logic into another method); from a client, no (a client can only invoke methods defined in the bean's local and/or remote interface).
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 382
    
    2
Hi Edvins,

Thank you for giving your thoughts. I will try some work around here.

Best Regards
Ayub
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: controlling ejbTimeout EJB 2.1