[jboss-svn-commits] JBL Code SVN: r15096 - labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Sep 13 18:07:08 EDT 2007


Author: tcunning
Date: 2007-09-13 18:07:08 -0400 (Thu, 13 Sep 2007)
New Revision: 15096

Added:
   labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java
Modified:
   labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java
Log:
bug:JBESB-918
Add JBRules monitoring counter.


Added: labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBRulesCounter.java	2007-09-13 22:07:08 UTC (rev 15096)
@@ -0,0 +1,383 @@
+package org.jboss.internal.soa.esb.services.routing.cbr;
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+import java.util.HashMap;
+import java.util.List;
+
+import javax.management.Attribute;
+import javax.management.AttributeList;
+import javax.management.AttributeNotFoundException;
+import javax.management.DynamicMBean;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.InstanceNotFoundException;
+import javax.management.InvalidAttributeValueException;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanException;
+import javax.management.MBeanInfo;
+import javax.management.MBeanOperationInfo;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
+
+import org.apache.log4j.Logger;
+import org.jboss.mx.util.MBeanServerLocator;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.listeners.ListenerTagNames;
+
+/**
+ * JBRulesMessageCounter is a MBean that displays shows which rules sets have been fired
+ * and the processing time of the firings.
+ * 
+ * @author <a href="mailto:tcunning at redhat.com">tcunning at redhat.com</a>
+ * @since Version 4.2.1
+ */
+public class JBRulesCounter implements DynamicMBean {
+	private static final Logger logger = Logger.getLogger(JBRulesCounter.class);
+	public static final String RULES_SUCCEED = "SUCCEED";
+	public static final String RULES_FAILED = "FAILED";
+	
+	private HashMap<String, Integer> actionCounterHash;
+	private HashMap<String, Integer> actionFailedCounterHash;
+	private HashMap<String, Long> actionProcessTimeHash;
+	private ConfigTree m_config;
+	private Integer serviceCount;
+	
+	public static final String RESET_COUNTER = "resetCounter";
+	private static final String MESSAGE_COUNTER = "messages successfully processed count";
+	private static final String FAILED_MESSAGE_COUNTER = "messages failed count";
+	private static final String PROCESSING_TIME = "processing time";
+	private static final String OVERALL_SERVICE_COUNT = "overall message transform count";
+	
+	/**
+	 * Constructor
+	 * @param f_config config tree
+	 */
+	public JBRulesCounter(ConfigTree f_config) {
+		
+		actionCounterHash = new HashMap<String, Integer>();
+		actionFailedCounterHash = new HashMap<String, Integer>();
+		actionProcessTimeHash = new HashMap<String, Long>();
+		serviceCount = new Integer(0);
+		
+		m_config = f_config;
+	}
+		
+	/**
+	 * Increment the total message count of this service.
+	 */
+	public void incrementTotalCount() {
+		serviceCount = new Integer(serviceCount.intValue() + 1);
+	}
+	
+	/**
+	 * Initialize the hashes by setting the counts and the processing time to an initial value of 0. 
+	 */
+	public void initHashes() {
+		serviceCount = new Integer(0);
+	}
+	
+	/**
+	 * Reset the counters - set all the entries in the action counter hash 
+	 * and in the action process time hash to zero.
+	 */
+	public void resetCounter() {
+		serviceCount = new Integer(0);
+		
+		for (String key : actionCounterHash.keySet()) {
+			actionCounterHash.put(key, new Integer(0));
+		}
+		
+		for (String key : actionFailedCounterHash.keySet()) {
+			actionFailedCounterHash.put(key, new Integer(0));
+		}
+		
+		for (String key : actionProcessTimeHash.keySet()) {
+			actionProcessTimeHash.put(key, new Long(0));
+		}
+	}
+	
+	/**
+	 *  This creates the MBeanInfo object provided.     We are returning generic 
+	 *  text for the attribute descriptions (the word Property and the name of the 
+	 *  attribute), all of the attributes are read-only, and we provide four 
+	 *  invocation methods - start/stop/initialise/destroy on the Lifecycle. 
+	 */
+    public MBeanInfo getMBeanInfo() {
+		
+		int count = actionCounterHash.size() + actionProcessTimeHash.size()
+			+ actionFailedCounterHash.size() + 1;
+        MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[count];
+        int counter = 0;
+              
+		for (String key : actionCounterHash.keySet()) {
+            attrs[counter] = new MBeanAttributeInfo(
+                    key, "java.lang.Integer", "Property " + key, true, false, false);
+            counter++;
+		}
+		
+		for (String key : actionProcessTimeHash.keySet()) {
+            attrs[counter] = new MBeanAttributeInfo(
+                    key, "java.lang.Double", "Property " + key, true, false, false);
+            counter++;
+		}
+		
+		for (String key : actionFailedCounterHash.keySet()) {
+            attrs[counter] = new MBeanAttributeInfo(
+                    key, "java.lang.Integer", "Property " + key, true, false, false);
+            counter++;
+		}
+		
+		MBeanAttributeInfo overallCount = new MBeanAttributeInfo(OVERALL_SERVICE_COUNT, "java.lang.Integer",
+				"Property " + OVERALL_SERVICE_COUNT, true, false, false);
+		attrs[counter] = overallCount;
+		counter++;
+			
+        MBeanOperationInfo[] opers = {
+        	new MBeanOperationInfo(
+        			RESET_COUNTER, "Reset the counter",
+                	null, "void", MBeanOperationInfo.ACTION)
+        };
+        return new MBeanInfo(
+                this.getClass().getName(), "Service Message Counter MBean",
+                attrs, null, opers, null); // notifications
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
+	 */
+	public Object getAttribute(String key) throws AttributeNotFoundException, MBeanException, ReflectionException {
+    	if (actionCounterHash.containsKey(key)) {
+    		Integer value = null;
+    		value = actionCounterHash.get(key);
+    		return value;
+    	} else if (actionProcessTimeHash.containsKey(key)) {
+    		Long processTotal = actionProcessTimeHash.get(key);
+    		String actionId = key.substring(0, key.indexOf(PROCESSING_TIME)-1);
+    		Integer successCount = actionCounterHash.get(actionId + " " + MESSAGE_COUNTER);
+    		Double value = null;
+    		
+    		if (successCount.intValue() > 0) {
+    			value = ((double) processTotal / successCount.intValue());
+    		} else {
+    			value = null;
+    		}
+    		return value;
+    	} else if (actionFailedCounterHash.containsKey(key)) {
+    		Integer value = actionFailedCounterHash.get(key);
+    		return value;
+    	} else if (OVERALL_SERVICE_COUNT.equals(key)) {
+    		return serviceCount;
+    	}
+    	return null;
+    }
+
+	/* (non-Javadoc)
+	 * @see javax.management.DynamicMBean#getAttributes(java.lang.String[])
+	 */
+	public AttributeList getAttributes(String[] arg0) {
+		AttributeList attributeList = new AttributeList();
+		for (String key : actionCounterHash.keySet()) {
+			Attribute at = new Attribute(key, actionCounterHash.get(key).toString());
+			attributeList.add(at);
+		}
+		
+		Attribute overallCount = new Attribute(OVERALL_SERVICE_COUNT, serviceCount);
+		attributeList.add(overallCount);
+		
+		for (String key : actionProcessTimeHash.keySet()) {
+			Long processTotal = actionProcessTimeHash.get(key);
+			String actionId = key.substring(0, key.indexOf(PROCESSING_TIME));
+			Integer successCount = actionCounterHash.get(actionId + " " + MESSAGE_COUNTER);
+			String avgTime = null;
+			if (successCount.intValue() > 0) {
+				avgTime = ((double) processTotal / successCount.intValue()) + " ns";
+			}
+			Attribute at = new Attribute(key, avgTime);
+			attributeList.add(at);
+		}
+		
+		for (String key : actionFailedCounterHash.keySet()) {
+			Attribute at = new Attribute(key, actionFailedCounterHash.get(key).toString());
+			attributeList.add(at);
+		}
+
+		return attributeList;
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.management.DynamicMBean#invoke(java.lang.String, java.lang.Object[], java.lang.String[])
+	 */
+	public Object invoke(String method, Object[] arg1, String[] arg2) throws MBeanException, ReflectionException {
+		if (method.equalsIgnoreCase(RESET_COUNTER)) {
+				resetCounter();
+			return "Invoking the " + method + " on the lifecycle.";
+		} else {
+			throw new ReflectionException(new NoSuchMethodException(method));
+		}
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
+	 */
+	public void setAttribute(Attribute arg0) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.management.DynamicMBean#setAttributes(javax.management.AttributeList)
+	 */
+	public AttributeList setAttributes(AttributeList arg0) {
+		return null;
+	}
+
+	/**
+	 * Register this MBean with JBoss.
+	 */
+	protected void registerMBean() {
+		MBeanServer mbeanServer = null;
+		try {
+			mbeanServer = MBeanServerLocator.locateJBoss();
+		} catch (IllegalStateException ise) {
+			// If we can't find a JBoss MBeanServer, just return
+			// Needed for unit tests
+			return;
+		}
+		
+		ObjectName listObjectName = getObjectName();
+		
+		if (mbeanServer.isRegistered(listObjectName)) {
+	    	try {
+	    		mbeanServer.unregisterMBean(listObjectName);
+			} catch (InstanceNotFoundException e) {
+				e.printStackTrace();
+			} catch (MBeanRegistrationException e) {
+				e.printStackTrace();
+			}
+        }
+		
+	    try {
+	    	mbeanServer.registerMBean(this, listObjectName);
+		} catch (InstanceAlreadyExistsException e) {
+			e.printStackTrace();
+		} catch (MBeanRegistrationException e) {
+			e.printStackTrace();
+		} catch (NotCompliantMBeanException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * Initialize the message profile with a count of 0 for the time hash, the processed hash, and the failed hash.
+	 * @param messageProfile message profile
+	 */
+	public void initMessageProfile(String messageProfile) {
+		if (!actionProcessTimeHash.containsKey(messageProfile + " " + PROCESSING_TIME)) {
+			actionProcessTimeHash.put(messageProfile, new Long(0));
+		}
+		if (!actionCounterHash.containsKey(messageProfile + " " + MESSAGE_COUNTER)) {
+			actionCounterHash.put(messageProfile, new Integer(0));
+		}
+		if (!actionFailedCounterHash.containsKey(messageProfile + " " + FAILED_MESSAGE_COUNTER)) {
+			actionCounterHash.put(messageProfile, new Integer(0));
+		}
+	}
+	
+
+	/**
+	 * Update the counts of the JBRulesCounter for a particular message profile.
+	 * @param procTime time
+	 * @param messageProfile profile
+	 * @param status status
+	 */
+	public void update(long procTime, String messageProfile, String status) {		
+		if ((messageProfile != null) && (!messageProfile.equals(""))) {
+			initMessageProfile(messageProfile);
+		}
+		
+		if (RULES_SUCCEED.equals(status)) {
+			Integer count = null;
+			Long time = null;
+			if (actionCounterHash.containsKey(messageProfile + " " + MESSAGE_COUNTER)) {
+				count = actionCounterHash.get(messageProfile + " " + MESSAGE_COUNTER);
+				count = count.intValue() + 1;
+			} else {
+				count = new Integer(1);
+			}
+			actionCounterHash.put(messageProfile + " " + MESSAGE_COUNTER, count);
+			
+			if (actionProcessTimeHash.containsKey(messageProfile + " " + PROCESSING_TIME)) {
+				time = actionProcessTimeHash.get(messageProfile + " " + PROCESSING_TIME);
+				time = time.longValue() + procTime;				
+			} else {
+				time = procTime;
+			}
+			actionProcessTimeHash.put(messageProfile + " " + PROCESSING_TIME, time);
+		} else if (RULES_FAILED.equals(status)) {
+			Integer count = null;
+			if (actionFailedCounterHash.containsKey(messageProfile + " " + FAILED_MESSAGE_COUNTER)) {
+				count = actionFailedCounterHash.get(messageProfile + " " + FAILED_MESSAGE_COUNTER);
+				count = count.intValue() + 1; 
+			} else {
+				count = new Integer(1);
+			}
+			actionFailedCounterHash.put(messageProfile + " " + FAILED_MESSAGE_COUNTER, count);
+		}		
+	}
+	
+	/**
+	 * Return the object name, which for a JBRulesCounter is the deployment name (x.esb) and the category
+	 * (JBRulesCounter).
+	 * @return object name
+	 */
+	protected ObjectName getObjectName()
+	{
+		String deploymentName = "";
+		List<KeyValuePair> properties = m_config.attributesAsList();
+		
+		// Get Deployment Name
+		try {
+			deploymentName = m_config.getParent().getParent().getAttribute("deployment");
+		} catch (Exception e) {
+			logger.error("", e);
+		}
+		
+		ObjectName listObjectName = null;
+		try {
+
+			StringBuffer objectName = new StringBuffer("category=JBRulesMessageCounter");
+			if (deploymentName != null) {
+				objectName.append(",").append(ListenerTagNames.DEPLOYMENT_NAME_TAG).append("=").append(deploymentName);
+			}
+			
+			listObjectName = new ObjectName("jboss.esb:" + objectName.toString());
+		} catch (MalformedObjectNameException e1) {
+			e1.printStackTrace();
+		} catch (NullPointerException e1) {
+			e1.printStackTrace();
+		}
+		return listObjectName;
+	}
+}

Modified: labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java
===================================================================
--- labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java	2007-09-13 22:01:07 UTC (rev 15095)
+++ labs/jbossesb/trunk/product/services/jbrules/src/main/java/org/jboss/internal/soa/esb/services/routing/cbr/JBossRulesRouter.java	2007-09-13 22:07:08 UTC (rev 15096)
@@ -39,7 +39,11 @@
 import org.drools.compiler.PackageBuilder;
 import org.drools.compiler.PackageBuilderConfiguration;
 import org.drools.rule.Package;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.internal.soa.esb.services.routing.cbr.JBRulesCounter;
+import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.services.registry.RegistryException;
 import org.jboss.soa.esb.services.routing.MessageRouterException;
 import org.jboss.soa.esb.services.routing.cbr.CBRException;
 import org.jboss.soa.esb.services.routing.cbr.ContentBasedRouter;
@@ -59,7 +63,10 @@
 	private static Map<String, String> ruleSets = new ConcurrentHashMap<String, String>();
 
 	private static Logger logger = Logger.getLogger(JBossRulesRouter.class);
-
+	
+	private ConfigTree m_config;
+	private JBRulesCounter rulesCounter = null;
+	
 	/**
      * Route the message with a reference to the ruleSets supplied in the message.
      * Not implemented.
@@ -104,7 +111,9 @@
 			boolean ruleReload, Message message, List<Object> objectList) throws MessageRouterException
 	{
         List<String> destinations = new ArrayList();
-		try {
+        long startTime = System.nanoTime();
+
+        try {
     		String newRuleSet = null;
     		boolean isRulesChanged = false;
     
@@ -143,16 +152,33 @@
     		workingMemory.insert(message);
     		logger.log(Level.DEBUG, "Fire the JBossRules Engine");
     		workingMemory.fireAllRules();
+    		long procTime = System.nanoTime() - startTime;
+    		if (rulesCounter != null) {
+    			rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_SUCCEED);
+    		}
+    			
     		logger.log(Level.DEBUG,
     				"Outgoing Destinations: " + destinations);
             workingMemory.dispose();
             return destinations;
         } catch (IOException ioe) {
-            throw new MessageRouterException("Could not read the rules. " +  ioe.getMessage(), ioe);
+        	if (rulesCounter != null) {
+        		long procTime = System.nanoTime() - startTime;
+        		rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
+        	}
+        	throw new MessageRouterException("Could not read the rules. " +  ioe.getMessage(), ioe);
         } catch (DroolsParserException dpe) {
-            throw new MessageRouterException("Could not parse the rules. " +  dpe.getMessage(), dpe);
+        	if (rulesCounter != null) {
+        		long procTime = System.nanoTime() - startTime;
+        		rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
+        	}
+        	throw new MessageRouterException("Could not parse the rules. " +  dpe.getMessage(), dpe);
         } catch (CBRException cbre) {
-            throw new MessageRouterException("Could not parse the rules. " +  cbre.getMessage(), cbre);
+        	if (rulesCounter != null) {
+        		long procTime = System.nanoTime() - startTime;
+        		rulesCounter.update(procTime, ruleSet, JBRulesCounter.RULES_FAILED);
+        	}
+        	throw new MessageRouterException("Could not parse the rules. " +  cbre.getMessage(), cbre);
         }
 	}
 
@@ -256,4 +282,11 @@
 		}
 		return stringBuffer.toString();
 	}
+
+	@Override
+	public void setConfigTree(ConfigTree configTree) {
+		m_config = configTree;
+		rulesCounter = new JBRulesCounter(configTree);
+		rulesCounter.registerMBean();
+	}
 }




More information about the jboss-svn-commits mailing list