What is the best solution for an ejb that needs to "wait" for data? I have an ejb that goes and checks a database table for some data, if the data is not there it needs to wait x number of seconds and check again. I know that Thread.sleep/Thread.wait and such manipulation is not recommended in the Java EE spec. However there has to be a realistic solution to this problem. Some other reading has suggested that you must kick off a timer or something however I really do need to block the requesting call until this data is available which eliminates the schedule or timer solution. Any other ideas?
Asked
Active
Viewed 1,198 times
0
-
Can you link the quote in spec? If you need to block, then sleeping is probably what you want to do. With servlets 3, you can release the container thread by starting an Async context `startAsync` and sleeping in a separate thread. – Sotirios Delimanolis Apr 23 '13 at 20:09
-
The restrictions of the EJB container can be read here. In here it says you should not manage "threads" which is what sleep() and wait() is doing. http://www.oracle.com/technetwork/java/restrictions-142267.html – EpicOfChaos Apr 23 '13 at 20:29
-
I think several people would argue that sleep() and wait() count as managing it. View this other question and responses. http://stackoverflow.com/questions/8202492/thread-sleep-in-an-ejb – EpicOfChaos Apr 23 '13 at 20:37
-
`sleep()` and `wait()` should NOT be used in JEE. This is a major no-no and will cause production problems! – Jonathan S. Fisher Apr 24 '13 at 19:37
-
I am curious what kind of issues arise from doing this? I see a lot of people say don't do it, it is a no no. But I haven't actually heard of what the problems are it creates. – EpicOfChaos Apr 24 '13 at 22:52
1 Answers
2
You can use and EJB Timer to do this, but honestly "polling" for data is a waste of resources. A better way is to use JMS. Have you producer of the data put the data in the database, then put a message on Queue. Both the message and database write will commit together in the same transaction. All you do after that is have a listener on the JMS queue that triggers when a message arrives. And there you go, no "polling needed."
If you need specific examples, let me know I can extend my answer a bit.

Jonathan S. Fisher
- 8,189
- 6
- 46
- 84
-
I agree that JMS would be ideal, however in my case the request is coming from a web service request which kind of ruins the idea of JMS. – EpicOfChaos Apr 23 '13 at 20:30
-
So you want a web service request to block while waiting for data to be written to a database??? – Jonathan S. Fisher Apr 23 '13 at 22:32
-
Yes. I know it seems strange but that is what I need. Normal scenerio it wouldn't be waiting much longer than 2 - 3 seconds – EpicOfChaos Apr 23 '13 at 22:52
-
An EJB timer is probably what you want. However, whoever came with this design needs to find a different career. – Jonathan S. Fisher Apr 24 '13 at 00:37
-
So let me give you a little more background on the problem. There are several web service calls made that is all requesting the same data, but instead of calling the 3rd party service several time the first thread in gets the semaphore and makes the call while the other threads wait for the results to come back. so they can just use the "cache" of the result. – EpicOfChaos Apr 24 '13 at 13:31
-
Not sure I understand. Can you edit your main question and explain what's happening? – Jonathan S. Fisher Apr 24 '13 at 19:36
-
I have tried a couple approaches using an EJB Timer however I cannot seem to get this solution to work. When you create the timer it then becomes non-blocking because the action of the @Timeout runs on a separate thread. Do you have an example or can you discuss this solution it more detail? Remember I need blocking. – EpicOfChaos Apr 25 '13 at 13:36