Java Reference
In-Depth Information
The two methods declared by this interface represent the two macros you have
defined for the MBean. The
turnOnHomeTheater()
method makes the MBean
publish the messages that cause the sound system to activate and the projector
screen to drop. The
turnOffHomeTheater()
method sends the messages to deacti-
vate the sound system and raise the screen. These two methods grant manage-
ment applications the ability to invoke their macros without the use of messages
from the lighting application (remote control).
Listing 13.2 shows the class for the controller MBean. It implements the
JMX-
ControllerMBean
interface as well as the
MessageListener
interface from the
javax.jmx
package. The
MessageListener
interface declares the
onMessage()
method that is invoked when a message is sent to the topic to which the MBean
is subscribed (
controlMessages
).
Listing 13.2
JMSController.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;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.jms.Topic;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.JMSException;
public class JMSController implements MessageListener,
JMSControllerMBean
{
private TopicConnection topicConnection=null;
private TopicSession topicSession=null;
private TopicSubscriber topicSubscriber=null;
private Topic topic=null;
private TopicConnectionFactory topicFactory=null;
private int count_=0;
private Context context=null;
B
public JMSController() throws JMSException, NamingException
{
String factoryJNDI="TopicConnectionFactory";
String topicJNDI="topic/controlMessages";