• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Himai Minh

Creating a service with EJB

Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I need to create a background service in EJB 3.1 that pulls a list of tasks to run from the DB and creates the distributed threads to run them.
I need the service to have global state, so it keeps a reference to the tasks (static ArrayList) that are currently running so it doesn't run them more than once

Initially I though I could use

public class MyService {

static ArrayList<Task> tasksList;

@Lock(LockType.WRITE) // write cause I am modifing static fields
@Schedule(second="0,15,30,45", minute="*", hour="*")
public void execute(Timer timer) {
Task newTask = new Task();

public class Task extends Thread {

But with this approach the thread won't be distributed, and also singleton is getting concurrent calls that eventually timeout when the cycle takes longer than 15 seconds.

So, how can I:
1) make use of a TimerService across the cluster that won't be re-entrant, or make it exit it another cycle is still running ?
2) how can I distribute the threads across the cluster ?

Saloon Keeper
Posts: 23729
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't read the EJB3 specs in a while, but originally at least, EJBs were explicitly forbidden from spawning threads.

What you would typically do instead is use the EJB to hand over the thread request to a separate and independent process using some sort of mechanism such as JMS. Or Message EJBs, which are not the same sort of thing as Session and Entity EJBs.

If you want to queue up work to be performed by a third-party process on a regular schedule, one possible solution would be to use the Quartz scheduler.
Posts: 1309
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim's right, even if your appserver would allow you to spawn threads, in a Java enterprise context is always a bad practice to avoid, at least if you do really know what you are doing. And I would anyway avoid spawning threads if they're going to use db connections or other resources.
You may try to lookup a servant ejb which in turn exposes a method accepting a CustomTask in Ejb timer execute method. This way you avoid spawning threads.
My doubt is related to what actually happens in a clustered environment to internal EJB lookups. I mean, if an EJB A looks up an Ejb B in a domain of two or more appserver, is it possible that Ejb B isn't colocated to Ejb A? If it is not possible, and only client (external) requests are handled in a clustered manner, you'd better rely upon an external program based on quartz - as Tim suggested - which triggers requests at specified time.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic