Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB Single Timer Bean Implementation

 
Yogi Mehta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Eclipse IDE Hibernate Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Java Yogi"

Please click on the "My Private Messages" link on top of this page, for a message from JavaRanch.
 
Yogi Mehta
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic