0

I'm looking for a correct way, to do the following in Java EE 6, if possible with vanilla Java EE 6 only.

I want to put a job in a job queue and have a fixed pool of worker objects, which should pull a job from the queue, if they are idle.

The worker objects are in a fixed relation to a legacy system, so it is not possible to use one worker object in multiple threads for all jobs and it is also not possible to instantiate a new worker object for every job.

The greedy worker pattern looks perfect, but that's only true for Java SE. In EE, I'm not sure, what the correct way is, to implement this.

Any suggestions?

Thanks in Advance. M.

user1631581
  • 201
  • 1
  • 2
  • 7

1 Answers1

0

The first thing to notice is, that by definition in the spec you must not create and start your own threads in JavaEE.

Concerning your setup, I'm not completely sure how it works in your system - do you have a fixed relation to clients all the time or are there only jobs from time to time to execute which then do work for one client?

In both cases you can just use stateful EJBs, so that one EJB serves a specific client system. Then for the first case this EJB serves the client for the whole lifecycle or for the second case you can start asynchronous EJBs to do the work.

Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96
  • Thanks for your answer. After doing this, I have for example four EJBs as a counterpart to my legacy system and I'm sure, that one EJB is only used in one thread at a time. Now let's look at the other end: How can I achieve, that incoming requests (for example from a servlet) are distributed to (or pulled from) that EJB, that has nothing to do (==> greedy worker approach)? – user1631581 Apr 26 '13 at 05:57
  • @user1631581 I still haven't completely understood your setup. You speak of 4 EJBs and 1 legacy system. Why do you have a fixed number? Can the legacy system only have a certain amount of connections at a time? And if your EJB is connected to the legacy system, does it serve it in a fire-and-forget style or does it have to maintain state over a longer period? – Alexander Rühl Apr 26 '13 at 06:16
  • @user1631581 [continue] As I have understood from your comment, there are a fixed number of workers and whoever is available should do the job. So then you don't have to do anything special. Your servlet can use an injected EJB and the container will provide an instance of the EJB for the work (which is guaranteed to be exclusively used by the caller). If for some reason there should be a fixed number, then you have to specify that in the deployment descriptor for the container (e.g. glassfish-ejb-jar.xml). – Alexander Rühl Apr 26 '13 at 06:22
  • This sounds intersting. But I guess, I should have provide more info: The number of workers have to be configurable by the end user. So it is not possible to keep this information in the deployment descriptor but it should be stored in a database. – user1631581 Apr 29 '13 at 05:50