Developing applications using the JMS APIs is a straightforward process. The APIs facilitate the creation of objects that mitigate the interface between client applications and the message broker. Once connected to a broker, clients can create, send and receive messages.
A JMS application typically consist of these basic interfaces and classes:
- Connection factory
Connection factories are administered objects that clients use to create connections to a message broker. You can optimize some messaging characteristics by enabling, disabling, or otherwise modifying the values of certain connection factory properties.
- Connection
Connections are the objects clients use to specify a transport protocol and credentials for sustained interaction with a broker.
- Session
Sessions are created by a client on a connection established with a broker. They define whether its messages will be transacted and the acknowledgement mode when they are not. Clients can create multiple sessions on a single connection.
- Destinations
Destinations are created by a client on a per-session basis. They are client-side representations of the queue or topic to which messages are sent. The message broker maintains its own representations of the destinations as well.
- Producer
Producers are client-side objects that create and send messages to a broker. They are instances the
MessageProducerinterface and are created on a per-session basis.The
MessageProducerinterface provides methods not only for sending messages, but also for setting various message headers, includingJMSDeliveryModewhich controls message persistence,JMSPrioritywhich controls message priority, andJMSExpirationwhich controls a message's lifespan.- Consumer
Consumers are client-side objects that process messages retrieved from a broker. They are instances of the
MessageConsumerinterface and are created on a per-session basis.The
MessageConsumerinterface can consume messages synchronously by using one of theMessageConsumer.receive()methods, or asynchronously by registering aMessageListenerwith theMessageConsumer.setMessageListener()method. With aMessageListenerregistered on a destination, messages arriving at the destination invoke the consumer'sMessageListener.onMessage()method, freeing the consumer from having to repeatedly poll the destination for messages.- Messages
Messages are the backbone of a messaging system. They are objects that contain not only the data payload, but also the required header and optional properties needed to transfer them from one client application to another. See JMS Message Basics.
Figure 2 shows the typical sequence of events involved in sending or receiving messages in a Fuse MQ Enterprise client application.
The following procedure shows how to implement the sequence of events shown in Figure 2:
Get a connection factory.
The process for getting a connection factory depends on your environment. It is typical to use JNDI to obtain the connection factory. Fuse MQ Enterprise allows you to instantiate connection factories directly in consumer code or obtain connection factories using Spring in addition to using JNDI.
Create a connection from the connection factory as shown in Example 1.
Start the connection using the connection's
start()method.Create a session from the connection.
Create a destination from the session.
Example 3 shows code for createing a queue called
foo.Create objects for producing and consuming messages.
Create a message from the session object.
Example 6 shows code for creating a text message.
Send and receive messages.
Close all JMS resources.









