Forums Register Login

Creating a service with EJB

+Pie Number of slices to send: Send
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

@Singleton
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();
newtask.start();
taskList.add(newTask);


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 ?

Thanks
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 735 times.
Similar Threads
TimerService getTimeRemaining negative !?
EJB Timer Services Query
How to stop a Timer after it's run more than once?
Use a singleton for javax.xml.ws.Service instance
Scheduler from database
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:54:38.