Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using the JMS API and XML in Content-Based Routing

By Todd Sundsted
2003-12-20


The code

Let's close by taking a look at parts of the code for the agent and the router classes. The complete source is available for download (see Resources).

The following code contains the functional component of the router class.

Router class



public
void
start()
throws JMSException {
// Start the first service. This service listens to the control
// queue. When it receives a control messages, it invokes the
// handle() method. The handle() method creates a new rule
// comprised of the sender's destination and the XPath
// expression.
startService(
m_queueconnection,
m_queueControl,
new MessageHandler() {
public
void
handle(TextMessage textmessage)
throws Exception {
synchronized (m_setRules) {
String string = textmessage.getText();
Destination destination = textmessage.getJMSReplyTo();
m_setRules.add(new Rule(destination, string));
}
}
}
);
// Start the second service. This service listens to the data
// queue. When it receives a data message, it invokes the
// handle() method. The handle() method converts the XML document
// into a DOM tree and then applies each XPath expression in the
// rule base to the DOM tree. If any are satisfied, the method
// sends the message to the appropriate queue.
startService(
m_queueconnection,
m_queueData,
new MessageHandler() {
public
void
handle(TextMessage textmessage)
throws Exception {
String string = textmessage.getText();
InputSource inputsource = new InputSource(string);
DOMParser domparser = new DOMParser();
domparser.parse(inputsource);
Node node = domparser.getDocument().getDocumentElement();
Iterator iterator = null;
synchronized (m_setRules) {
iterator = new java.util.LinkedList(m_setRules).iterator();
}
while (iterator.hasNext()) {
Rule rule = (Rule)iterator.next();
NodeList nodelist = XPathAPI.selectNodeList(node, rule.stringXPath);
if (nodelist.getLength() > 0) {
textmessage.setJMSReplyTo(m_queueControl);
QueueSession queuesession = null;
queuesession = m_queueconnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender queuesender = null;
queuesender = queuesession.createSender(m_queueControl);
queuesender.send(textmessage);
queuesender.close();
queuesession.close();
}
}
}
}
);
}

Routers listen for JMS messages on two queues: a control queue, which is used by agents to register themselves and their interests with the router, and a data queue, which is used by harvesters to send messages to the router.

The StartService() method above is called by the router's main() method after the JMS layer has been initialized. It starts two services: one that manages the control queue and one that manages the data queue.

The following code contains the functional component of the agent class.

Agent class



public
void
start(String stringXPath)
throws JMSException {
// Send a message to the control queue of the Router with an XPath
// expression.
QueueSession queuesession = null;
queuesession = m_queueconnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender queuesender = null;
queuesender = queuesession.createSender(m_queueRouterControl);
TextMessage textmessage = null;
textmessage = queuesession.createTextMessage(stringXPath);
textmessage.setJMSReplyTo(m_queueAgentData);
queuesender.send(textmessage);
queuesender.close();
queuesession.close();
// Start the service. This service listens to the data
// queue. When it receives a data message, it invokes the
// handle() method. The handle() method simply display the
// message for now.
startService(
m_queueconnection,
m_queueAgentData,
new MessageHandler() {
public
void
handle(TextMessage textmessage)
throws Exception {
System.out.println(textmessage.getText());
}
}
);
}

Agents are pretty simple. The method above is at the heart of an agent. It is called by the class's main() method after the basic JMS layer has been initialized. When an agent is started, it must be given a rule template, in the form of an XPath expression.



Tutorial Pages:
» How to make sure information gets to the right people
» Messaging and the JMS API
» A simple content-based routing system with agents
» XML and XPath
» The code
» Conclusion
» Resources


IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT