Java Reference
In-Depth Information
received. At this point, the MBean is ready to receive messages from the
JMS
message bus.
The
onMessage()
method handles messages as they are published to the MBean.
Remember that this MBean is interested in only two messages. If either the
MOVIE-
LIGHTSOn
or
MOVIELIGHTSOff
message is received, the MBean will execute a macro.
If any other message is received, the MBean will print a message indicating that
it is not interested. To complete a macro, the MBean will invoke its
publishMes-
sages()
method, passing in the two messages to be sent.
The
publishMessages()
method uses the
JMS
bus to send device control messages.
In this method, the MBean looks up the topic in which it will publish messages.
After the topic lookup, it creates a
topicPublisher
so that it can publish the mes-
sage. After acquiring the
topicPublisher
, it needs to create a message to publish.
In this case, it will only be using a text message returned from a call to the
Topic-
Session
object. Before publishing the message, it sets the value of the text mes-
sage using the
setText()
method. After it has the topic, message, and publisher,
it can publish the message. Using the
publish()
method in the
topicPublisher
,
the MBean publishes both messages to the
deviceMessages
topic.
Finally, the MBean implements the two methods exposed by its MBean interface.
These methods are directly accessible from the management system by connect-
ing the
JMX
agent containing the MBean. If you want to turn on or off the home
theater system, you can do so from a management application. These methods
will be useful for testing or diagnosing problems with the message bus.
C
D
E
Constructing the debugging device message listener
(the IR signal transmitter)
Now it is time to write the subscriber class that simulates the
IR
signal transmit-
ter. This class, shown in listing 13.3, subscribes to the
deviceMessages
topic and
prints to the screen when it receives messages. This class looks very similar to the
previous MBean class. It will be executed from the command line.
Listing 13.3
DebugSubscriber.java
package jmxbook.ch13;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.jms.TopicPublisher;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;