Java Reference
In-Depth Information
catch(Exception ex)
{
System.err.println("Could not handle message: " + ex);
ex.printStackTrace();
}
}
D
public void close() throws JMSException {
topicSession.close();
topicConnection.close();
}
public void turnOnHomeTheater()
{
System.out.println("Turning On Home Theater System");
publishMessages("SurroundOn","ScreenDown");
}
E
Expose
macro
methods
public void turnOffHomeTheater()
{
System.out.println("Turning Off Home Theater System");
publishMessages("SurroundOff","ScreenUp");
}
}
B
In the constructor, the MBean attempts to subscribe to the
controlMessages
topic
in the
JMS
container. The first step to do this is to get the Java Naming and Direc-
tory Interface (
JNDI
) context in order to access the classes required to connect to
the
JMS
messaging provider. After getting a
Context
object, the MBean looks up
an instance of the
TopicConnectionFactory
class for the
controlMessages
topic.
From the topic factory, the MBean creates a topic connection, which it uses to
create a
TopicSession
object. The session will allow the MBean to receive messages
on the topic in which it is interested. In this example, it is listening for
MOVIE-
LIGHTSOn
and
MOVIELIGHTSOff
.
After it creates a topic session, the MBean looks up the actual
Topic
object using
its
Context
instance and
JNDI
name. After acquiring the
Topic
instance, the MBean
creates a
TopicSubscriber
from its
TopicSession
instance. The MBean tells the
subscriber which class to call back when
JMS
messages arrive. In this case, the class
to be called is the MBean itself. The MBean uses the
setMessageListener()
method
of the subscriber to indicate this. When messages arrive to the subscriber, it will
invoke the
onMessages()
method implemented from the
MessageListener
interface.
After the connections are established and the subscriber is configured, the
connection must be started using the
start()
method in order to have messages