Java Reference
In-Depth Information
// OBS! For the message listener to receive any messages
// the connection has to be started
topicConnection.start();
}
public void onMessage(Message m) {
try {
String msg = ((TextMessage)m).getText();
System.out.println("DeviceSubscriber got message: " + msg);
}
catch(Exception ex) {
System.err.println("Device Could not handle message: " + ex);
ex.printStackTrace();
}
}
public void close() throws JMSException {
topicSession.close();
topicConnection.close();
}
public static void main(String[] args){
DebugSubscriber subscriber=null;
try{
System.out.println("Starting Debugging Subscriber");
subscriber=new DebugSubscriber();
}
catch(Exception e){
System.out.println("Error Starting Device DebugClient");
e.printStackTrace();
}
}
}
This class operates similarly to the MBean you just wrote. It looks up a particular
topic, subscribes to it, and begins to listen for applicable messages. When a mes-
sage is received, the onMessage() method from the MessageListener interface is
invoked. If this class represented an actual device controller for the example, the
onMessage() method would contain the code for communicating the messages to
the appropriate devices. In this case, the method simply prints the message to
standard out. In addition, this class's main() method creates an instance of the
class and prints an error if the subscription fails.
Search WWH ::




Custom Search