Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Advanced JMS Design Patterns for WebLogic Server Environments Part 2

Advanced JMS Design Patterns for WebLogic Server Environments Part 2

Part 1 of this article (WLDJ, Vol. 1, issue 8) explored third-party Java Message Service (JMS) integration into WebLogic Server (WLS) and addressed related issues. In Part 2, we'll implement transactional JMS design patterns using SonicMQ and the WebLogic Server Adapter (WLSAdapter) as the JMS solution. Included in this discussion are the message-driven bean (MDB), message-producing bean (MPB), and message- consumer bean (MCB).

Pattern 1: Message-Driven Bean with a Queue Listener
This design pattern is used with the WLSAdapter to create and configure an MDB that will be used with a SonicMQ Queue to implement an XA transaction. The destinations and connection factories will be stored in a file-based JNDI store.

Connection Factory
The first step is to create the JNDI entry for the connection factory. This can be done with a JNDI loader application that operates outside of WebLogic, or in a startup class that creates the entries within the WLS internal JNDI store (see end of article). We'll assume a file-based JNDI store for this example. Use the WLSAdapter to create the XAConnectionFactory.

XAQueueConnectionFactory xafactory =
new com.sonicsw.sonicmq.j2ee.wls.
XAQueueConnection Factory ("localhost:2506", "QueueMessageDriven",

This creates a connection factory named "xafactory" that's bound to a JNDI entry. The variable m_context is a reference the file-based JNDI context created from the com.sun.jndi. fscontext.RefFSContextFactory package.

m_context.rebind("myJMSxaqcf", xafactory);

At this point, a connection factory named "myJMSxaqcf" is ready for use in the JNDI store. A reference to the queue in the JNDI store is now created:

javax.jms.Queue queue = new progress.message.jclient.Queue ("sonicQueue");

This queue would also be bound to a JNDI entry:

m_context.rebind("sonicQueue", queue);

Deployment Descriptors
An EJB deployed in WLS requires two descriptor files: a standard "ejb-jar.xml" and a WebLogic-specific file, "weblogic-ejb.xml". In ejb-jar.xml, the MDB is prepared for an XA transaction by setting the trans-attribute tag to "Required"(see Listing 1; the listings for this article may be found online at www.sys-con.com/weblogic/sourcec.cfm).

The queue and connection factory are designated in the WebLogic-specific descriptor. The tag destination-jndi-name identifies the destination, in this case the queue "sonicQueue". The connection factory "myJMSxaqcf" is identified in connection-factory-jndi-name tag (see Listing 2).

Message-Driven Bean
The MDB doesn't require anything special to actually receive the message in an XA transaction. It just needs to be deployed transactionally in WebLogic, and it must implement the MessageDrivenBean and MessageListener interfaces.
The onMessage() method of this example will receive a message sent to the queue "sonicQueue":

public void onMessage(Message msg) { try { System.out.println("Received from sonicQueue"); } catch(JMSException ex) { ex.printStackTrace(); } }

Pattern 2: Message-Driven Bean with a Topic Listener
and Reconnect

This design pattern is used with the WLSAdapter to create
and configure an MDB that will be used with a SonicMQ Topic
to implement an XA transaction.
It will support reconnect capabilities.

Connection Factory
Use the WLSAdapter to create the XAConnectionFactory.

xafactory = new

To identify the fail-over brokers, connection URLs for
the factory are set:


A reconnect version of the connection factory is then created from the original connection factory:

long pingInterval = 1000L; long facSleep = 10000L; int maxIterations = 10; com.sonicsw.ssps.reconnect.
rec_xafactory =
new com.sonicsw.ssps.reconnect.
(xafactory, pingInterval, facSleep,

Parameters are set to define reconnect behavior. The value of "pingInterval" indicates how often (in milliseconds) to check for a lost connection. The value of "facSleep" indicates how long to wait before reconnecting. Finally, "maxIterations" indicates how many attempts should be made to reconnect. The reconnect version of the connection factory is then bound in the JNDI store:

m_context.rebind("myJMSrecxatcf", rec_xafactory);

Deployment Descriptors
The values of the destination-jndi-name tag and the connection-factory-jndi-name tag reflect the topic and the connection factory, in this case "sonicTopic" and "myJMSxatcf", respectively (see Listing 3).

Pattern 3: Stateless Session Message-Consumer Bean
This design pattern is used with the WLSAdapter to create and configure a stateless session bean that acts as a message consumer, an MCB. The example uses the receive() method of a receiver object to synchronously receive messages from a queue. The design would be similar for a topic-based MCB, where the receive() method of a subscriber object would be used.

This example assumes the home interface of the bean exposes three methods that correspond directly to the three types of receive() methods available on a receiver object:

public interface SonicMCBHome extends javax.ejb.EJBObject { public void receive()
throws RemoteException,JMSException ;
public void receive(long timeout)
throws RemoteException, JMSException;
public void receiveNoWait()
throws RemoteException, JMSException; }

Message-Consumer Bean
In the case of a queue-based MCB, the ejbCreate() method will look up a queue from which to create a session object (see Listing 4). From that session, a queue receiver object is created and the connection is started:

queue = (Queue) ctx.lookup(queueName);
receiver = session.createReceiver(queue);

The three receive() methods are implemented in the bean; each method corresponds to a matching receive() method signature on the queue receiver object (see Listing 5).

When one of these methods is called, the receiver will poll for a message from the queue. The receiver will wait indefinitely for a message if the first receive() method is called. If the second receive() method is called, the bean will wait an amount of time equal to the timeout parameter in milliseconds. If no wait time is desired, the receiveNoWait() implementation is used. If the second receive() method times out, a null value is returned. An MCB with a topic subscriber would be similar (see Listing 6). The receive() method is called from a topic subscriber instead of the queue receiver (see Listing 7).

Pattern 4: Message-Producing Bean
This design pattern is used with the WLSAdapter to create and configure a stateless session bean to act as a message producer. The MPB exposes one method, send(), in its home interface. The message is sent out on both a queue and a topic using an XA transaction to ensure that the send occurs on both destinations. The MPB also supports reconnect.

Connection Factories
First, XAConnectionFactories are created and bound. One is created for the queue and one for the topic, along with their reconnect counterparts (see Listing 8). JNDI entries are created for the two destinations, "sonicQueue" and "sonicTopic" (see Listing 9).

Deployment Descriptors In the ejb-jar.xml file, the trans-attribute tag is set to "Required" for XA transactions (see Listing 10). In the WebLogic-specific descriptor, however, the only necessary information is the ejb-name and the JNDI name for this stateless session bean. The connection factories and destinations aren't referenced here:


Message-Producing Bean
In the ejbCreate() method of the MPB, an initial context is created:

Hashtable env = new Hashtable();
InitialContext ctx = new InitialContext(env);

Then, a queue sender is created from the "myJMSxaqcf" connection factory, and a reference to the XAResource for the queue is retained (see Listing 11). The analogous steps are taken for a topic destination (see Listing 12). At this point, a topic publisher and a queue sender are ready for use in the send() method. When the MPB's send() method is invoked, a reference to the transaction object is obtained from WebLogic's Transaction helper:

javax.transaction.Transaction txn =

Next, the queue message is sent by enlisting the XA queue resource, creating and sending the message, and, finally, delisting the resource:

TextMessage qtext = qsession.createTextMessage
(qName + ":" + message);
txn.delistResource(qxar, XAResource.TMSUCCESS);

The steps are repeated for the topic:

TextMessage ttext = qsession.createTextMessage
(tName + ":" + message);
txn.delist Resource(txar, XAResource.TMSUCCESS);

A Note About Session Pooling
One clever aspect of the MPB built on a stateless session bean is the lightweight session pooling that can then be implemented through WLS. For a session bean, the initial and maximum number of instances of the bean that reside on the WLS can be configured in the WebLogic-specific descriptor file for that bean (see Listing 13).

Although these parameters' values can't be changed while WLS is running, this interface allows for some control over resources and a simple pooling mechanism to optimize message production.

Pulling the Design Patterns Together
Figure 1 illustrates a circumstance where an MDB implementation consumes messages from a topic, processes them into a local database via an entity bean, and then produces a response message for transmission over a queue via an MPB. In a highly scaled environment, the transaction might also use pooled, auto-reconnect MPBs to achieve higher throughput and manage scalability. Most importantly, the entire interaction is transactional. If a failure occurs along any execution point, the entire process can be rolled back, with the developer coding minimal recovery logic.

This illustration is just one of many possible combinations of core JMS design patterns integrated with applications' EJBs. Dozens more are facilitated and documented in the adapter package. From this brief discussion of the design patterns and implementation techniques, it should be clear that along with a robust adapter suite such as that provided with SonicMQ, the tools exist to create reliable, high-performance enterprise solutions for most complex scenarios.

While BEA WebLogic Server has JMS support to address simple requirements, the need for sophisticated and higher-performance capabilities of pure-play JMS providers will remain in certain circles for quite some time. Fortunately, the leading JMS providers have worked around "WLS-isms" to enable transactional integration of their products.

In the meantime, BEA has stated that WLS 7.0 will solve the issues of standards compliance. This may indeed obviate the need for XA integration adapters. However, the issues of automatic reconnection, JNDI loading, and failure injection will remain, as will the need for advanced adapter solutions that address these issues. Moreover, having the design wherewithal, best practices, and proven design patterns to implement reliable, globally scalable messaging systems in enterprise application server networks, adapters or not, will be an enduring capability well beyond the retirement of WLS 7.0. The qualities of SonicMQ and its associated adapter for WLS stand out as unique among the field of solutions in this area.

The authors wish to acknowledge the following individuals who contributed to this article: Gary Ark, Bill Cullen, Kathy Guo, May Hsu, K.V. Sastry, and Ademola Taiwo.

One quirk of WLS is that its JNDI repository isn't persistent across server sessions. Consequently, an outboard JNDI must be used or a JNDI loader class must be hard-wired for each application (the latter being the most common default). This seriously limits deployment flexibility and complicates network administration and maintenance.

The Sonic WLSAdapter cleverly answers these issues by providing a pattern for a generic, XML-driven JNDI loader class and an accompanying utility that generates the XML file directly from the JMS broker management API.

More Stories By Hub Vandervoort

Hub Vandervoort is vice president of Professional Services for Sonic Software. He has over twenty years experience as a consultant and senior technology executive in the networking, communications software, and Internet industries. Vandervoort previously co-founded three start-up ventures, including early message-oriented middleware (MOM) leader Horizon Strategies, Inc., which he merged with Momentum Software Corporation. He also co-founded, and served as board member of the Message-Oriented-Middleware Association (MOMA).

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use of real time applications accelerate, legacy networks are no longer able to architecturally support cloud adoption and deliver the performance and security required by highly distributed enterprises. These outdated solutions have become more costly and complicated to implement, install, manage, and maintain.SD-WAN offers unlimited capabilities for accessing the benefits of the cloud and Internet. ...
Business professionals no longer wonder if they'll migrate to the cloud; it's now a matter of when. The cloud environment has proved to be a major force in transitioning to an agile business model that enables quick decisions and fast implementation that solidify customer relationships. And when the cloud is combined with the power of cognitive computing, it drives innovation and transformation that achieves astounding competitive advantage.
DXWorldEXPO LLC announced today that "IoT Now" was named media sponsor of CloudEXPO | DXWorldEXPO 2018 New York, which will take place on November 11-13, 2018 in New York City, NY. IoT Now explores the evolving opportunities and challenges facing CSPs, and it passes on some lessons learned from those who have taken the first steps in next-gen IoT services.
SYS-CON Events announced today that Silicon India has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Published in Silicon Valley, Silicon India magazine is the premiere platform for CIOs to discuss their innovative enterprise solutions and allows IT vendors to learn about new solutions that can help grow their business.
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT staff augmentation services for software technology providers. By providing clients with unparalleled niche technology expertise and industry experience, Chetu has become the premiere long-term, back-end software development partner for start-ups, SMBs, and Fortune 500 companies. Chetu is headquartered in Plantation, Florida, with thirteen offices throughout the U.S. and abroad.
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 18th International Cloud Expo, which will take place on June 7–9, 2016, at the Javits Center in New York City, NY. DatacenterDynamics is a brand of DCD Group, a global B2B media and publishing company that develops products to help senior professionals in the world's most ICT dependent organizations make risk-based infrastructure and capacity decisions.
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.