9 Enhanced Support for Using WebLogic JMS with EJBs and Servlets

Usability features that are generally hidden behind the Java EE standard have been enhanced to make it easier to access EJB and servlet containers with WebLogic JMS or third-party JMS providers. In fact, implementing this "JMS wrapper" support, as described in this section, is the best practice method of sending a WebLogic JMS message from inside an EJB or servlet.

Simplified Access to Remote or Foreign JMS Providers briefly describes the Administration Console support for foreign JMS providers, as documented in "Accessing Foreign JMS Providers" in the Oracle Fusion Middleware Configuring and Managing JMS for Oracle WebLogic Server. This feature makes it possible to easily map foreign JMS providers — including remote instances of WebLogic Server in another cluster or domain — so that they appear in the local JNDI tree as a local JMS object.

Enabling WebLogic JMS Wrappers

WebLogic Server uses JMS wrappers that make it easier to use WebLogic JMS inside a Java EE component, such as an EJB or a servlet, while also providing a number of enhanced usability and performance features:

Declaring JMS Objects as Resources In the EJB or Servlet Deployment Descriptors

You enable these enhanced Java EE features by declaring a JMS connection factory as a resource-ref in the EJB or servlet deployment descriptors, as described in Declaring a Wrapped JMS Connection Factory. For example, when a connection factory is declared as a resource-ref, a JMS application can look it up from JNDI using the java:comp/env/ subtree that is created for each EJB or servlet. It is important to note that the features listed above are only enabled when using a JMS resource inside the deployment descriptors. The EJB and servlet programmers still have direct access to the JMS provider by performing a direct JNDI lookup of the connection factory or destination.

For more information about packaging EJBs, see "Implementing Enterprise JavaBeans" in Oracle Fusion Middleware Programming Enterprise JavaBeans for Oracle WebLogic Server. For more information about programming servlets, see "Creating and Configuring Servlets" in Oracle Fusion Middleware Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server.

Declaring a Wrapped JMS Connection Factory

You can declare a JMS connection factory as part of an EJB or servlet by defining a resource-ref element in the ejb-jar.xml or web.xml file, respectively. This process creates a "wrapped" JMS connection factory that can benefit from the more advanced session pooling, automatic transaction enlistment, connection monitoring, and container-managed security features described in Improving Performance Through Pooling.

This element declares that a JMS QueueConnectionFactory object will be bound into JNDI, at the location:

java:comp/env/jms/QCF

This JNDI name is only valid inside the context of the EJB or servlet where the resource-ref is declared, which is what the java:comp/env JNDI context signifies.

In addition to this element, there must be a matching resource-description element in the weblogic-ejb-jar.xml (for EJBs) or weblogic.xml (for servlets) file that tells the Java EE container which JMS connection factory to put in that location. Here is an example:

The connection factory specified here must already exist in the global JNDI tree. (This example uses one of the default JMS connection factories that is automatically created when the built-in WebLogic JMS server is used). To use another WebLogic JMS connection factory from the same cluster, simply include that connection factory's JNDI name inside the jndi-name element. To use a connection factory from another vendor, or from another WebLogic Server cluster, create a Foreign JMS Server.

If the JNDI name specified in the resource-description element is incorrect, then the application is still deployed. However, you will receive an error when you try to use the connection factory.

Declaring JMS Destinations

You can also bind a JMS queue or topic destination into the java:comp/env/jms JNDI tree by declaring it as a resource-env-ref element in the ejb-jar.xml or web.xml deployment descriptors. The transaction enlistment, pooling, connection monitoring features take place in the connection factory, not in the destinations. However, this feature is useful for consistency, and to make an application less dependent on a particular configuration of WebLogic Server, since destinations can easily be modified by simply changing the corresponding resource-env-ref description, without having to recompile the source code.

This element declares that a JMS Queue destination object will be bound into JNDI, at the location:

java:comp/env/jms/TESTQUEUE

As with a referenced connection factory, this JNDI name is only valid inside the context of the EJB or servlet where the resource-ref is declared.

