Thread: [Batchserver-cvs] batchserver/src/org/jmonks/batchserver/framework/management JobManagementClient.ja
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-04-05 20:54:52
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/management In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27676 Added Files: JobManagementClient.java Log Message: no message --- NEW FILE: JobManagementClient.java --- /* * JobManagementClient.java * * Created on April 5, 2006, 12:05 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.framework.management; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.management.InstanceNotFoundException; import javax.management.ListenerNotFoundException; import javax.management.MBeanServerConnection; import javax.management.MBeanServerInvocationHandler; import javax.management.MalformedObjectNameException; import javax.management.NotificationListener; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import org.apache.log4j.Logger; import org.jmonks.batchserver.framework.config.ConfigurationException; /** * * Provides the high level services to all management clients abstracting the * JMX connector details. * * @author Suresh Pragada * @version 1.0 * @since 1.0 */ public class JobManagementClient { /** * Singleton management client instance. */ private final static JobManagementClient jobManagementClient=new JobManagementClient(); /** * Variable holds the Job connector helper class. */ private JobConnectorHelper jobConnectorHelper=null; /** * Map holds all the jobs currently running. Job names will be keys and JMX Connector * objects will be values in this map. */ private Map connectorMap=new HashMap(); private static Logger logger=Logger.getLogger(JobManagementClient.class); /** * Creates a new instance of JobManagementClient * * @throws ConfigurationException If unable to get the JobConnectorHelper reference. */ private JobManagementClient() { this.jobConnectorHelper=JobConnectorHelper.getJobConnectorHelper(); if(jobConnectorHelper==null) throw new ConfigurationException(ConfigurationException.JOB_CONNECTOR_CONFIG, "could not get the reference to job connector helper class."); } /** * Returns the singleton management client instance. */ public static JobManagementClient getJobManagementClient() { return jobManagementClient; } /** * Returns the list of running jobs as an array. * * @return Returns the list of running jobs as an array. In case of 0 jobs it returns * array of the size 0. */ public List getRunningJobs() { logger.trace("Entering getRunningJobs"); List runningJobList=null; List jobList=this.jobConnectorHelper.getRegisteredJobList(); for(Iterator iterator=jobList.iterator();iterator.hasNext();) { String jobName=(String)iterator.next(); if(!this.connectorMap.containsKey(jobName)) { logger.info("New job has been found in the lookup = " + jobName); JMXConnector jmxConnector=this.jobConnectorHelper.createConnector(jobName); this.connectorMap.put(jobName, jmxConnector); logger.debug("Successfully added the job to connection map = " + jobName); } } runningJobList=(List)this.connectorMap.keySet(); logger.trace("Exiting getRunningJobs"); return runningJobList; } /** * Gets the management bean of the requested job. If the requested jobname * cannot be found in the lookup, it returns null. * * @param jobName Name of the job. * * @return Returns the manager bean to control the job. * * @throws IllegalArgumentException If jobName is null. */ public JobManagerMBean getManagementBean(String jobName) { logger.trace("Entering getManagementBean " + jobName); if(jobName==null || "".equals(jobName)) throw new IllegalArgumentException("Job name cannot be null to get the manager bean reference."); JobManagerMBean managerBean=null; if(this.connectorMap.containsKey(jobName)) { JMXConnector jmxConnector=(JMXConnector)this.connectorMap.get(jobName); if(jmxConnector==null) { this.connectorMap.remove(jobName); } else { try { MBeanServerConnection mbeanServerConnection=jmxConnector.getMBeanServerConnection(); ObjectName managerBeanObjectName= new ObjectName(JobManagementAgent.MBEAN_SERVER_DOMAIN_NAME+":jobName="+jobName+",objectType="+JobManagementAgent.JOB_MANAGER_MBEAN_NAME); managerBean=(JobManagerMBean)MBeanServerInvocationHandler.newProxyInstance(mbeanServerConnection, managerBeanObjectName, JobManagerMBean.class, false); } catch(MalformedObjectNameException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(IOException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } } } else managerBean=null; logger.trace("Exiting getManagementBean"); return managerBean; } /** * Gets the monitor bean of the requested job. If the requested jobname * cannot be found in the lookup, it returns null. * * @param jobName Name of the job. * * @return Returns the monitor bean to control the job. * * @throws IllegalArgumentException If jobName is null. */ public JobMonitorMBean getMonitorMBean(String jobName) { logger.trace("Entering getMonitorMBean " + jobName); if(jobName==null || "".equals(jobName)) throw new IllegalArgumentException("Job name cannot be null to get the monitor bean reference."); JobMonitorMBean monitorBean=null; if(this.connectorMap.containsKey(jobName)) { JMXConnector jmxConnector=(JMXConnector)this.connectorMap.get(jobName); if(jmxConnector==null) { this.connectorMap.remove(jobName); } else { try { MBeanServerConnection mbeanServerConnection=jmxConnector.getMBeanServerConnection(); ObjectName monitorBeanObjectName= new ObjectName(JobManagementAgent.MBEAN_SERVER_DOMAIN_NAME+":jobName="+jobName+",objectType="+JobManagementAgent.JOB_MONITOR_MBEAN_NAME); monitorBean=(JobMonitorMBean)MBeanServerInvocationHandler.newProxyInstance(mbeanServerConnection, monitorBeanObjectName, JobMonitorMBean.class, false); } catch(MalformedObjectNameException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(IOException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } } } else monitorBean=null; logger.trace("Exiting getMonitorMBean"); return monitorBean; } /** * Registers the given listener to all the mbeans of given job. * * @param jobName Name of the job that listener needs to be attached. * @param notificationListener Listener object that is interested in the notifications. * * @return Returns true, if it could register the listener, false, otherwise. */ public boolean registerListener(String jobName,NotificationListener notificationListener) { logger.trace("Entering registerListener " + jobName); if(jobName==null || "".equals(jobName)) throw new IllegalArgumentException("job name cannot be null to register the listener."); if(notificationListener==null) throw new IllegalArgumentException("notification listener cannot be null to register the listener."); boolean registered=false; if(this.connectorMap.containsKey(jobName)) { JMXConnector jmxConnector=(JMXConnector)this.connectorMap.get(jobName); if(jmxConnector==null) { this.connectorMap.remove(jobName); } else { try { MBeanServerConnection mbeanServerConnection=jmxConnector.getMBeanServerConnection(); ObjectName monitorBeanObjectName= new ObjectName(JobManagementAgent.MBEAN_SERVER_DOMAIN_NAME+":jobName="+jobName+",objectType="+JobManagementAgent.JOB_MONITOR_MBEAN_NAME); ObjectName managerBeanObjectName= new ObjectName(JobManagementAgent.MBEAN_SERVER_DOMAIN_NAME+":jobName="+jobName+",objectType="+JobManagementAgent.JOB_MANAGER_MBEAN_NAME); mbeanServerConnection.addNotificationListener(monitorBeanObjectName,notificationListener, null, null); mbeanServerConnection.addNotificationListener(managerBeanObjectName,notificationListener, null, null); registered=true; } catch(InstanceNotFoundException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(MalformedObjectNameException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(IOException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } } } else registered=false; logger.trace("Exiting registerListener = " + registered); return registered; } /** * Unregisters the given listener from all the mbeans of given job. * * @param jobName Name of the job that listener needs to be detached. * @param notificationListener Listener object that needs to be detached from all the mbeans. * * @return Returns true, if it could unregister the listener, false, otherwise. */ public boolean unregisterListener(String jobName,NotificationListener notificationListener) { logger.trace("Entering unregisterListener " + jobName); if(jobName==null || "".equals(jobName)) throw new IllegalArgumentException("job name cannot be null to unregister the listener."); if(notificationListener==null) throw new IllegalArgumentException("notification listener cannot be null to unregister the listener."); boolean unregistered=false; if(this.connectorMap.containsKey(jobName)) { JMXConnector jmxConnector=(JMXConnector)this.connectorMap.get(jobName); if(jmxConnector==null) { this.connectorMap.remove(jobName); } else { try { MBeanServerConnection mbeanServerConnection=jmxConnector.getMBeanServerConnection(); ObjectName monitorBeanObjectName= new ObjectName(JobManagementAgent.MBEAN_SERVER_DOMAIN_NAME+":jobName="+jobName+",objectType="+JobManagementAgent.JOB_MONITOR_MBEAN_NAME); ObjectName managerBeanObjectName= new ObjectName(JobManagementAgent.MBEAN_SERVER_DOMAIN_NAME+":jobName="+jobName+",objectType="+JobManagementAgent.JOB_MANAGER_MBEAN_NAME); mbeanServerConnection.removeNotificationListener(monitorBeanObjectName,notificationListener); mbeanServerConnection.removeNotificationListener(managerBeanObjectName,notificationListener); unregistered=true; } catch(ListenerNotFoundException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(InstanceNotFoundException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(MalformedObjectNameException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } catch(IOException exception) { exception.printStackTrace(); logger.error(exception.getMessage(), exception); } } } else unregistered=false; logger.trace("Exiting unregisterListener"); return unregistered; } } |