I know i must not tinker with threading in EJB Containers, therefore i do not know how to do the following thing in an EJB Environment the right way:
Involved are:
Scenario is:
Client calls a Method of Server, which in turn sends several Messages to Queue. After that, Server does some other stuff. In the meantime, Mdb consumes a message, calls Wi which does some rather long calculation, and gets the result. Now Mdb gives the result to Server. When Server got all the "results" for every message it sent, it does some more calculations with the results from the W s, and returns that result to Client.
My Problem:
In Java SE I would simply do .wait() to have Server wait for the results of the W s, after Server did the work after sending the messages. Then mdb would .notify() when it has set the results. Since I must not tinker with threading in EJB Containers, as the specification states, I am lost cause I did not find any appropriate way to achieve the same behavior in EJB Environment.
Any help on that problem would really be appreciated, thanks in advance.
P.S.: I am working with JBoss 5.1.0, in case there are any vendor-specific measures to tackle that problem.
notify method wakes up only one thread waiting on the object and that thread starts execution. So if there are multiple threads waiting for an object, this method will wake up only one of them. The choice of the thread to wake depends on the OS implementation of thread management.
For a thread to call wait() or notify(), the thread has to be the owner of the lock for that object. Otherwise, a runtime error occur and the rest of code is not executed.
In between, we have also put the main thread to sleep by using TimeUnit. sleep() method. So the main thread can wait for some time and in the meantime, T1 will resume and complete its execution.
The appropriate solution for this is the "request/response" pattern for messaging. In a nutshell, you can do "synchronous" operations via messaging systems by sending a message and waiting for a response message (all of this is legal in the J2EE world). there are a variety of ways you can achieve this in practice, but the general idea is that you send the request messages with some sort of unique identifier, then wait for response messages using a message filter set for the request id(s) that you sent (this is generally what the "correlationId" field is used for). the MDB would get the request messages, process them, and send the response messages using the specified unique identifiers from the request messages. you can do all of this with one queue, or you can use separate request/response queues, or you can do crazy things like creating "temporary" response queues (per-request). you can tell the MDB where to send the request messages using the Message.setJMSReplyTo
method.
The general pattern is:
receive()
until all messages are received and processed (or times out)(Obviously, the server proceeds directly from step 2 to step 4, i just wrote it this way to highlight the control flow.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With