You must also define a matching resource-env-description element in the weblogic-ejb-jar.xml or weblogic.xml file. This provides a layer of indirection which allows you to easily modify referenced destinations just by changing the corresponding resource-env-ref deployment descriptors.

The queue or topic destination specified here must already exist in the global JNDI tree. Again, if the destination does not exist, the application is deployed, but an exception is thrown when you try to use the destination.

Sending a JMS Message In a Java EE Container

After you declare the JMS connection factory and destination resources in the deployment descriptors so that they are mapped to the java:comp/env JNDI tree, you can use them to send and/or receive JMS messages inside an EJB or a servlet.

This is standard code that complies with the Java EE specification and should run on any EJB or servlet product that properly supports Java EE — the difference is that it runs more efficiently on WebLogic Server, because under the covers various objects are pooled, as described in Pooled JMS Connection Objects.

Note that this code fragment uses a try...finally block to guarantee that the close() method on the JMS Connection object is executed even if one of the statements inside the block throws an exception. If no connection pooling were being done, then this block would be necessary in order to ensure that the connection is closed, and to prevent server resources from being wasted. But since WebLogic Server pools some of the objects that are created by this code fragment, it is even more important that close() be called; otherwise, the EJB or servlet container will not know when to return the object to the pool.

Also, none of the transactional XA extensions to the JMS API are used in this code fragment. Instead, the container uses them internally if the JMS code is used inside a transaction context. But whether XA is used internally, the user-written code is the same, and does not use any JMS XA classes. This is what is specified by Java EE. Writing EJB code in this way enables you to run EJBs in an environment where transactions are present or in a non-transactional environment, just by changing the deployment descriptors.

Note:

When using a wrapped JMS connection factory, which is obtained by using the resource-ref feature and looked up by using the java:comp/env/jms JNDI tree context, then the EJB must not use the transactional XA interfaces.

What's Happening Under the JMS Wrapper Covers

This section explains what is actually taking place under the covers when WebLogic Server creates a set of wrappers around the JMS objects. For example, the code fragment in Sending a JMS Message In a Java EE Container, shows an instance of a WebLogic-specific wrapper class being returned rather than the actual JMS connection factory because the connection factory was looked up from the java:comp/env JNDI tree. This wrapper object intercepts certain calls to the JMS provider and inserts the correct Java EE behavior, as described in the following sections.

Automatically Enlisting Transactions

This feature works for either WebLogic JMS implementations or for third-party JMS providers that support two-phase commit transactions (XA protocol). If a wrapped JMS connection sends or receives a message inside a transaction context, the JMS session being used to send or receive the message is automatically enlisted in the transaction through the XA capabilities of the JMS provider. This is the case whether the transaction was started implicitly because the JMS code was invoked inside an EJB with container-managed transactions enabled, or whether the transaction was started manually using the UserTransaction interface in a servlet or an EJB that supports bean-managed transactions.

However, if an EJB or servlet attempts to send or receive a message inside a transaction context and the JMS provider does not support XA, the send() or receive() call throws the following exception:

[J2EE:160055] Unable to use a wrapped JMS session in the transaction because two-phase commit is not available.

Therefore, if you are using a JMS provider that doesn't support XA to send or receive a message inside a transaction, either declare the EJB with a transaction mode of NotSupported or suspend the transaction using one of the JTA APIs.

Container-Managed Security

WebLogic JMS uses the security credentials that are present on the thread when the EJB or servlet container is invoked. For foreign JMS providers, however, when you declare a JMS connection factory via a resource-ref element in the weblogic-ejb-jar.xml or web.xml file, there is an optional sub-element called res-auth. This element may have one of two settings:

Container — When you set the res-auth element to Container, security to the JMS provider is managed by the Java EE container. In this case, if the JMS connection factory was mapped into the JNDI tree using a Foreign JMS Connection Factory configuration MBean, then the user name and password from that MBean is used (see Simplified Access to Remote or Foreign JMS Providers). Otherwise, WebLogic Server connects to the provider with no user name or password specified. In this mode, it is an error to pass a user name and password to the createConnection() method of the JMS connection factory.

