Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a service with EJB

Jose Hidalgo
Posts: 6
  • Mark post as helpful
  • send pies
  • 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 ?

Tim Holloway
Saloon Keeper
Posts: 18325
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
Claude Moore
Ranch Hand
Posts: 826
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic