Send and receive JMS Messages

Send and receive JMS Messages

After setting everything up like described in the Dependencies and Getting started page we are now ready to to send/receive messages.

Receive messages from queues and topics

There are two ways to do this:

Manual setup

You can fetch the Connection from the OSGi service factory, create a session and setup everything you like. This is the most flexible way, but you must keep track of the Connection and handle all the creation of JMS objects (Sessions, Queues, Topics, Consumers,...) on your own.

White-Board-Pattern aproach

Pax JMS provides a component that embodies the Whiteboard-Pattern. This means you can simply register a MessageListener with the OSGi Service Registry and it will be picket up, and all the "hard work" will eb done by Pax JMS. When registering a MessageListener the following properties are supported:

  • org.ops4j.pax.jms.consumer.queue or org.ops4j.pax.jms.consumer.topic - the queue or topic to listen for messages. One of these is required.

  • org.ops4j.pax.jms.consumer.selector - you can specify an optional message selector that should be used

  • org.ops4j.pax.jms.consumer.nonlocal - optional property that specifies if the nonLocal flag should be given to the consumer, see Session.html#createConsumer for details)

  • org.ops4j.pax.jms.session.mode - you can control the acknoledge session mode with either the String or int representation see Connection.html#createSession for details, if not given AUTO_ACKNOWLEDGE is assumed.

  • org.ops4j.pax.jms.session.transacted - optional flag to specify if you want a transacted session (see Connection.html#createSession for details) if not given false is assumed

Send messages to queues and topics

Because there is no JMS standard interface for producers that support IOC (inversion of control), currently the way to do this is fetch the Connection from the OSGi service factory, create a session and producer as defined in the JMS spec.

We might later introduce:

  • an interface

  • annotations

  • ...

that will make the process of fetching a consumer more convinient preferable one that works with the whiteboardpattern.