• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJB Single Timer Bean Implementation

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
Hibernate Eclipse IDE Postgres Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

}

}


 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
reply
    Bookmark Topic Watch Topic
  • New Topic