![]() These are available in both JMS 1.1 and JMS 2.0 and are created using createConsumer. In summary, then, whereas JMS 1.1 defined two different types of topic subscription, JMS 2.0 defines four types, all of which can be created using either the classic or simplified APIs: MessageConsumer messageConsumer = session.createSharedDurableConsumer(topic,"myDurableSub") Since the two JVMs need to be able to identify the subscription that they need to share, they need to supply a name to identify the shared subscription, as shown in Listing 2. This method is available both on Session (for applications using the classic API) and on JMSContext (for applications using the simplified API). You can create a "shared" nondurable subscription using a new method: createSharedConsumer. If you think of a "subscription" as a logical entity that receives a copy of every message sent to the topic, then we want the two consumers to use the same subscription. If you use createConsumer to create a second consumer in a separate JVM (or a separate thread on the same JVM), each consumer will use a separate subscription, and so it will receive a copy of every message received by the topic. (In Java EE, you could do it using a pool of message-driven beans ). In JMS 1.1, there's no way to do this in a normal Java SE application. However, what if the application takes a long time to process each message? How do we make the application more scalable by sharing the work of processing these messages between, say, two JVMs, with one JVM processing some of the messages and the other JVM processing the remaining messages? In Listing 1, the consumer will receive a copy of every message sent to the topic. Message = messageConsumer.receive(10000) ("Message received: " + ((TextMessage) message).getText()) Message message = messageConsumer.receive(10000) MessageConsumer messageConsumer = session.createConsumer(topic) Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE) In Listing 1, the createConsumer method on Session is used to create a nondurable subscription on the specified topic (we'll discuss durable subscriptions in just a moment):Ĭopy private void createUnsharedConsumer(ConnectionFactory connectionFactory, Topic topic)Ĭonnection connection = connectionFactory.createConnection() Let's review how topic subscriptions worked in JMS 1.1. This restriction has been removed in JMS 2.0 by the introduction of a new kind of topic subscription called a shared subscription. This meant that the work of processing messages on a topic subscription could not be shared among multiple threads, connections, or Java Virtual Machines (JVMs), thereby limiting the scalability of the application. In JMS 1.1, a subscription on a topic was not permitted to have more than one consumer at a time. ![]() Multiple Consumers Allowed on the Same Topic Subscription Here we discuss five important new messaging features in JMS 2.0. As I explain below, some of the features are available only in a standalone environment while others are available only in Java EE Web or EJB applications. ![]() It can also be used standalone in a Java SE environment. JMS 2.0 is part of the Java EE 7 platform and can be used in Java EE Web or EJB applications. The opportunity has been taken to introduce a number of new messaging features as well. In JMS 2.0, the emphasis has been on catching up with ease-of-use improvements that have been made to other enterprise Java technologies in recent years. However, if you judge the success of an API standard by the number of different implementations, JMS is one of the most successful APIs around. One might think that an API that has remained unchanged for so long has grown moribund and unused. JMS 2.0, which was released in April 2013, is the first update to the JMS specification since version 1.1 was released in 2002. Here, we look at important new messaging features. In Part One, we looked at the new ease-of-use features introduced in JMS 2.0. It assumes a basic familiarity with JMS 1.1. This article, which is the second in a two-part series, introduces some of the new messaging features introduced in Java Message Service (JMS) 2.0. Learn how to take advantage of new messaging features in JMS 2.0.
0 Comments
Leave a Reply. |