The Java Message Service (JMS) | JMS and the Importance of Messaging | InformIT
A relationship field is like a foreign key in a database. If a b then a Session Bean Quiz QueueSession qSes; Through JNDI get access to a QueueSender. The boolean parameter indicates whether the QueueSession is transacted or not; the . Finally, we create the QueueSender to send messages to the Queue we. 12 Developing Container-Managed Relationships. 13 Understanding JMS and the QueueSession to create a QueueSender: QueueSender sender.
Sending applications publish their messages, indicating that the message represents information about a topic in the hierarchy. Applications wishing to receive those messages subscribe to that topic. Subscribing to a topic in the hierarchy which contains subtopics allows the subscriber to receive all messages published to the topic and its subtopics.
This figure illustrates the publish and subscribe model. Multiple applications may both subscribe and publish messages to a topic, and the applications remain anonymous to each other.
The MOM acts as a broker, routing the published messages for a topic to all subscribers for that topic. JMS is a set of interfaces and associated semantics that define how a JMS client accesses the facilities of an enterprise messaging product. Products that want to provide JMS functionality do so by supplying a provider that implements these interfaces. As a developer, you build a JMS application by defining a set of messages and a set of client applications that exchange those messages.
There are many enterprise messaging products on the market today, and several of the companies that produce these products were involved in the development of JMS.
These existing systems vary in capability and functionality. The authors knew that JMS would be too complicated and unwieldy if it incorporated all of the features of all existing systems. Likewise, they believed that they could not limit themselves to only the features that all of the systems had in common. The authors believed that it was important that JMS include all of the functionality required to implement "sophisticated enterprise applications.
A provider need not implement both domains. While acknowledged by the JMS authors as important for the development of robust messaging applications, these features are considered JMS provider-specific. It is important to realize that legacy programs will often be part of an overall JMS application and their inclusion must be anticipated in planning.
The format and content of messages to be exchanged by JMS and non-jms clients is integral to the design of a JMS application. As was stated previously, JMS defines a set of interfaces for which a provider must supply concrete implementations specific to its MOM product. An administrator of a messaging system provider creates objects that are isolated from the proprietary technologies of the provider.
Administered objects Providers of MOM products differ significantly in the mechanisms and techniques they use to implement messaging. To keep JMS clients portable, objects that implement the JMS interfaces have to be isolated from the proprietary technologies of a provider. The mechanism for doing this is administered objects. These objects, which implement JMS interfaces, are created by an administrator of the provider's messaging system and are placed in the JNDI namespace.
There are two types of administered objects: Used to create a connection to the provider's underlying messaging system. Used by the JMS client to specify the destination of messages being sent or the source of messages being received. While the administered objects themselves are instances of classes specific to a provider's implementation, they are retrieved using a portable mechanism JNDI and accessed through portable interfaces JMS.
Interfaces JMS defines a set of high-level interfaces that encapsulate various messaging concepts. The high-level interfaces are: An administered object that creates a Connection.
An active connection to a provider. An administered object that encapsulates the identity of a message destination, such as where messages are sent to or received from. A single-threaded context for sending and receiving messages.
For reasons of simplicity and because Sessions control transactions, concurrent access by multiple threads is restricted. Multiple Sessions can be used for multithreaded applications.
Used for sending messages. Used for receiving messages. Interfaces continued The following table identifies the domain-specific interfaces inherited from each high-level interface. Use the ConnectionFactory to create a Connection. Use the Connection to create one or more Sessions. At this point, messages can begin to flow and the application can receive, process, and send messages, as required. In later sections, we'll develop JMS programs and you'll get to see this setup in detail. Messages At the heart of a messaging system are, of course, messages.
JMS provides several message types for different types of content, but all messages derive from the Message Introducing the Java Message Service Page 8 9 interface. A Message is divided into three constituent parts: If your application needs to categorize or classify a message in a way not provided by the standard header fields, you can add a property to the message to accomplish that categorization or classification.
JMS defines a standard set of properties that are optional for providers to supply. Each message interface is specialized for the type of content it supports. Header fields The following list gives the name of each header field of Message, its corresponding Java type, and a description of the field.
This field is set by the provider during the send process; clients cannot determine the JMSMessageID for a message until after it has been sent. A persistent message is delivered "once and only once"; a non-persistent message is delivered "at most once. A non-persistent message may be lost by a provider during application or system failure. Extra care will be taken to assure that a persistent message is not affected by failures. There is often considerable overhead in sending persistent messages, and the trade-offs between reliability and performance must be carefully considered when deciding the delivery mode of a message.
This value is calculated during the send process as the sum of the time-to-live value of the sending method and the current time. Expired messages should not be delivered by the provider. A value of 0 indicates that the message will not expire. A priority of 0 is the lowest priority; a priority of 9 is the highest priority.
A JMS program responding to a message from another JMS program would copy the JMSMessageID of the message it is responding to into this field, so that the requesting program could correlate the response to the particular request that it made.
Some providers maintain a repository of message types and will use this field to reference the type definition in the repository; in this case, the JMS program should not use this field. Standard properties The following list gives the name of each standard property of Message, its corresponding Java type, and a description of the property.
Support for standard properties by a provider is optional. Message body There are five forms of message body, and each form is defined by an interface that extends Message. Contains a stream of Java primitive values that are filled and read sequentially using standard stream operations.
Contains a set of name-value pairs; the names are of type string and the values are Java primitives. Contains a stream of uninterpreted bytes; allows encoding a body to match an existing message format. Each provider supplies classes specific to its product that implement these interfaces. It is important to note that the JMS specification mandates that providers must be prepared to accept and handle a Message object that is not an instance of one of its own Message classes.
While these "alien" objects may not be handled by a provider as efficiently as one of the provider's own implementations, they must be handled to ensure interoperability of all JMS providers. Transactions A JMS transaction groups a set of produced messages and a set of consumed messages into an atomic unit of work. If an error occurs during a transaction, the production and consumption of messages that occurred before the error can be "undone. A transacted Session always has a current transaction, that is, there is no begin ; commit and rollback end one transaction and automatically begin another.
Acknowledgement Acknowledgement is the mechanism whereby a provider is informed that a message has been successfully received. If the Session receiving the message is transacted, acknowledgement is handled automatically.
If the Session is not transacted, then the type of acknowledgement is determined when the Session is created. There are three types of acknowledgement: Lazy acknowledgement of message delivery; reduces overhead by minimizing work done to prevent duplicates; should only be used if duplicate messages are expected and can be handled.
Message delivery is automatically acknowledged upon completion of the method that receives the message. Message delivery is explicitly acknowledged by calling the acknowledge method on the Message. Message selection JMS provides a mechanism, called a message selector, for a JMS program to filter and categorize the messages it receives. The message selector is a String that contains an expression whose syntax is based on a subset of SQL The message selector is evaluated when an attempt is made to receive a message, and only messages that match the selection criteria of the selector are made available to the program.
Selection is based on matches to header fields and properties; body values cannot be used for selection. The syntax for message selectors is provided in detail in the JMS specification. Their reasoning is that XML will be a popular, if not the most popular, means of representing the content of messages.
A portable transport mechanism JMS coupled with a portable data representation XML is proving to be a powerful tool in enterprise application integration EAI and other areas of data exchange. A message-driven bean, which will implement the MessageListener interface see MessageListener on page 16will be invoked by the EJB container on the arrival of a message at a destination designated at deployment time. The message-driven bean will contain the business logic to process the message, including, if needed, the invoking of other enterprise beans.
Point-to-point Introduction interfaces In this section, we'll look at each of the important JMS interfaces for point-to-point programming and some of their methods. In the next section Point-to-point code that performs point-to-point programming on page 17we'll look at some sample message processing. It contains a createqueueconnection method which returns a QueueConnection object.
QueueConnection QueueConnection encapsulates an active connection to a provider. Some of its methods are: Returns a QueueSession object. The boolean parameter indicates whether the QueueSession is transacted or not; the int indicates the acknowledgement mode see Acknowledgement on page Activates the delivery of messages from the provider.
Temporarily stops delivery of messages; delivery can be restarted with start. Closes the connection to the provider and releases all resources held in its behalf. Returns a QueueSender object to send messages to the specified Queue. Returns a QueueReceiver object to receive messages from the specified Queue. Returns a QueueBrowser object to browse messages on the specified Queue.
Commits all consumed or produced messages for the current transaction. Rolls back all consumed or produced messages for the current transaction.
It is an administered object that is QueueSender QueueSender is used to send point-to-point messages.
Sams Teach Yourself EJB in 21 Days - PDF Free Download
Sends the indicated Message. Sets the delivery mode for subsequent messages sent; valid values are DeliveryMode. Sets the priority for subsequent messages sent; valid values are 0 through 9. You can call the receive method, which waits for messages, or you can create a listener object that receives messages when they become available.
Notice that most of the setup is the same as in Listing You tell the QueueReceiver about your message listener and it will automatically let you know when a message comes in. Although JMS allows you to have multiple receivers on a queue, it is up to the individual implementations to decide how to handle multiple receivers.
Some might distribute the messages evenly across all receivers, and others might just send all the messages to the first receiver you create. Note Because sessions are single-threaded, you can only process one message at a time. If you need to process multiple messages concurrently, you must create multiple sessions.
Publishing Messages Although the overall concept of publish-subscribe is a bit different from point-to-point messages, the JMS calls for creating a topic and publishing messages are remarkably similar to the calls for sending queue messages. In fact, if you go through the program in Listing You must also change the createSender call to createPublisher. As with queues, each topic must have a unique name.
- Introducing the Java Message Service
Unlike queues, in which you only have one receiver, you normally have many subscribers to a topic. You can also have multiple publishers. Your clients will just see more messages than they do if only one copy is running.
Sams Teach Yourself EJB in 21 Days
Subscribing to Topics Subscribing to a topic is just as easy as listening on a queue. Again, you can just about convert the program in Listing The actual listener implementation for topics is identical to the one for queues. Durable Subscriptions Most pub-sub systems you see today deal with frequently published data and tend to provide real-time monitoring and status updates.
Pub-sub is good for these kinds of operations. Many pub-sub implementations have a peculiar limitation that makes them only suitable for applications with frequently published data-more specifically, applications in which it doesn't matter if you miss a message because there will be another one in a few minutes. The reason pub-sub usually only works in these types of applications is that pub-sub doesn't usually have the concept of a persistent, or durable subscription.
For example, suppose you are publishing flight cancellations for LaGuardia Airport. Although it's true that they do seem to occur often enough that there will be another one in a few minutes, you really don't want to miss one. Now, suppose you have a program that makes a list of passengers who need to be rebooked for the next available flight tomorrow, next week, sometime next year, and so on.
If the program shuts down for a few minutes, it might miss three or four cancel lations! You want the message server to hold on to your messages while your program is down. JMS supports durable subscription.
After you create a durable subscription, the JMS server keeps any messages you miss while your program isn't running. To create a durable subscription, use the createDurableSubscriber method in the TopicSession class: That is, if you call the subscription RebookFlightCx that's the subscription name, not the topic nameyou must always ask for RebookFlightCx when resuming that subscription.
Keep in mind that durable connections are expensive for the server to maintain, so only use them when absolutely necessary.