Reputation: 12787
I want to send message to AMQ and wait for synchronous reply, in case of error, throws application exception.
I have 4 solutions:
MessageReceiver.receive() (waits indefinitely, current impl. but is not recommended way of receiving messages from AMQ; actually I see stuck messages in queue. But I can throw application exception freely after the line of receive()
and all tests pass)
Message Driven Bean + some local cache: message sender sends the message, then enters a while()
loop, checking a cache bean if the reply has arrived. The loop can be with or without Thread.sleep()
. Meanwhile, MDB receiving the message, putting reply into the cache bean. So I use this cache bean to synchronize things.
It works, and MDB is the recommended way of receiving messages in EJB, but I am either entering while()
for a million of times during the 2 seconds I am waiting(I see it a code smell), or managing the thread by myself(Thread.sleep()
), which is against EJB recommendation. Also, on the thread of MDB I cannot throw application exception, as this is a worker thread, not the main thread; and onMessage()
signature does not allow application exception, so the test fails; I need to throw on main thread when reply arrives.
solution 1) but with @Asynchronous
as https://docs.oracle.com/javaee/6/tutorial/doc/gkkqg.html. I see it similar as 1), because Future.get()
will block indefinitely, and receiving messages without MDB is not the recommended way, but I am not managing the thread myself. I can also throw freely.
@Timeout and TimerService. The bad thing is that it is used for application tasks(intended for cron tasks with at most seconds precision, not for milliseconds later tasks). But waiting for even 1s is too long for our service SLA. Also, I cannot throw application exception.
I am not using JBoss 7, so I don't have access to ManagedExecutorService
, like mentioned in EJB asynchronous call not returning control to caller
What should I do?
Upvotes: 0
Views: 17