reidmorrison / jruby-jms

Complete JRuby API into Java Messaging Specification (JMS)
Apache License 2.0
47 stars 21 forks source link

JMS Synchronous Messaging #12

Closed lstanden closed 10 years ago

lstanden commented 11 years ago

Hello again :)

I'm trying to work out how best to perform synchronous messaging. After doing some debugging, I've determined that supplying a modest timeout (ie. 2 seconds) will result in execution in the top level code for the full duration. After checking out the code, this makes sense form what I'm seeing there.

Would it be possible to add a feature for consuming exactly one message from the queue and then resuming? This would allow the following workflow, which is commonly in use here.

  1. Create temporary queue
  2. Send message to persistent queue with jms_reply_to set to temporary queue (#1)
  3. Immediately consume response from temporary queue
  4. Perform some other action

What I don't want to have is #3 block for the full timeout duration when the message responds immediately. Thanks :)

lstanden commented 11 years ago

I managed to get something that functions using the following code:

session.consumer(:destination => destination) do |consumer|
  if message = consumer.get(:timeout => 2000)
    data = message.data
    # etc
  end
end

I think the syntax could easily be improved by having a consume_one helper method (or similar). I may submit a patch when I get a few minutes :)

reidmorrison commented 10 years ago

Great idea to create a higher level api to wrap the one time call capability. The following sample works, but does require more coding: https://github.com/reidmorrison/jruby-jms/blob/master/examples/client-server/requestor.rb

Looking forward to the merge request