• 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

Creating a service with EJB

 
Greenhorn
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

@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
 
Saloon Keeper
Posts: 27762
196
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.
 
Bartender
Posts: 1357
39
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.
 
Do not set lab on fire. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic