4 Understanding the Simplified API Programming Model

Understand the key features of JMS simplified API defined by the Java Message Service (JMS) 2.0 specification. Also learn how it is implemented for creating JMS applications for WebLogic Server.

About JMS 2.0 Simplified API

The JMS 2.0 simplified API provides the same basic functionality as the JMS 1.1 API (classic API), but the new interfaces and several API changes make it easier to use.

The following interfaces provided by the simplified API were implemented in Oracle WebLogic Server 12.2.1 release:

  • ConnectionFactory : An administered object used by a client to create a Connection. This interface is also used by the classic API.

  • JMSContext : An active connection to a JMS provider and a single-threaded context used to send or receive messages.

  • JMSProducer : An object created by a JMSContext to send messages to a queue or topic.

  • JMSConsumer : An object created by a JMSContext to receive messages sent to a.queue or topic

Figure 4-1 shows how these objects fit together in a JMS client application.

Figure 4-1 Simplified API Programming Model

Description of Figure 4-1 follows
Description of "Figure 4-1 Simplified API Programming Model"

For more information about the JMS 2.0 interfaces, see the javax.jms package documentation at http://docs.oracle.com/javaee/7/api/javax/jms/package-summary.html.

New Interfaces in the Simplified JMS API

The JMS 2.0 simplified API consists of three new interfaces.

JMSContext

The main interface in the simplified API is JMSContext . It combines the functions of the Connection and Session objects of the JMS 1.1 API. Creating a single JMSContext object eliminates the need to create a connection, session, and a text message separately.

For more information about the JMSContext interface, see http://docs.oracle.com/javaee/7/api/javax/jms/JMSContext.html.

The WLJMSContext interface in the weblogic.jms.extensions package defines the fields and methods that are not supported by javax.jms.JMSContext. It provides the same extension features as WLConnection and WLSession. See the Javadoc for WLJMSContext in Java API Reference for Oracle WebLogic Server.

JMSProducer

To send messages in the simplified API, use a JMSProducer object. You can create a JMSProducer object by calling the createProducer method on a JMSContext object.

Note:

You do not need to save the JMSProducer object in a variable. It is recommended that you create this object when sending a message. For more information, see Sending Messages Using the Simplified JMS API.

For more information about the JMSProducer interface, see http://docs.oracle.com/javaee/7/api/javax/jms/JMSProducer.html.

The WLJMSProducer interface defines methods and attributes specific to WebLogic JMS. You can use these features by casting the JMSProducer instance to the WLSJMSProducer interface defined in the weblogic.jms.extensions package. See the Javadoc for WLJMSProducer in Java API Reference for Oracle WebLogic Server.

JMSConsumer

The JMSConsumer object receives messages from a queue or topic. You can create a JMSConsumer object by passing a Queue or Topic object to one of the createConsumer methods on a JMSContext or by passing a Topic object to one of the createSharedConsumer or createDurableConsumer methods on a JMSContext object.

For more information about the JMSConsumer interface, see http://docs.oracle.com/javaee/7/api/javax/jms/JMSConsumer.html.

New Methods to Simplify Messaging in JMS 2.0

In addition to the methods for sending and receiving messages on JMSContext objects, JMS 2.0 introduces a few more methods to simplify the code.

Method to Extract the Body Directly from a Message

The getBody method provides an easy way to obtain the body from a message. This method applies to both the classic and simplified API.

void onMessage(Message message){ // delivers a BytesMessage
   byte[] bytes = message.getBody(byte[].class);
   ...

For more information, see the Javadoc at

http://docs.oracle.com/javaee/7/api/javax/jms/Message.html

Method to Receive a Message Body Directly

The receiveBody method can be used to receive any type of message except for StreamMessage and Message, as long as the class of the expected body is known in advance.

JMSConsumer consumer = ...
String body = consumer.receiveBody(String.class,1000);

For more information, see the Javadoc at:

https://docs.oracle.com/javaee/7/api/javax/jms/JMSConsumer.html

Method to Create a Session

A new createSession method, that accepts a single parameter or no parameter, was added to the javax.jms.Connection. See Create a Session Using the createSession Method.