Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

JMS and MSMQ

.pdf
Скачиваний:
11
Добавлен:
17.02.2016
Размер:
198.38 Кб
Скачать

Enterprise Level Applications

Course # EGR348

Lecture #11

KBTU, Almaty

Messaging construction and service

1.

Java Message Service (JMS)

2.

Microsoft Message Queuing (MSMQ).

 

KBTU, Almaty

KBTU, Almaty

Messaging construction and service

The JMS is, in effect, a standard. It is not a messaging system, but rather a standard application programming interface (API) for messaging systems. A first version of the JMS standard was released in 2001. Basically, the JMS standard is intended to provide a common interface through which (Java-based) applications can interact with a messaging system, regardless of its underlying implementation.

JMS provides two distinct types of messaging: queues and topics. These are referred to in the JMS standard as types of destinations. Queues are intended for point-to-point (one-to-one) communication between applications, while topics provide the concept of publish/subscribe where one application may send a message to several other applications (one-to-many). In the parlance of JMS, applications that send messages are called producers and applications that receive messages are called consumers. Producers and consumers are also referred to as clients, while the messaging system that implements the JMS interface is called JMS provider.

JMS

In point-to-point communication, applications make use of queues, where only one consumer receives each message. The consumer may or may not be online, so a message may remain in the queue for a relatively long period of time until it is eventually received by the consumer. JMS messages may have attributes such as expiry time or time-to-live that determine the maximum time that the message should be in the queue without being received. Also, all messages in JMS are acknowledged upon receipt. This receipt may be implicit or explicit. In publish/subscribe communication, application use topics, for which there may be multiple consumers. Each consumer receives a copy of the message that arrives at the topic. However, the default behavior here is that a consumer receives the message only if it is online and has an active subscription to the topic; if the consumer goes offline or deactivates the subscription, the default behavior of JMS is to stop delivering messages to that consumer.

KBTU, Almaty

JMS

The JMS API defines a set of Java class interfaces, with predefined methods, that the messaging system must implement in its own set of classes. For example, the MessageProducer interface defines the signature of the method send() that is used to dispatch a message to a given destination; for that purpose, the method send() has a parameter of type Message that is used to refer to the message to be sent. On

its turn, Message is another interface that specifies the methods to get and set the properties and content of a message.

The use of the JMS API begins by obtaining a reference to an object that implements the ConnectionFactory interface. Such object must have been previously created and must be readily accessible for applications to initiate connections to the messaging system: createConnection() method.

KBTU, Almaty

JMS

Fig.1. JMS interfaces.

KBTU, Almaty

JMS

Typically, applications will implement the following behavior:

1.Use JNDI to retrieve an object that implements the ConnectionFactory interface.

2.Use the ConnectionFactory object to create a Connection object. This opens a connection to the messaging system but, at this stage, message delivery is still disabled.

3.Use the Connection object to create one or more Session objects.

4.Use JNDI again to lookup one or more references to Destination objects.

5.Use the Session object and the Destination references to create MessageProducer and MessageConsumer objects.

6.To send messages, use Session to create a Message object, and use a MessageProducer to send the message. To receive messages, initiate the delivery of messages on the Connection and use a MessageConsumer to receive the message.

KBTU, Almaty

JMS

// to send a text message using JMS import javax.naming. ;

import javax.jms. ;

Context jndiContext = new InitialContext(); QueueConnectionFactory queueConnectionFactory =

(QueueConnectionFactory) jndiContext.lookup("MyConnectionFactory"); QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

QueueSession queueSession =

queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = (Queue) jndiContext.lookup("MyQueue");

QueueSender queueSender = queueSession.createSender(queue); Message message = queueSession.createTextMessage(); message.setText("This is a text message.");

queueSender.send(message);

KBTU, Almaty

JMS

// to receive a text message using JMS import javax.naming. ;

import javax.jms. ;

Context jndiContext = new InitialContext(); QueueConnectionFactory queueConnectionFactory =

(QueueConnectionFactory) jndiContext.lookup("MyConnectionFactory"); QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

QueueSession queueSession =

queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = (Queue) jndiContext.lookup("MyQueue");

QueueReceiver queueReceiver = queueSession.createReceiver(queue); queueConnection.start();

Message message = (TextMessage) queueReceiver.receive(); String text = message.getText();

KBTU, Almaty

JMS

//Implementation of callback interface to receive messages asynchronously class TextListener implements MessageListener

{

public void onMessage(Message message)

{

TextMessage message = (TextMessage) message; String text = message.getText();

}

}

//Application code to receive message asynchronously

QueueReceiver queueReceiver = queueSession.createReceiver(queue); TextListener textListener = new TextListener(); queueReceiver.setMessageListener(textListener); queueConnection.start();

Here, the onMessage() method simply converts the input message to an appropriate class, and then retrieves the message content as before.

KBTU, Almaty

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]