|
Single Control Center
Listing 5. The AbstractApplicationMonitorAdministrator completes the Application Monitor Server system by providing the capability to view and control processes remotely.
package Adminstrator;
import Server.ClientOb;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.jms.*;
import javax.naming.*;
import javax.xml.parsers.
ParserConfigurationException;
import Util.XMLMessageUtil;
/**
* The AbstractApplicationMonitorAdminstrator is
* used to receive remote client status
* information from the ApplicationMonitorServer.
* It can also send messages to the
* ApplicationMonitorServer.
* All communication is accomplished via Java
* Messaging Service.
*/
public abstract class
AbstractApplicationMonitorAdministrator
implements ActionListener
{
private TopicPublisher commandTopicPublisher;
// JMS publisher used to publish
// ApplicationMonitorCommand messages
private TopicPublisher syncTopicPublisher;
// JMS publisher used to publish
// ApplicationMonitorSync messages
private TopicSession topicSession;
// JMS connected session
private UpdateListener updateListener;
// Object that listens for JMS
// ApplicationMonitorUpdate messages
private long lastAliveTime =
System.currentTimeMillis();
// Timestamp of last ApplicationMonitorAlive
// message received
/**
* Constructor. Connects to the JMS messaging
* server, retrieves all necessary topics, sets
* up the appropriate topic publishers and
* subscribers, and initializes a timer that
* checks for "ApplicationMonitorAlive"
* messages.
*/
public
AbstractApplicationMonitorAdministrator()
{
Context jndiContext = null;
TopicConnectionFactory
topicConnectionFactory = null;
// Creates needed JMS topics
TopicConnection topicConnection = null;
// Connects to the JMS server
Topic syncTopic = null;
// Topic used to publish
// "ApplicationMonitorSync" messages
Topic updateTopic = null;
// Topic used to subscribe to
// "ApplicationMonitorUpdate" messages
Topic commandTopic = null;
// Topic used to publish
// "ApplicationMonitorCommand" messages
Topic resyncTopic = null;
// Topic used to subscribe to
// "ApplicationMonitorReSync" messages
Topic aliveTopic = null;
// Topic used to subscribe to
// "ApplicationMonitorAlive" messages
TopicSubscriber updateTopicSubscriber =
null;
// Subscriber of "
// ApplicationMonitorUpdate" messages
TopicSubscriber resyncTopicSubscriber =
null;
// Subscriber of
// "ApplicationMonitorReSync" messages
TopicSubscriber aliveTopicSubscriber = null;
// Subscriber of "ApplicationMonitorAlive"
// messages
// Create a JNDI API InitialContext object
try
{
jndiContext = new InitialContext();
}
catch (NamingException e)
{
System.out.println(
"Could not create JNDI API context: " +
e.toString());
System.exit(1);
}
// Look up connection factory and topic.
// If either does not exist, exit.
try
{
topicConnectionFactory =
(TopicConnectionFactory)jndiContext.
lookup("TopicConnectionFactory");
updateTopic =
(Topic) jndiContext.lookup(
"ApplicationMonitorUpdate");
syncTopic = (Topic)jndiContext.lookup(
"ApplicationMonitorSync");
commandTopic = (Topic)jndiContext.lookup(
"ApplicationMonitorCommand");
resyncTopic = (Topic)jndiContext.lookup(
"ApplicationMonitorReSync");
aliveTopic = (Topic)jndiContext.lookup(
"ApplicationMonitorAlive");
}
catch (NamingException e)
{
System.out.println(
"JNDI API lookup failed: " +
e.toString());
System.exit(1);
}
try
{
// Create a topic connection and session
topicConnection =
topicConnectionFactory.
createTopicConnection();
topicSession = topicConnection.
createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
syncTopicPublisher =
topicSession.createPublisher(syncTopic);
commandTopicPublisher =
topicSession.createPublisher(
commandTopic);
updateTopicSubscriber =
topicSession.createSubscriber(
updateTopic);
updateListener = new UpdateListener(this);
updateTopicSubscriber.setMessageListener(
updateListener);
resyncTopicSubscriber =
topicSession.createSubscriber(
resyncTopic);
ResyncListener resyncListener =
new ResyncListener(this);
resyncTopicSubscriber.setMessageListener(
resyncListener);
aliveTopicSubscriber =
topicSession.createSubscriber(
aliveTopic);
AliveListener aliveListener =
new AliveListener(this);
aliveTopicSubscriber.setMessageListener(
aliveListener);
// Start JMS topic subscriptions
topicConnection.start();
}
catch (JMSException e)
{
System.out.println(
"Exception occurred: " + e.toString());
}
// Start a timer to listen for
// ApplicationMonitorAlive messages
javax.swing.Timer timer =
new javax.swing.Timer(3000, this);
timer.setRepeats(true);
timer.start();
}
/**
* Sets the last time the
* ApplicationMonitorAlive messages was used
*/
public void setLastAliveTime()
{
lastAliveTime = System.currentTimeMillis();
}
/**
* Timer callback. If the
* plicationMonitorAlive message was not
* received within the last 10 seconds, call
* the processDead() method
*/
public void actionPerformed(ActionEvent _evt)
{
if (System.currentTimeMillis() -
lastAliveTime > 10000)
processDead();
}
/**
* Publishes the JMS ApplicationMonitorSync
* message
*/
public void sync() throws JMSException
{
TextMessage message = null;
message = topicSession.createTextMessage();
message.setText("SYNC");
TemporaryTopic tempTopic =
topicSession.createTemporaryTopic();
message.setJMSReplyTo(tempTopic);
TopicSubscriber syncTopicSubscriber =
topicSession.createSubscriber(tempTopic);
syncTopicSubscriber.setMessageListener(
updateListener);
syncTopicPublisher.publish(message);
}
/**
* Publishes a <Command> XML
* ApplicationMonitorCommand JMS message
*/
public void sendCommand(String _action,
String _appName, String _host,
String _version) throws
ParserConfigurationException, JMSException,
IOException
{
XMLMessageUtil xmlMessageUtil =
new XMLMessageUtil("Command");
xmlMessageUtil.add("Action", _action);
xmlMessageUtil.add("AppName", _appName);
xmlMessageUtil.add("Host", _host);
xmlMessageUtil.add("Version", _version);
TextMessage commandMessage =
topicSession.createTextMessage();
commandMessage.setText(
xmlMessageUtil.getXML());
commandTopicPublisher.publish(
commandMessage);
}
/**
* Method to process <Update> XML messages
* received on the ApplicationMonitorUpdate JMS
* subscription. This message is received due
* to the ApplicationMonitorServer detecting a
* remote client status change.
*/
public abstract void processUpdate(
ClientOb _clientOb);
/**
* Method to process ApplicationMonitorServer
* death
*/
public abstract void processDead();
/**
* Method to process ApplicationMonitorReSync
* JMS messages. This message is received due to
* the ApplicationMonitorServer re-initializing
*/
public abstract void processReSync();
}
|