Point-To-Point (queue) programming

As discussed in the previous section, interfaces that extend the core JMS interfaces of Queue help build Point-To-Point components.

Please remember that to be able to execute the following example programs, you need the message queue to have been set up.

The following is a sample program to send messages to the Point-To-Point Queue:

package pointtopoint;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

/**
* Program to send messages to the queue
* @author Raja
*
*/
public class QueueMessageSender {
public static void main(String[] args) {
QueueMessageSender messageSender = new QueueMessageSender();
messageSender.enqueueMessage();
}

public void enqueueMessage() {
BufferedReader inlineReader = new BufferedReader(
new InputStreamReader(System.in));
try {

// Prompt for the JNDI Queue connection factory name
System.out.println("Enter the Queue Connection Factory name:");
String queueConnFactoryName = inlineReader.readLine();
System.out.println("Enter the Queue name:");
String queueName = inlineReader.readLine();

// Look up for the administered objects of the Queue
InitialContext context = new InitialContext();
QueueConnectionFactory queueConnFactory =
(QueueConnectionFactory)
context.lookup(queueConnFactoryName);
Queue queueReference = (Queue) context.lookup(queueName);
context.close();

// Create the JMS objects from administered objects
QueueConnection queueConnection =
queueConnFactory.createQueueConnection();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender =
queueSession.createSender(queueReference);

// Enqueue multiple text messages entered one after the other
String messageContent = null;
while (true) {
System.out.println("Enter the new message to send or 'abandon'
to exit the program:");
messageContent = inlineReader.readLine();
if ("abandon".equals(messageContent))
break;
TextMessage textMessage =
queueSession.createTextMessage(messageContent);
queueSender.send(textMessage);
}

// Clean Up
System.out.println("Messages Successfully
posted to the queue...");
inlineReader.close();
queueConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

While the preceding program can send messages to the Point-To-Point queue, the following program will keep receiving messages from the queue until the stopReceivingMessages command is received:

package pointtopoint;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

/**
* Program to receive messages from the queue
* @author Raja
*
*/
public class QueueMessageReceiver implements MessageListener {
private boolean stopReceivingMessages = false;

public static void main(String[] args) {
QueueMessageReceiver queueMessageReceiver =
new QueueMessageReceiver();
queueMessageReceiver.startReceivingMessages();
}

public void startReceivingMessages() {
BufferedReader inlineReader = new BufferedReader(
new InputStreamReader(System.in));
try {

// Prompt for the JNDI Queue connection factory name
System.out.println("Enter the Queue Connection Factory name:");
String queueConnFactoryName = inlineReader.readLine();
System.out.println("Enter the Queue name:");
String queueName = inlineReader.readLine();

// Look up for the administered objects of the Queue
InitialContext context = new InitialContext();
QueueConnectionFactory queueConnFactory =
(QueueConnectionFactory)
context.lookup(queueConnFactoryName);
Queue queueReference = (Queue) context.lookup(queueName);
context.close();

// Create the JMS objects from administered objects
QueueConnection queueConnection =
queueConnFactory.createQueueConnection();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueReceiver queueMessageReceiver =
queueSession.createReceiver(queueReference);
queueMessageReceiver.setMessageListener(this);
queueConnection.start();

// Keep receiving the messages from the queue until the stop
// receiving messages command is received
while (!stopReceivingMessages) {
Thread.sleep(1000);
}

// Clean Up
System.out.println("Messages successfully received so far,
Stop receiving messages!");
queueConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void onMessage(Message message) {
try {
String messageContent = ((TextMessage) message).getText();
System.out.println(messageContent);
if ("stop".equals(messageContent))
stopReceivingMessages = true;
} catch (JMSException e) {
e.printStackTrace();
stopReceivingMessages = true;
}
}

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset