User: user57  
  Date: 02/02/15 20:16:32

  Added:       varia/src/main/org/jboss/varia/counter
                        CounterInterceptor.java CounterService.java
                        CounterServiceMBean.java package.html
  Log:
   o CounterService moved from server to varia
   o Repackaged under org.jboss.varia.counter
   o Added counter-service.xml, which should go into an example
     deployment directory, but now just gets dropped into deploy
  
  Revision  Changes    Path
  1.1                  
contrib/varia/src/main/org/jboss/varia/counter/CounterInterceptor.java
  
  Index: CounterInterceptor.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.varia.counter;
  
  import java.util.Map;
  
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import org.jboss.invocation.Invocation;
  import org.jboss.ejb.Container;
  import org.jboss.ejb.plugins.*;
  
  
  import org.jboss.logging.Logger;
  import org.jboss.varia.counter.CounterService;
  
  /**
   * Interceptor that uses the CounterService MBean to record the length of time
   * spent in 'lower' interceptors (below it in the stack).
   * 
   * <p><b>How to use:</b></p>
   * <p>First, the CounterService MBean must be installed in JBoss.
   *    See counter-service.xml for details/examples.
   *
   * <p>Next, you need to configure this interceptor into the interceptor stacks
   * of any beans you wish to monitor. This can be done either globally for a
   * container-config in standardjboss.xml, or on a per-bean basis in a jar's
   * jboss.jcml. Just insert the following at the top of the 
&lt;container-interceptors&gt;
   * section. If you're overriding this for a bean in jboss.xml, you'll need to
   * override the entire container-interceptors section.</p>
   * 
   * <code>
   * &lt;interceptor&gt;org.jboss.varia.counter.CounterInterceptor&lt;/interceptor&gt;
   * </code>
   * 
   * <p>This can go anywhere in the container-interceptors section, but either
   * the top or the bottom will probably be best for gathering application
   * statistics.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]";>Dan Christopherson</href>
   * @version $Revision: 1.1 $
   */
  public class CounterInterceptor
     extends AbstractInterceptor
  {
     Container container = null;
     CounterService counter = null;
     boolean loggedNoCounter = false;
     StringBuffer baseCounterName = null;
     int baseNameLength = 0;
  
     public CounterInterceptor() {
     }
     
     public void setContainer(Container container) {
        baseCounterName = new StringBuffer(container.getBeanClass().getName());
        baseNameLength = baseCounterName.length();
        this.container = container;
     }
     public Container getContainer() {
        return container;
     }
  
     public Object invokeHome(Invocation mi) throws Exception {
        long startTime=System.currentTimeMillis();
        try {
           return super.invokeHome(mi);
        } finally {
           if (getCounter() != null) {
              long endTime=System.currentTimeMillis();
              baseCounterName.append("Home.");
              baseCounterName.append(mi.getMethod().getName());
              counter.accumulate(baseCounterName.toString(), endTime-startTime);
              baseCounterName.setLength(baseNameLength);
           }
        }
     }
  
     public Object invoke(Invocation mi) throws Exception {
        long startTime=System.currentTimeMillis();
        try {
           return super.invoke(mi);
        } finally {
           if (getCounter() != null) {
              long endTime=System.currentTimeMillis();
              baseCounterName.append('.');
              baseCounterName.append(mi.getMethod().getName());
              counter.accumulate(baseCounterName.toString(), endTime-startTime);
              baseCounterName.setLength(baseNameLength);
           }
        }
     }
  
     public void create() throws java.lang.Exception {
        //get a reference to the CounterService from JNDI
        log.debug("CounterInterceptor initializing");
     }
  
     private CounterService getCounter() {
        if (counter == null) {
           try {
              InitialContext ctx = new InitialContext();
              counter = (CounterService)ctx.lookup(CounterServiceMBean.JNDI_NAME);
           } catch (NamingException ne) {
              if (!loggedNoCounter) {
                 log.warn("CounterInterceptor can't get counter service ", ne);
                 loggedNoCounter = true;
              }
           }
        }
        return counter;
     }
  
     // Monitorable implementation ------------------------------------
     
     public void sample(Object s)
     {
        // Just here to because Monitorable request it but will be removed soon
     }
     
     public Map retrieveStatistic()
     {
        return null;
     }
     
     public void resetStatistic()
     {
     }
  }
  
  
  
  1.1                  
