File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes EJB Timer issue 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 Timer issue" Watch "EJB Timer issue" New topic
Author

EJB Timer issue

Ayub ali khan
Ranch Hand

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

I implemented a sample EJB timer stateless session bean.

in the ejbTimeout method I am printing hello world message. No one is looking up the bean, however the hello world message is printed at regular intervals. Please clarify how this is happening :
Below is the code:
============================================

package ejbs;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import javax.ejb.TimerHandle;
import javax.ejb.TimerService;

/**
* Bean implementation class for Enterprise Bean: MyTimer
*/
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 My Timer");

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());


}

}
public Date getCustomTimerInfo(){
Timer timer=timerHandle.getTimer();

return timer.getNextTimeout();
}
}
Thanks and Regards
Ayub


SCEA part I,TOGAF Foundation
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31077
    
233

Ayub,
The initializeTimer method is setting up the timer to run regularly. You don't need a person to call the bean for this to happen because the container takes care of calling the bean at the requested intervals.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 383
    
    2
Hi Jeanne,

I was following the example given inOn Java article. Here I had to look up the bean from the servlet which will be triggered when I start the application.

Now I understand that there is no need to look up the bean from the servlet.

The content in the above article is not correct !!

Thank you
Debu Panda
author
Ranch Hand

Joined: Jan 21, 2007
Posts: 100
Ayub,
I was the author of the article (: And indeed a timer has to be created by invoking a method as you have initializeTimer method. Note that automatic submission of timer will be introduced in EJB 3.1.

You are submitting an interval timer so it will run for ever until you cancel
Look at:

ts.createTimer(firstDate,<b>timeout</b>, timerName);

If you want your time out to occur at a single interval you just specify

ts.createTimer(firstDate, timerName);

Hope that helps!

-Debu


Author: EJB 3 In Action (http://manning.com/panda)
nandkishor rao
Ranch Hand

Joined: May 24, 2006
Posts: 53
Please clarify

I am using ts.createTimer(timerValue, timerName);
where timerValue is in long. For this one also it is calling ejbTimeout at regular intervals, I don't know way.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10289
    
168

I am using ts.createTimer(timerValue, timerName);


How many times is this ts.createTimer code getting called?


[My Blog] [JavaRanch Journal]
nandkishor rao
Ranch Hand

Joined: May 24, 2006
Posts: 53
Please clarify

I am using ts.createTimer(timerValue, timerName);
where timerValue is in long. For this one also it is calling ejbTimeout at regular intervals, I don't know way.
nandkishor rao
Ranch Hand

Joined: May 24, 2006
Posts: 53
only once.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10289
    
168

Nandkishor, lets continue this discussion in your other thread
[ April 19, 2007: Message edited by: Jaikiran Pai ]
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 383
    
    2
Hi Debu,

Thank you for clarification. When I deployed the EJB timer bean,it was being invoked automatically. I mean the ejbTimeout method was being called.

I will do a clean deploy without invoking the bean manually and check.

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