Application — When you set the res-auth element to Application, any user name or password on the MBean is ignored. Instead, the application code must specify a user name and password to the createConnection() method of the JMS connection factory, or use the version of createConnection() with no user name or password if none are required.

Connection Testing

The JMS wrapper classes monitor each connection that is established to the JMS provider. They do this in two ways:

Registering a JMS ExceptionListener object on the connection.

Testing the connection every two minutes by sending a message to a temporary queue or topic and then receiving it again.

Java EE Compliance

The Java EE specification states that you should not be allowed to make certain JMS API calls inside a Java EE application. The JMS wrappers enforce these restrictions by throwing the following exceptions when they are violated:

On the connection object, the methods createConnectionConsumer(), createDurableConnectionConsumer(), setClientID(), setExceptionListener(), and stop() should not be called.

On the session object, the methods getMessageListener() and setMessageListener() should not be called.

On the consumer object (a QueueReceiver or TopicSubscriber object), the methods getMessageListener() and setMessageListener() should not be called.

Furthermore, the createSession() method, and the associated createQueueSession() and createTopicSession() methods, are handled differently. The createSession() method takes two parameters: an "acknowledgement" mode and a "transacted" flag. When used inside an EJB, these two parameters are ignored. If a transaction is present, then the JMS session is enlisted in the transaction as described in Automatically Enlisting Transactions; otherwise, it is not. By default, the acknowledgement mode is set to "auto acknowledge". This behavior is expected by the Java EE specification.

Note:

This may make it more difficult to receive messages from inside an EJB, but the recommended way to receive messages from inside an EJB is to use a MDB, as described in "Message-Driven EJBs" in Oracle Fusion Middleware Programming Enterprise JavaBeans for Oracle WebLogic Server.

Inside a servlet, however, the parameters to createQueueSession() and createTopicSession() are handled normally, and users can make use of all the various message acknowledgement modes.

Improving Performance Through Pooling

The automatic pooling of connections and other objects by the JMS wrappers means that it is efficient to write code as shown in Sending a JMS Message In a Java EE Container. Although in this example the Connection Factory, Connection, and Session objects are created every time a message is sent, in reality these three classes work together so that when they are used as shown, they do little more than retrieve a Session object from the pool.

Speeding Up JNDI Lookups by Pooling Session Objects

The JNDI lookups of the Connection Factory and Destination objects can be expensive in terms of performance. This is particularly true if the Destination object points to a Foreign JMS Destination MBean, and therefore, is a lookup on a non-local JNDI provider. Because the Connection Factory and Destination objects are thread-safe, they can be looked up once inside an EJB or servlet at creation time, which saves the time required to perform the lookup each time.

Inside a servlet, these lookups can be performed inside the init() method. The Connection Factory and Destination objects may then be assigned to an instance variable and reused whenever a message is sent.

Inside an EJB, these lookups can be performed inside the ejbCreate() method and assigned to an instance variable. For a session bean, each instance of the bean will then have its own copy. Since stateless session beans are pooled, this method is also very efficient (and is perfectly consistent with the Java EE specifications), because the number of a times that lookups occur is drastically reduced by pooling the JMS connection objects. (Caching these objects in a static member of the EJB class may work, but it is discouraged by the Java EE specification.)

However, if these objects are cached inside the ejbCreate() or init() method, then the EJB or servlet must have some way to recreate them if there has been a failure. This is necessary because some JMS providers, like WebLogic JMS, may invalidate a Destination object after a server failure. So, if the EJB runs on Server A, and JMS runs on Server B, then the EJB on Server A will have to perform the JNDI lookup of the objects from Server B again after that server has recovered. The example, PoolTestBean.java includes a sample EJB that performs this caching and re-lookup process correctly.

Speeding Up Object Creation Through Caching

