I had taken a lab course with this department. Without the lab assistant, I would not make it through. I would like to summarize a part of my lab work here related with JMS, so I will not forget. Full lab report here.
Following is the general architecture of the system. JMS is used for communication between the NodeController and a Node Client.

The Overview Architecture of the Job Spooling System
I was doing a distributed application in Java. Part of the lab is sending a message from an EJB (version 3) to a client application on a different machine using JMS (Java Message Service). First, we have to create a JMS Topic which is kind of a channel that we send can send a message too. Naming this technology with the word “Topic” is not a good idea, because this word is a common name. Thus, it is difficult to search for examples in Google.
I was using JBoss 4. I had to create a JMS Topic in the JBoss first by going to the JMX console of JBoss. I assume that you know how to install JBoss, and develop EJB 3.0 applications.
1. Go to JBoss homepage at http://localhost:8080. The URL could be something else that you have set.
2. Go to JMX console by clicking at JMX Console under JBoss management.
3. Go to DestinationManager under jboss.mq.
4. Create a JMS topic named “pomtopic” by invoking the createTopic function. Supply param p1 and p2 with the value “pomtopic,” and then click “Invoke” as shown in the figure below. The “pomtopic” topic will be
created. Java application can send message to and receive message from this JMS topic.
I am not sure why there are two paramters (p1 and p2). Just try to use the same name. The name could be something else. The name will be referenced from the codes.
Following is the sender code of the JMS Topic. The sender code is in the EJB 3.0. Some variables are declared and instantiated automatically though dependency injection technique.
==========sender=============================
……
// Connection factory for JMS Topic communication
@Resource(mappedName=”TopicConnectionFactory”)
private ConnectionFactory connectionFactory;
//For JMS Topic communication, the name of JMS Topic is fixed to “pomtopic”.
//This topic must be created in JBoss JMX console before using, otherwise, it will throw an exception.
@Resource(mappedName=”pomtopic”)
private Topic topic;
// This function will send the message to the JMS topic.
//Derived from the JMS tutorial from Sun website
// @param msg represents the message that this function will send to the JMS topic which node client is //listening to.
//@return result of sending, True for successfully. False for when there is an exception occurred.
public boolean sendMessageToTopic(String msg)
{
try {
Connection connect = connectionFactory.createConnection( );
Session session = connect.createSession(true,0);
MessageProducer producer = session.createProducer(topic);
TextMessage textMsg = session.createTextMessage( );
textMsg.setText(msg);
producer.send(textMsg);
connect.close( );
} catch (JMSException ex) {
logger.debug(ex.getMessage());
return false;
}
return true;
}
….
==================================================
Next is the client code who is waiting for message. First, you need to create a connection to the JMS Topic first. The Java function below demonstrates the connection.
=====client : function for creating a JMS Topic connection=============
public void connectByTopic() {
String topicName = null;
Context jndiContext = null;
TopicConnectionFactory topicConnectionFactory = null;
TopicConnection topicConnection = null;
TopicSession topicSession = null;
Topic topic = null;
TopicSubscriber topicSubscriber = null;
/* Part of Code from Sun
* Create a JNDI API InitialContext object if none exists
* yet.
*/
try {
jndiContext =getInitialContext();// new InitialContext();
} catch (NamingException e) {
System.out.println(“Could not create JNDI API ” +
“context: ” + e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Look up connection factory and topic. If either does
* not exist, exit.
*/
try {
topicConnectionFactory = (TopicConnectionFactory)
jndiContext.lookup(“TopicConnectionFactory”);
topic = (Topic) jndiContext.lookup(“pomtopic”);
} catch (NamingException e) {
System.out.println(“JNDI API lookup failed: ” +
e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create subscriber.
* Register message listener (TextListener).
* Receive text messages from topic.
* When all messages have been received, enter Q to quit.
* Close connection.
*/
try {
topicConnection = topicConnectionFactory.createTopicConnection();
topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
topicSubscriber = topicSession.createSubscriber(topic);
topicSubscriber.setMessageListener(this);
topicConnection.start();
} catch (JMSException e) {
System.out.println(“Exception occurred: ” +
e.toString());
} finally {
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {}
}
}
}
==================================================
After creating a JMS Topic connection, then the Java client application needs to listen for a message.
=========Client : function for listening a JMS Topic message==========
public void onMessage(Message message) {
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
System.out.println(“Reading message: ” +
msg.getText());
//the following function will process the message sent from the server
processMessageAndExecute(msg.getText());
} else {
System.out.println(“Message of wrong type: ” +
message.getClass().getName());
}
} catch (JMSException e) {
System.out.println(“JMSException in onMessage(): ” +
e.toString());
} catch (Throwable t) {
System.out.println(“Exception in onMessage():” +
t.getMessage());
}
}
=================================================