contrib/varia/src/main/org/jboss/varia/counter/CounterService.java
  
  Index: CounterService.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.varia.counter;
  
  import java.text.DecimalFormat;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Vector;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.naming.Reference;
  import javax.naming.StringRefAddr;
  import org.jboss.system.ServiceMBeanSupport;
  import org.jboss.naming.NonSerializableFactory;
  
  /**
   * A service offering accumulator style counters to help in diagnosing
   * performance issues.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]";>Dan Christopherson</href>
   * @version $Revision: 1.1 $
   */
  public class CounterService
     extends ServiceMBeanSupport
     implements CounterServiceMBean
  {
     private HashMap counterMap = new HashMap();
     
     /**
      * accumulate a count into a named counter. will initialize the named
      * counter if neccessary.
      */
     public void accumulate(String counterName, long add)
     {
        Counter counter = null;
        synchronized (counterMap)
        {
           counter = (Counter)counterMap.get(counterName);
           if (counter == null)
           {
              counter = new Counter(counterName);
              counterMap.put(counterName, counter);
           }
        }
        counter.addToCount(add);
     }
     
     protected void startService() throws Exception
     {
        InitialContext ctx = new InitialContext();
        
        //bind myself into JNDI, at java:/CounterService
        NonSerializableFactory.bind(JNDI_NAME, this);
        StringRefAddr addr = new StringRefAddr("nns", JNDI_NAME);
        Reference ref = new Reference(this.getClass().getName(), addr, 
NonSerializableFactory.class.getName(), null);
        ctx.bind(JNDI_NAME, ref);
     }
     
     protected void stopService() throws Exception
     {
        InitialContext ctx = new InitialContext();
        ctx.unbind(JNDI_NAME);
        NonSerializableFactory.unbind(JNDI_NAME);
     }
     
     public String list()
     {
        DecimalFormat format = new DecimalFormat("####0.0000");
        String retVal = "";
        Iterator keys = counterMap.keySet().iterator();
        while (keys.hasNext())
        {
           String key = (String)keys.next();
           Counter counter = (Counter)counterMap.get(key);
           long total = 0;
           int entries = 0;
           synchronized (counter)
           {//so we dont catch half of it.
              total = counter.getCount();
              entries = counter.getEntries();
           }
           double avg = ((double)total)/((double)entries);
           String descrip = key+": total="+total+" on "+entries+"entries for "+
           "an average of "+format.format(avg)+"<br>\n";
           retVal += descrip;
        }
        return retVal;
     }
     
     private static class Counter
     {
        private String name;
        private long count=0;
        private int entries=0;
        
        public Counter(String n)
        {
           name = n;
        }
        
        public String getName()
        {
           return name;
        }
        
        public synchronized long getCount()
        {
           return count;
        }
        
        public synchronized int getEntries()
        {
           return entries;
        }
        
        public synchronized void addToCount(long add)
        {
           count += add;
           entries++;
        }
     }
  }
  
  
  
  1.1                  
contrib/varia/src/main/org/jboss/varia/counter/CounterServiceMBean.java
  
  Index: CounterServiceMBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.varia.counter;
  
  /** 
   * MBean interface for the CounterService.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]";>Dan Christopherson</href>
   * @version $Revision: 1.1 $
   */
  public interface CounterServiceMBean 
     extends org.jboss.system.ServiceMBean
  {
     String JNDI_NAME = "java:/CounterService";
     
     String list();
  }
  
  
  
  1.1                  contrib/varia/src/main/org/jboss/varia/counter/package.html
  
  Index: package.html
  ===================================================================
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
      <!-- $Id: package.html,v 1.1 2002/02/16 04:16:32 user57 Exp $ -->
      <!--
  
      JBoss: The OpenSource J2EE WebOS 
  
      Distributable under LGPL license.
      See terms of license at gnu.org.
  
      -->
    </head>
  
    <body bgcolor="white">
      <p>Counter components.
  
      <h2>Package Specification</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
        
      <h2>Related Documentation</h2>
      <ul>
        <li><a href="javascript: alert('not available')">Not Available</a>
      </ul>
  
      <h2>Package Status</h2>
      <ul>
        <li><font color="green"><b>STABLE</b></font>
      </ul>
  
      <h2>Todo</h2>
      <ul>
        <li>???
      </ul>
  
      <!-- Put @see and @since tags down here. -->
  
    </body>
  </html>
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to