Chapter 23
888
The send() method in this example first sets the message text, then sends the message:
public void send(String msg) throws JMSException
{
// Set the message's text
message.setText(msg);
The queueSender.send(Message message) method sends a message to the Sender's queue. The
message parameter contains the message to be sent to the destination. In this case the destination is
MyJMSQueue. Only one client that subscribes to MyJMSQueue will receive the message:
// Send the message
queueSender.send(message);
}
/**
* Closes JMS connection objects.
*/
public void close() throws JMSException
{
queueSender.close();
queueSession.close();
queueConnection.close();
}
/**
* Writes messages to the queue until the user enters 'q' or 'Q'.
*/
public static void main(String[] args) throws Exception
{
// Create a sender.
Sender sender = new Sender();
// Send user's messages until user enters an 'q' or 'Q'.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
do
{
// Get the message
System.out.print("Enter message ('q' or 'Q' quits): ");
msg = in.readLine();
// Publish message if it isn't empty.
if(msg.trim().length() != 0)
{
sender.send(msg);
}
}while(!msg.equalsIgnoreCase("q"));
// Close up the JMS connections
sender.close();
}
}
As you can see, this example is very similar to the Publisher from the Pub/Sub example above.