Java Message Service

Java Message Service ( JMS) is out a programming interface ( API) for the activation of a Message Oriented Middleware ( MOM) for sending and receiving messages from a client that is written in the Java programming language. JMS has the goal of loosely coupled, to allow reliable and asynchronous communication between components of a distributed application.

JMS is part of the Java Platform, Enterprise Edition, the specification of the service and the associated API has been standardized by the Java Community Process as part of JSR 914. The current version of JMS is the version 2.0 of 21 May 2013, and is part of the Java Enterprise Edition 7.0.

For the application you need a provider that implements the API, thus providing the service. There are both commercial products and open source projects.

Operation

Messaging is a way of loosely coupled distributed communication and in the form of messages sent between software components. Messaging tries to break the otherwise tight coupling of other communication facilities such as TCP communication via sockets, CORBA or RMI by introducing a located between the client component. This ensures that the clients no closer knowledge of the remote site ( s) must have their communication, which increases both the range of use as well as the maintenance and reuse of components.

JMS and driven by these service support two different approaches for sending messages to a message queue (English queue) for so-called point-to -point connections and on the other a sign- dispatch system ( engl. topic) for publish-subscribe communication:

  • In the queue of the transmitter sends to a queue at the hanging one or more recipients. If no receiver available, the message can optionally be stored and potential recipients, they can always pick it up later. In the event only one receiver, one can best compare this with a parcel service. Each shipment has exactly one receiver. Is this not at home, he can pick up at any time later the shipment. For multiple recipients of the messages is ensured in the service that each queued message is assigned exactly once. This makes it possible to realize load balancing, can be added and removed as desired at the receiver.
  • In the sign- dispatch system, the messages are sent to a topic, listening to any number of recipients. If the message is not consumed, because no receiver has logged on to the topic, then this is irrelevant. This can be best compared to a television ( Broadcasting). Either you turn on the television and see the news or letting the TV and not see the message. Optionally, the messages can also be cached ( durable - subscription ).

Implementation

To send messages or to receive first a queue or a topic to be determined over which the communication takes place. This is usually achieved by a JNDI lookup:

Context ctx = new InitialContext ( ); QueueConnectionFactory factory = ( QueueConnectionFactory ) ctx.lookup ( " QueueConnectionFactory "); MyQueue queue = ( Queue ) ctx.lookup ( " MyQueue "); Subsequently, a connection is produced, on which a session is started. Then a transmitter or a receiver is then, depending on whether is sent or received, opened, can be sent or received via the Messages:

Queue Connection connection = factory.createQueueConnection (); Queue Session session = connection.createQueueSession (false, Session.AUTO_ACKNOWLEDGE ); Queue Sender sender = session.createSender ( myQueue ); Text Message message = session.createTextMessage (); sender.send ( message); or

... QueueReceiver receiver = session.createReceiver ( myQueue ); connection.start (); Message message = receiver.receive (); Sending and receiving via Topic is similar to sending and receiving via queue. Other classes are on the one hand used ( Topic Session, TopicConnection, TopicPublisher, TopicSubscriber, topic ), on the other hand one gets when receiving a message via Topic this from not using receiver.receive (), but implements a MessageListener.onMessage (message) event handler, the receives messages via message events.

The different queues and topics on mailable message types are as follows:

  • Message - message with no content (Body )
  • Stream Message - message with a stream of Java primitives
  • MapMessage - message with a map of Java objects
  • Text Message - message with a string (eg for XML messages )
  • Object Message - message with a serialized Java object
  • Bytes Message - message with a stream of bytes

JMS provider

To use JMS, a JMS provider is required that manages the topics, queues and sessions. The following list outlines the JMS provider on. She calls both commercial and free software, but does not claim to be complete.

However, not included are those JMS providers that are offered exclusively as part of a Java EE container (Java Application Server ). An overview of Java EE containers is available separately.

The table below shows the information contained in the "Operating Modes " column mean the following:

Modern JMS providers allow both modes of operation.

432199
de