MixedMessagingClientpublic class MixedMessagingClient extends Object implements RunnableMixedMessagingClient: A JMS messaging client that uses the generic
messaging aspects of the API, introduced in JMS 1.1. This client
can use either point-to-point messaging using Queues, or
publish-subscribe messaging using Topics. |
Fields Summary |
---|
private javax.jms.Connection | mConn | private javax.jms.Destination | mDestination | private javax.jms.MessageConsumer | mConsumer | private javax.jms.Session | mSession | private static String | MSG_TYPE |
Constructors Summary |
---|
public MixedMessagingClient(String cFactoryJNDIName, String queueJNDIName)
// Constructor, with client name, and the JNDI locations of the JMS
// connection factory and queue that we want to use.
init(cFactoryJNDIName, queueJNDIName);
|
Methods Summary |
---|
protected boolean | init(java.lang.String cFactoryJNDIName, java.lang.String queueJNDIName)
boolean success = true;
Context ctx = null;
// Attempt to make connection to JNDI service
try {
ctx = new InitialContext();
}
catch (NamingException ne) {
System.out.println("Failed to connect to JNDI provider:");
ne.printStackTrace();
success = false;
}
// If no JNDI context, bail out here
if (ctx == null) {
return success;
}
// Attempt to lookup JMS connection factory from JNDI service
ConnectionFactory connFactory = null;
try {
connFactory = (ConnectionFactory)ctx.lookup(cFactoryJNDIName);
System.out.println("Got JMS connection factory.");
}
catch (NamingException ne2) {
System.out.println("Failed to get JMS connection factory: ");
ne2.printStackTrace();
success = false;
}
try {
// Make a connection to the JMS provider and keep it.
// At this point, the connection is not started, so we aren't
// receiving any messages.
mConn = connFactory.createConnection();
// Try to find our designated queue
mDestination = (Queue)ctx.lookup(queueJNDIName);
// Make a session for queueing messages: no transactions,
// auto-acknowledge
mSession =
mConn.createSession(false,
javax.jms.Session.AUTO_ACKNOWLEDGE);
}
catch (JMSException e) {
System.out.println("Failed to establish connection/queue:");
e.printStackTrace();
success = false;
}
catch (NamingException ne) {
System.out.println("JNDI Error looking up factory or queue:");
ne.printStackTrace();
success = false;
}
try {
// Make our receiver, for incoming messages.
// Set the message selector to only receive our type of messages,
// in case the same queue is being used for other purposes.
mConsumer = mSession.createConsumer(mDestination,
"JMSType = '" + MSG_TYPE + "'");
}
catch (JMSException je) {
System.out.println("Error establishing message receiver:");
je.printStackTrace();
}
return success;
| public static void | main(java.lang.String[] args)
if (args.length < 3) {
System.out.println("Usage: MixedMessagingClient" +
" connFactoryName destName" +
" [send|listen|recv_synch] <messageToSend>");
System.exit(1);
}
// Get the JNDI names of the connection factory and
// destination, from the command-line
String factoryName = args[0];
String destName = args[1];
// Get the command to execute (send, recv, recv_synch)
String cmd = args[2];
// Create and initialize the messaging participant
MixedMessagingClient msger =
new MixedMessagingClient(factoryName, destName);
// Run the participant in its own thread, so that it can react to
// incoming messages
Thread listen = new Thread(msger);
listen.start();
// Send a message to the queue
if (cmd.equals("send")) {
String msg = args[3];
msger.sendMessage(msg);
System.exit(0);
}
// Register a listener
else if (cmd.equals("listen")) {
MessageListener listener = new TextLogger();
msger.registerListener(listener);
System.out.println("Client listening to destination " + destName
+ ". . .");
System.out.flush();
try { listen.wait(); } catch (Exception we) {}
}
// Synchronously receive a message from the queue
else if (cmd.equals("recv_synch")) {
String msg = msger.receiveMessage();
System.out.println("Received message: " + msg);
System.exit(0);
}
| public java.lang.String | receiveMessage()
String msg = "-- No message --";
try {
// (Re)start the connection, in case it's not already started
mConn.start();
// Check for a message
Message m = mConsumer.receive();
if (m instanceof TextMessage) {
msg = ((TextMessage)m).getText();
}
else {
msg = "-- Unsupported message type received --";
}
}
catch (JMSException je) {
}
return msg;
| public void | registerListener(javax.jms.MessageListener listener)
try {
// Set the listener on the receiver
mConsumer.setMessageListener(listener);
// Start the connection, in case it's still stopped
mConn.start();
}
catch (JMSException je) {
System.out.println("Error registering listener: ");
je.printStackTrace();
}
| public void | run()
while (true) {
try { this.wait(); } catch (Exception we) {}
}
| public void | sendMessage(java.lang.String msg)
try {
// Create a JMS msg sender connected to the destination queue
MessageProducer producer = mSession.createProducer(mDestination);
// Use the session to create a text message
TextMessage tMsg = mSession.createTextMessage();
tMsg.setJMSType(MSG_TYPE);
// Set the body of the message
tMsg.setText(msg);
// Send the message using the sender
producer.send(tMsg);
System.out.println("Sent the message");
}
catch (JMSException je) {
System.out.println("Error sending message " + msg + " to queue");
je.printStackTrace();
}
|
|