This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ejb as abstract ?

 
Ranch Hand
Posts: 399
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Can I declare an ejb as abstract ?

Also I am trying to create a utility which will allow to create multiple timers and schedule their programs.

Can this be achieved using EJB Timer ?
will making ejbTimeout as abstract, allow another ejb to implement the functionality of ejbTimeout and put their scheduling logic there ?


Here is the code for EJB Timer
============================================

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;
private String name;
private int repeatInterval;

/**
* 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);
this.name=timerName;
repeatInterval=new Long(timeout).intValue();
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: "+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();
System.out.println("Returning custom time from bean ");
return timer.getNextTimeout();
}
public String getName(){
System.out.println("REturning name from Time bean "+name);
return name;
}
public int getRepeatInterval(){
System.out.println("REturning repeat Interval from Time bean : "+repeatInterval);
return repeatInterval;
}
}

Thanks and Regards
Ayub.
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ayub ali khan:
Can I declare an ejb as abstract ?


Yes. Just be sure to included the concrete classes (rather than the abstract superclass) is your deployment descriptor. Then the superclass is just a class, not an EJB.
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:

Yes. Just be sure to included the concrete classes (rather than the abstract superclass) is your deployment descriptor. Then the superclass is just a class, not an EJB.



You do know, that you changed your answer from yes to no in just two sentences.

Yes, because you can declare the class as abstract, but at that point it ceases to be an EJB, so then no an EJB cannot be abstract.

Mark
 
Ayub ali khan
Ranch Hand
Posts: 399
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeanne and Mark.

The two different views to this question has answered my question.

Best Regards
Ayub
 
Jeanne Boyarsky
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
You do know, that you changed your answer from yes to no in just two sentences.

Yes, because you can declare the class as abstract, but at that point it ceases to be an EJB, so then no an EJB cannot be abstract.


I hadn't realized that at the time or I would have started with "no." I think I arrived at the answer as I was typing
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic