Welcome!

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.

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

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.

com.sonicsw.sonicmq.j2ee.wls.XATopic
ConnectionFactory
xafactory = new
com.sonicsw.sonicmq.j2ee.wls.
XATopicConnectionFactory
("localhost:2506","TopicMessageDriven",
"mypassword");

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

xafactory.setConnectionURLs
("localhost:2506,
localhost:2507");

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.
TopicConnectionFactory
rec_xafactory =
new com.sonicsw.ssps.reconnect.
TopicConnectionFactory
(xafactory, pingInterval, facSleep,
maxIterations);

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);
connection.start();

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:

<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>JMSstatelessSession</ejb-name>
<jndi-name>wls-jms-statelessSession</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

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

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL,
"file://localhost/C:/temp/jndi/fileStore/");
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 =
weblogic.transaction.TxHelper.getTransaction();

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

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

The steps are repeated for the topic:

txn.enlistResource(txar);
TextMessage ttext = qsession.createTextMessage
(tName + ":" + message);
publisher.publish(ttext);
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.

Conclusion
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.

Acknowledgement
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.

SIDEBAR
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
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.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
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...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...