User: juhalindfors
  Date: 01/12/12 16:12:58

  Modified:    src/main/test TestMX.java
  Log:
  MMB wrapper for standard mbean
  
  Revision  Changes    Path
  1.4       +236 -173  jmx/src/main/test/TestMX.java
  
  Index: TestMX.java
  ===================================================================
  RCS file: /cvsroot/jboss/jmx/src/main/test/TestMX.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestMX.java       2001/12/08 16:08:17     1.3
  +++ TestMX.java       2001/12/13 00:12:58     1.4
  @@ -4,164 +4,196 @@
   import javax.management.loading.*;
   import java.util.*;
   
  -public class TestMX {
  +import org.jboss.mx.modelmbean.*;
   
  -   public static void main(String[] args)  {
  -      try {
  +public class TestMX
  +{
   
  -      MBeanServer server = MBeanServerFactory.createMBeanServer();
  -      
  -      NotificationListener regListener = new NotificationListener() {
  -         public void handleNotification(Notification notif, Object handback) {
  -            
  -            System.out.println(notif);
  -            System.out.println(((MBeanServerNotification)notif).getMBeanName());
  -            System.out.println("handback : " + handback);
  -            System.out.println("type: " + notif.getType());
  -            System.out.println("source: " + notif.getSource());
  -            System.out.println("seq #: " + notif.getSequenceNumber());
  -            System.out.println("timestamp: " + notif.getTimeStamp());
  -            System.out.println("userdata: "  + notif.getUserData());
  -            System.out.println("message: " + notif.getMessage());
  +   public static void main(String[] args)
  +   {
  +      try
  +      {
  +
  +         MBeanServer server = MBeanServerFactory.createMBeanServer();
  +
  +         NotificationListener regListener = new NotificationListener()
  +                                            {
  +                                               public void 
handleNotification(Notification notif, Object handback)
  +                                               {
  +
  +                                                  System.out.println(notif);
  +                                                  
System.out.println(((MBeanServerNotification)notif).getMBeanName());
  +                                                  System.out.println("handback : " 
+ handback);
  +                                                  System.out.println("type: " + 
notif.getType());
  +                                                  System.out.println("source: " + 
notif.getSource());
  +                                                  System.out.println("seq #: " + 
notif.getSequenceNumber());
  +                                                  System.out.println("timestamp: " 
+ notif.getTimeStamp());
  +                                                  System.out.println("userdata: "  
+ notif.getUserData());
  +                                                  System.out.println("message: " + 
notif.getMessage());
  +                                               }
  +                                            };
  +
  +         server.createMBean("test.User", new ObjectName("standard:name=user"));
  +         server.createMBean("test.BroadcastingUser", new 
ObjectName("standard:name=buser"));
  +
  +         System.out.println(server.invoke(new ObjectName("standard:name=user"), 
"printInfo", null, null));
  +         System.out.println(server.invoke(new ObjectName("standard:name=buser"), 
"printInfo", new Object[0], new String[0]));
  +
  +
  +
  +
  +         // simple speed test, invoke empty methodInvocation 1 million times
  +         ObjectName oname = new ObjectName("Speed:name=mymbean,class=Dynamic");
  +         System.out.println(oname.getCanonicalName());
  +         System.out.println(oname.getDomain());
  +         System.out.println(oname.getKeyPropertyListString());
  +         ObjectInstance oi = server.registerMBean(new Dynamic(), oname);
  +
  +         long time = System.currentTimeMillis();
  +         for (int i = 0; i < 1000000; ++i)
  +            server.invoke(oname, "methodInvocation", null, null);
  +         long end = System.currentTimeMillis();
  +
  +         System.out.println("1M invocations, DYNAMIC, no args, null return: " + 
(end - time));
  +
  +
  +
  +         server.registerMBean(new DynamicUser(), new 
ObjectName("Test:test=dynamic"));
  +
  +         String name = (String)server.getAttribute(new 
ObjectName("Test:test=dynamic"), "Name");
  +         System.out.println("Name: " + name);
  +
  +         server.setAttribute(new ObjectName("Test:test=dynamic"), new 
Attribute("Name", "Juha"));
  +         name = (String)server.getAttribute(new ObjectName("Test:test=dynamic"), 
"Name");
  +         System.out.println("Name: " + name);
  +
  +         oname = new ObjectName("Speed:name=mymbean");
  +         server.registerMBean(new DynamicUser(), oname);
  +         String info = (String)server.invoke(oname, "printInfo", null, null);
  +         System.out.println(info);
  +
  +
  +         // simple speed test, invoke empty methodInvocation 1 million times
  +         oname = new ObjectName("Speed:name=mymbean,class=Dynamic");
  +
  +         time = System.currentTimeMillis();
  +         for (int i = 0; i < 1000000; ++i)
  +            server.invoke(oname, "methodInvocation", null, null);
  +         end = System.currentTimeMillis();
  +
  +         System.out.println("1M invocations, DYNAMIC, no args, null return: " + 
(end - time));
  +
  +
  +
  +         // simple speed test, invoke printInfo 1 million times
  +         oname = new ObjectName("Speed:name=mymbean");
  +
  +         time = System.currentTimeMillis();
  +         for (int i = 0; i < 1000000; ++i)
  +            server.invoke(oname, "printInfo", null, null);
  +         end = System.currentTimeMillis();
  +
  +         System.out.println("1M invocations, DYNAMIC, no args, return printInfo 
string: " + (end - time));
  +
  +
  +         server.registerMBean(new SuperStandardDynamic(), new 
ObjectName("standard:name=superStandardDynamic"));
  +         server.registerMBean(new StandardDynamic(), new 
ObjectName("standard:name=standardDynamic"));
  +         server.registerMBean(new RealDynamic(), new 
ObjectName(":name=realDynamic"));
  +
  +
  +         // simple speed test, invoke printInfo million times (STANDARD MBEAN)
  +         oname = new ObjectName("standard:name=user");
  +         time = System.currentTimeMillis();
  +         for (int i = 0; i < 1000000; ++i)
  +            server.invoke(oname, "printInfo", null, null);
  +         end = System.currentTimeMillis();
  +         System.out.println("1M invocations, STANDARD, no args, return printInfo 
string: " + (end - time));
  +
  +         // simple speed test, invoke no args, no return methodInvocation 1 million 
times (STANDARD MBEAN)
  +         oname = new ObjectName("standard:name=superStandardDynamic");
  +         time = System.currentTimeMillis();
  +         for (int i = 0; i < 1000000; ++i)
  +            server.invoke(oname, "methodInvocation", null, null);
  +         end = System.currentTimeMillis();
  +         System.out.println("1M invocations, STANDARD, no args, no return: " + (end 
- time));
  +
  +         oname = new ObjectName("standard:name=user");
  +         server.invoke(oname, "addPhoneNumber", new Object[] { "555-1234" }, new 
String[] { String.class.getName() });
  +
  +         System.out.println(server.invoke(oname, "printInfo", null, null));
  +
  +
  +         server.setAttribute(oname, new Attribute("Address", "Strawberry Street"));
  +         server.setAttribute(oname, new Attribute("Password", "abcdefg"));
  +         System.out.println(server.getAttribute(oname, "Address"));
  +         System.out.println(server.invoke(oname, "printInfo", null, null));
  +
  +         server.setAttribute(oname, new Attribute("PhoneNumbers", new String[] { 
"123-345", "123-422", "123-5242" }));
  +         System.out.println(server.invoke(oname, "printInfo", null, null));
  +         
  +         ObjectName mmbName = new ObjectName("model:test=test");
  +         server.registerMBean(new XMBean(new User(), 
XMBeanConstants.STANDARD_INTERFACE), mmbName);
  +
  +         System.out.println(server.invoke(mmbName, "printInfo", null, null));
  +         server.invoke(mmbName, "addPhoneNumber", new Object[] { "model mbean 
operation invoke" }, new String[] { String.class.getName() });
  +         System.out.println(server.invoke(mmbName, "printInfo", null, null));
  +
  +         // simple speed test, invoke printInfo 1 million times
  +         time = System.currentTimeMillis();
  +         for (int i = 0; i < 1000000; ++i)
  +            server.invoke(mmbName, "printInfo", null, null);
  +         end = System.currentTimeMillis();
  +
  +         System.out.println("1M invocations, MMB (INTROSPECTION), no args, return 
printInfo string: " + (end - time));
  +         
  +      }
  +      catch (Throwable t)
  +      {
  +         if (t instanceof MBeanException)
  +         {
  +            System.out.println(t.getMessage());
  +            ((MBeanException)t).getTargetException().printStackTrace();
            }
  -      };
  -      
  -      server.createMBean("test.User", new ObjectName("standard:name=user"));
  -      server.createMBean("test.BroadcastingUser", new 
ObjectName("standard:name=buser"));
  -      
  -      System.out.println(server.invoke(new ObjectName("standard:name=user"), 
"printInfo", null, null));
  -      System.out.println(server.invoke(new ObjectName("standard:name=buser"), 
"printInfo", new Object[0], new String[0]));
  -
  -
  -      
  -      
  -      // simple speed test, invoke empty methodInvocation 1 million times
  -      ObjectName oname = new ObjectName("Speed:name=mymbean,class=Dynamic");
  -      System.out.println(oname.getCanonicalName());
  -      System.out.println(oname.getDomain());
  -      System.out.println(oname.getKeyPropertyListString());
  -      ObjectInstance oi = server.registerMBean(new Dynamic(), oname);
  -      
  -      long time = System.currentTimeMillis();
  -      for (int i = 0; i < 1000000; ++i)
  -         server.invoke(oname, "methodInvocation", null, null);
  -      long end = System.currentTimeMillis();
  -      
  -      System.out.println("1M invocations, DYNAMIC, no args, null return: " + (end - 
time));
  -
  -
  -      
  -      server.registerMBean(new DynamicUser(), new ObjectName("Test:test=dynamic"));
  -
  -      String name = (String)server.getAttribute(new 
ObjectName("Test:test=dynamic"), "Name");
  -      System.out.println("Name: " + name);
  -
  -      server.setAttribute(new ObjectName("Test:test=dynamic"), new 
Attribute("Name", "Juha"));
  -      name = (String)server.getAttribute(new ObjectName("Test:test=dynamic"), 
"Name");
  -      System.out.println("Name: " + name);
  -
  -      oname = new ObjectName("Speed:name=mymbean");
  -      server.registerMBean(new DynamicUser(), oname);
  -      String info = (String)server.invoke(oname, "printInfo", null, null);
  -      System.out.println(info);
  -
  -
  -      // simple speed test, invoke empty methodInvocation 1 million times
  -      oname = new ObjectName("Speed:name=mymbean,class=Dynamic");
  -      
  -      time = System.currentTimeMillis();
  -      for (int i = 0; i < 1000000; ++i)
  -         server.invoke(oname, "methodInvocation", null, null);
  -      end = System.currentTimeMillis();
  -      
  -      System.out.println("1M invocations, DYNAMIC, no args, null return: " + (end - 
time));
  -
  -
  -
  -      // simple speed test, invoke printInfo 1 million times
  -      oname = new ObjectName("Speed:name=mymbean");
  -      
  -      time = System.currentTimeMillis();
  -      for (int i = 0; i < 1000000; ++i)
  -         server.invoke(oname, "printInfo", null, null);
  -      end = System.currentTimeMillis();
  -      
  -      System.out.println("1M invocations, DYNAMIC, no args, return printInfo 
string: " + (end - time));
  -      
  -      
  -      server.registerMBean(new SuperStandardDynamic(), new 
ObjectName("standard:name=superStandardDynamic"));
  -      server.registerMBean(new StandardDynamic(), new 
ObjectName("standard:name=standardDynamic"));
  -      server.registerMBean(new RealDynamic(), new ObjectName(":name=realDynamic"));
  -      
  -      
  -      // simple speed test, invoke printInfo million times (STANDARD MBEAN)
  -      oname = new ObjectName("standard:name=user");
  -      time = System.currentTimeMillis();
  -      for (int i = 0; i < 1000000; ++i)
  -         server.invoke(oname, "printInfo", null, null);
  -      end = System.currentTimeMillis();
  -      System.out.println("1M invocations, STANDARD, no args, return printInfo 
string: " + (end - time));
  -
  -      // simple speed test, invoke no args, no return methodInvocation 1 million 
times (STANDARD MBEAN)
  -      oname = new ObjectName("standard:name=superStandardDynamic");
  -      time = System.currentTimeMillis();
  -      for (int i = 0; i < 1000000; ++i)
  -         server.invoke(oname, "methodInvocation", null, null);
  -      end = System.currentTimeMillis();
  -      System.out.println("1M invocations, STANDARD, no args, no return: " + (end - 
time));
  -      
  -      oname = new ObjectName("standard:name=user");
  -      server.invoke(oname, "addPhoneNumber", new Object[] { "555-1234" }, new 
String[] { String.class.getName() });
  -      
  -      System.out.println(server.invoke(oname, "printInfo", null, null));
  -      
  -      
  -      server.setAttribute(oname, new Attribute("Address", "Strawberry Street"));
  -      server.setAttribute(oname, new Attribute("Password", "abcdefg"));
  -      System.out.println(server.getAttribute(oname, "Address"));
  -      System.out.println(server.invoke(oname, "printInfo", null, null));
  -      
  -      server.setAttribute(oname, new Attribute("PhoneNumbers", new String[] { 
"123-345", "123-422", "123-5242" }));
  -      System.out.println(server.invoke(oname, "printInfo", null, null));
  -   
  -      } catch (Throwable t) {
  -      if (t instanceof MBeanException) {
  -         System.out.println(t.getMessage());
  -         ((MBeanException)t).getTargetException().printStackTrace();
  +         else
  +            t.printStackTrace();
         }
      }
   
  -   }
   
  -   public static interface MyRealInterface extends DynamicMBean {}
  -   
  -   public static class RealDynamic extends SuperStandardDynamic implements 
MyRealInterface {
  -   }
  -   
  -   public static interface SuperStandardDynamicMBean {
  -     void methodInvocation();
  -   }
  -   
  -   public static interface MyInterface extends SuperStandardDynamicMBean {
  -      
  -   }
  -   
  -   public static class SuperStandardDynamic extends Dynamic implements MyInterface {
  -      public void methodInvocation() {}
  +   public static interface MyRealInterface extends DynamicMBean
  +   {}
  +
  +   public static class RealDynamic extends SuperStandardDynamic implements 
MyRealInterface
  +      {}
  +
  +   public static interface SuperStandardDynamicMBean
  +   {
  +      void methodInvocation();
      }
  -   
  -   static interface StandardDynamicMBean { }
  -   
  -   public static class StandardDynamic extends Dynamic implements 
StandardDynamicMBean {
  -      
  +
  +   public static interface MyInterface extends SuperStandardDynamicMBean
  +      {}
  +
  +   public static class SuperStandardDynamic extends Dynamic implements MyInterface
  +   {
  +      public void methodInvocation()
  +      {}
      }
  -   
  -   public static class Dynamic implements DynamicMBean {
   
  +   static interface StandardDynamicMBean
  +      { }
  +
  +   public static class StandardDynamic extends Dynamic implements 
StandardDynamicMBean
  +      {}
  +
  +   public static class Dynamic implements DynamicMBean
  +   {
  +
         public java.lang.Object getAttribute(java.lang.String attribute)
         throws AttributeNotFoundException,
                  MBeanException,
  -         ReflectionException {
  +               ReflectionException
  +      {
   
            return null;
         }
  @@ -170,22 +202,27 @@
         throws AttributeNotFoundException,
                  InvalidAttributeValueException,
                  MBeanException,
  -         ReflectionException {}
  +               ReflectionException
  +         {}
   
  -      public AttributeList getAttributes(java.lang.String[] attributes) {
  +      public AttributeList getAttributes(java.lang.String[] attributes)
  +      {
            return null;
         }
   
  -      public AttributeList setAttributes(AttributeList attributes) {
  +      public AttributeList setAttributes(AttributeList attributes)
  +      {
            return null;
         }
   
         public java.lang.Object invoke(java.lang.String actionName,
                                        java.lang.Object[] params, java.lang.String[] 
signature)
         throws MBeanException,
  -         ReflectionException {
  +               ReflectionException
  +      {
   
  -         if (actionName.equals("methodInvocation")) {
  +         if (actionName.equals("methodInvocation"))
  +         {
               methodInvocation();
               return null;
            }
  @@ -193,7 +230,8 @@
            return null;
         }
   
  -      public MBeanInfo getMBeanInfo() {
  +      public MBeanInfo getMBeanInfo()
  +      {
   
            return new MBeanInfo("jmx.book.examples.Dynamic", "", null,
                                 new MBeanConstructorInfo[] { new 
MBeanConstructorInfo("default", "", null) },
  @@ -201,10 +239,12 @@
                                 null);
         }
   
  -      private void methodInvocation() {}
  +      private void methodInvocation()
  +      {}
      }
   
  -   public static class DynamicUser implements DynamicMBean {
  +   public static class DynamicUser implements DynamicMBean
  +   {
   
   
         // mgmt attribute name constants
  @@ -234,7 +274,8 @@
         public Object getAttribute(String attribute) throws
                  AttributeNotFoundException,
                  MBeanException,
  -         ReflectionException {
  +               ReflectionException
  +      {
   
            if (attribute == null || attribute.equals(""))
               throw new IllegalArgumentException(
  @@ -262,19 +303,24 @@
   
         // getAttributes implementation
   
  -      public AttributeList getAttributes(String[] attributes) {
  +      public AttributeList getAttributes(String[] attributes)
  +      {
   
            if (attributes == null)
               throw new IllegalArgumentException("null array");
   
            AttributeList list = new AttributeList();
   
  -         for (int i = 0; i < attributes.length; ++i) {
  -            try {
  +         for (int i = 0; i < attributes.length; ++i)
  +         {
  +            try
  +            {
                  list.add(new Attribute(
                              attributes[i], getAttribute(attributes[i])
                           ));
  -            } catch (JMException ignored) {
  +            }
  +            catch (JMException ignored)
  +            {
                  // if the attribute could not be retrieved, skip it
               }
            }
  @@ -289,14 +335,16 @@
                  AttributeNotFoundException,
                  InvalidAttributeValueException,
                  MBeanException,
  -         ReflectionException {
  +               ReflectionException
  +      {
   
            if (attribute == null)
               throw new
               AttributeNotFoundException("null attribute");
   
            // map attributes to fields
  -         try {
  +         try
  +         {
               if (attribute.getName().equals(NAME))
                  this.name = (String)attribute.getValue();
   
  @@ -313,7 +361,9 @@
                  throw new AttributeNotFoundException(
                     "attribute "+attribute.getName()+" not found."
                  );
  -         } catch (ClassCastException e) {
  +         }
  +         catch (ClassCastException e)
  +         {
               throw new InvalidAttributeValueException(
                  "Invalid attribute type " +
                  attribute.getValue().getClass().getName()
  @@ -324,7 +374,8 @@
   
         // setAttributes implementation
   
  -      public AttributeList setAttributes(AttributeList list) {
  +      public AttributeList setAttributes(AttributeList list)
  +      {
   
            if (list == null)
               throw new IllegalArgumentException("null list");
  @@ -332,12 +383,16 @@
            AttributeList results = new AttributeList();
            Iterator it           = list.iterator();
   
  -         while (it.hasNext()) {
  -            try {
  +         while (it.hasNext())
  +         {
  +            try
  +            {
                  Attribute attr = (Attribute)it.next();
                  setAttribute(attr);
                  results.add(attr);
  -            } catch (JMException ignored) {
  +            }
  +            catch (JMException ignored)
  +            {
                  // if unable to set the attribute, skip it
               }
            }
  @@ -352,7 +407,8 @@
                              Object[] params,
                              String[] signature)
         throws MBeanException,
  -         ReflectionException {
  +               ReflectionException
  +      {
   
            if (actionName == null || actionName.equals(""))
               throw new IllegalArgumentException("no operation");
  @@ -361,11 +417,13 @@
            if (actionName.equals(PRINT_INFO))
               return printInfo();
   
  -         else if (actionName.equals(ADD_NUMBER)) {
  +         else if (actionName.equals(ADD_NUMBER))
  +         {
               addPhoneNumber((String)params[0]);
               return null;
            }
  -         else if (actionName.equals(REMOVE_NUMBER)) {
  +         else if (actionName.equals(REMOVE_NUMBER))
  +         {
               removePhoneNumber(((Integer)params[0]).intValue());
               return null;
            }
  @@ -378,7 +436,8 @@
   
         // getMBeanInfo implementation
   
  -      public MBeanInfo getMBeanInfo() {
  +      public MBeanInfo getMBeanInfo()
  +      {
   
            final boolean READABLE  = true;
            final boolean WRITABLE  = true;
  @@ -506,7 +565,8 @@
         }
   
         // management operation implementations
  -      public String printInfo() {
  +      public String printInfo()
  +      {
            return
               "User: " + name +"\n"+
               "Address: " + address +"\n"+
  @@ -515,15 +575,18 @@
               "Phone #: " + numbers[2] +"\n";
         }
   
  -      public void addPhoneNumber(String number) {
  +      public void addPhoneNumber(String number)
  +      {
            for (int i = 0; i < numbers.length; ++i)
  -            if (numbers[i] == null) {
  +            if (numbers[i] == null)
  +            {
                  numbers[i] = number;
                  break;
               }
         }
   
  -      public void removePhoneNumber(int index) {
  +      public void removePhoneNumber(int index)
  +      {
            if (index < 0 || index >= numbers.length)
               return;
   
  
  
  

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

Reply via email to