| Commit in servicemix/base/src/main/java/org/servicemix/components/jmx on MAIN | |||
| CounterMonitorComponent.java | +160 | added 1.1 | |
First cut of the CounterMonitorComponent
servicemix/base/src/main/java/org/servicemix/components/jmx
CounterMonitorComponent.java added at 1.1
diff -N CounterMonitorComponent.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ CounterMonitorComponent.java 23 Sep 2005 10:48:49 -0000 1.1 @@ -0,0 +1,160 @@
+/**
+ * <a href="">ServiceMix: The open source ESB</a>
+ *
+ * Copyright 2005 RAJD Consultancy Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ **/
+
+package org.servicemix.components.jmx;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.servicemix.components.util.ComponentSupport;
+import org.servicemix.jbi.jaxp.StringSource;
+import javax.jbi.JBIException;
+import javax.jbi.component.ComponentContext;
+import javax.jbi.management.DeploymentException;
+import javax.jbi.messaging.InOnly;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.management.MBeanServer;
+import javax.management.Notification;
+import javax.management.NotificationListener;
+import javax.management.ObjectName;
+import javax.management.monitor.CounterMonitor;
+import javax.xml.transform.Source;
+/**
+ * A JMX Counter Monitor as a Component to enable firing notifications
+ *
+ * @version $Revision: 1.1 $
+ */
+public class CounterMonitorComponent extends ComponentSupport implements NotificationListener{
+ private static final Log log = LogFactory.getLog(ComponentSupport.class);
+ private String name;
+ private ObjectName ourName;
+ private String observedObjectName;
+ private String attributeName;
+ private long granularityPeriod;
+ private Number threshold;
+ private Number offset;
+ private MBeanServer mbeanServer;
+ private CounterMonitor counterMonitor = new CounterMonitor();
+
+ /**
+ * Called when the Component is initialized
+ *
+ * @param cc
+ * @throws JBIException
+ */
+ public void init(ComponentContext cc) throws JBIException{
+ super.init(cc);
+ validate();
+ if(mbeanServer == null){
+ mbeanServer = cc.getMBeanServer();
+ }
+ try{
+ ObjectName observedName = new ObjectName(observedObjectName);
+ if(name == null){
+ String type = observedName.getKeyProperty("type");
+ type = type != null?type:"UNKNOWN";
+ name = mbeanServer.getDefaultDomain() + ":type=CounterMonitior_" + type;
+ }
+ ourName = new ObjectName(name);
+ counterMonitor.addObservedObject(observedName);
+ counterMonitor.setObservedAttribute(attributeName);
+ counterMonitor.setGranularityPeriod(granularityPeriod);
+ counterMonitor.setDifferenceMode(false);
+ counterMonitor.setInitThreshold(threshold);
+ counterMonitor.setOffset(offset);
+ mbeanServer.registerMBean(cc,ourName);
+ mbeanServer.addNotificationListener(ourName,this,null,new Object());
+ }catch(Exception e){
+ throw new DeploymentException(e);
+ }
+ }
+
+ /**
+ * Start the item.
+ *
+ * @exception javax.jbi.JBIException
+ * if the item fails to start.
+ */
+ public void start() throws javax.jbi.JBIException{
+ super.start();
+ counterMonitor.start();
+ }
+
+ /**
+ * Stop the item. This suspends current messaging activities.
+ *
+ * @exception javax.jbi.JBIException
+ * if the item fails to stop.
+ */
+ public void stop() throws javax.jbi.JBIException{
+ counterMonitor.stop();
+ super.stop();
+ }
+
+ /**
+ * Shut down the item. The releases resources, preparatory to uninstallation.
+ *
+ * @exception javax.jbi.JBIException
+ * if the item fails to shut down.
+ */
+ public void shutDown() throws javax.jbi.JBIException{
+ stop();
+ if(ourName != null && mbeanServer != null){
+ try{
+ mbeanServer.removeNotificationListener(ourName,this);
+ }catch(Exception e){
+ throw new JBIException(e);
+ }
+ }
+ super.shutDown();
+ }
+
+
+ /**
+ * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, java.lang.Object)
+ */
+ public void handleNotification(Notification notification,Object arg1){
+ try {
+ Source source = new StringSource(notification.getMessage());
+ InOnly exchange = getExchangeFactory().createInOnlyExchange();
+ NormalizedMessage message = exchange.createMessage();
+ message.setContent(source);
+ exchange.setInMessage(message);
+ done(exchange);
+ }
+ catch (Exception e) {
+ log.error("Failed to send Notification message to the NMR");
+ }
+
+ }
+
+ protected void validate() throws JBIException{
+ if(observedObjectName == null){
+ throw new DeploymentException("observedObjectName is null");
+ }
+ if(attributeName == null){
+ throw new DeploymentException("attributeName is null");
+ }
+ if(threshold == null){
+ throw new DeploymentException("threshold is null");
+ }
+ if(offset == null){
+ throw new DeploymentException("offset is null");
+ }
+ }
+}
