6

From a general Computer Science perspective - when we think of a Queue in a logical sense - we think of being able to 'peek' the first item in the queue.

When I look at the JMS API - it has a MessageListener - which has an OnMessage() method. This feels a little bit like "don't call us- we'll call you."

Does JMS have a concept of a queue peek?

hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • How are you planning to use this functionality? You can read a message and roll it back which allows it to be seen in the queue again. – Peter Lawrey Mar 09 '13 at 23:52
  • Thanks @PeterLawrey - if you could post a link to an example of this then I'll mark it as answered. – hawkeye Mar 10 '13 at 00:51
  • Turn off auto-commit. This requires you to their commit() the message or rollback it. I don't know of any good examples, but a google might help. – Peter Lawrey Mar 10 '13 at 03:09
  • 1
    actually, it would be very nice if JMS had some sort of queue peek, so if you want to pause your consumers, you could just check a flag before consuming the message. Or, if the message-driven bean could have some sort of a @beforeOnMessage method where you could just execute before actually consuming the message – Leo Feb 07 '14 at 13:29

2 Answers2

6

1) If you want to receive messages synchroneously use MessageConsumer.receive

2) Basically, 'peek' in queues means to return, but do not remove, the head of the queue. For this you can use QueueBrowser to look at messages on a queue without removing them.

Jason Pyeron
  • 2,388
  • 1
  • 22
  • 31
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

JMS has a QueueReceiver, which, well, receives messages. You can set a MessageListener to this QueueReceiver to be notified of new messages.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255