Once Connection Factory object and/or Destination object pooling has been established, it may be tempting to cache other objects, such as the Connection, Session, and Producer objects, inside the ejbCreate() method. This will work, but it is not always the most efficient solution. Essentially, by doing this you are removing a Session object from the cache and permanently assigning it to a particular EJB, whereas by using the JMS wrappers as designed, that Session object can be shared by other EJBs and servlets as well. Furthermore, the wrappers attempt to reestablish a JMS connection and create new session objects if there is a communication failure with the JMS provider, but this will not work if you cache the Session object on your own.

Enlisting the Proper Transaction Mode

When a JMS send() or receive() operation is performed inside a transaction, the EJB or servlet automatically enlists the JMS provider in the transaction. A transaction can be started automatically inside an EJB or servlet that has container-managed transactions, or it can be started explicitly using the UserTransaction interface. In either case, the container automatically enlists the JMS provider. However, if the underlying JMS connection factory used by the EJB or servlet does not support XA, the container throws an exception.

Performing the transaction enlistment has overhead. Furthermore, if an XA connection factory is used, but the send() or receive() method is invoked outside a transaction, the container must still create a JTA transaction to wrap the send() or receive() method in order to ensure that the operation properly takes place no matter which JMS provider is used. Although this is only a one-phase commit, it can still slow down the server.

Therefore, when writing an EJB or servlet that uses a JMS resource in a non-transactional manner, it is best to use a JMS connection factory that is not configured to support XA.

Examples of JMS Wrapper Functions

The following files make up a simple stateless EJB session bean that uses the WebLogic JMS wrapper functions to send a transactional message (sendXATransactional) when an EJB is called. Although this example uses a session bean, the same XML descriptors and bean class (with very few changes) can be used for a message-driven bean.

ejb-jar.xml

This section describes the EJB components. For the "JMS wrapper" code snippets provided in this section, note that this section declares the resource-ref and resource-env-ref elements for the wrapped JMS connection factory (QueueConnectionFactory) and referenced JMS destination (TESTQUEUE).

Simplified Access to Remote or Foreign JMS Providers

Another set of foreign JMS provider features makes it possible to create a "symbolic link" between a JMS connection factory or destination object in an third-party JNDI provider to an object inside the local WebLogic Server. This feature can also be used to reference remote instances of WebLogic Server in another cluster or domain in the local WebLogic JNDI tree.

There are three System Module MBeans for this task:

Foreign Server — Contains information about the remote JNDI provider, including its initial context factory, URL, and additional parameters. It is the parent of the Foreign Connection Factory and Foreign Destination MBeans. It can be targeted to an independent WebLogic Server or to a cluster. For more information see, "ForeignServerBean" in the Oracle Fusion Middleware Oracle WebLogic Server MBean Reference.

Foreign Connection Factory — represents a foreign connection factory. It contains the name of the connection factory in the remote JNDI provider, the name to map it to in the server's JNDI tree, and an optional user name and password. The user name and password are only used when a Foreign Connection Factory is used inside a resource-reference in an EJB or a servlet, with the "Container" mode of authentication. It creates non-replicated JNDI objects on each WebLogic Server instance to which the parent Foreign Connection Factory MBean is targeted. (To create the JNDI object on every node in a cluster, target the parent MBean to the cluster.). For more information see, "ForeignConnectionFactoryBean" in the Oracle Fusion Middleware Oracle WebLogic Server MBean Reference.

Foreign Destination — represents a foreign destination. It contains the name to look up on the foreign JNDI provider, and the name to map it to on the local server.

For information on how to configure foreign resources using the Administration Console, see "Configuring Basic JMS System Resources" in Oracle Fusion Middleware Configuring and Managing JMS for Oracle WebLogic Server.

Once deployed, these foreign System Module MBeans work by creating objects in the local server's JNDI tree, which then perform the lookup of the referenced remote JMS objects whenever the foreign System Module MBeans are looked up. This means that the local server and the remote JNDI directory are never out of sync. However, from a performance perspective, it means that a JNDI lookup of one of these MBeans can potentially be expensive. The sections under Improving Performance Through Pooling describes some ways to improve the performance of these remote lookups.

Scripting on this page enhances content navigation, but does not change the content in any way.