@Override
  public void init() throws ServletException {

    LOG.info("enter--init");

    super.init();
    ArrayList<MBeanServer> serverList = MBeanServerFactory.findMBeanServer(null);
    MBeanServer serverHelloWorld = (MBeanServer) serverList.get(0);
    final HelloWorldService helloWorldMBean = new HelloWorldService();
    ObjectName helloWorld = null;
    try {
      helloWorld = new ObjectName("jboss.jmx:name=HelloJMX");
      serverHelloWorld.registerMBean(helloWorldMBean, helloWorld);
    } catch (MalformedObjectNameException e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    } catch (InstanceAlreadyExistsException e) {
      e.printStackTrace();
    } catch (MBeanRegistrationException e) {
      e.printStackTrace();
    } catch (NotCompliantMBeanException e) {
      e.printStackTrace();
    }

    LOG.info("exit--init");
  }
  public void start() {
    this.mbeanServer = ManagementFactory.getPlatformMBeanServer();

    try {
      platformMonitor = new ObjectName(OBJECT_NAME);
      this.mbeanServer.registerMBean(this, platformMonitor);

    } catch (MalformedObjectNameException e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    } catch (InstanceAlreadyExistsException e) {
      e.printStackTrace();
    } catch (MBeanRegistrationException e) {
      e.printStackTrace();
    } catch (NotCompliantMBeanException e) {
      e.printStackTrace();
    }
  }
    public void registerCircuitBreaker(final CircuitBreaker circuitBreaker, final String name)
        throws JMException {
      ObjectName mbeanObjectName = null;

      ObjectName serviceName = Qi4jMBeans.findServiceName(server, application.name(), name);
      if (serviceName != null) {
        mbeanObjectName = new ObjectName(serviceName.toString() + ",name=Circuit breaker");
      } else {
        try {
          mbeanObjectName = new ObjectName("CircuitBreaker:name=" + name);
        } catch (MalformedObjectNameException e) {
          throw new IllegalArgumentException("Illegal name:" + name);
        }
      }

      CircuitBreakerJMX bean = new CircuitBreakerJMX(circuitBreaker, mbeanObjectName);

      try {
        server.registerMBean(bean, mbeanObjectName);
        registeredCircuitBreakers.put(circuitBreaker, mbeanObjectName);
      } catch (InstanceAlreadyExistsException e) {
        e.printStackTrace();
      } catch (MBeanRegistrationException e) {
        e.printStackTrace();
      } catch (NotCompliantMBeanException e) {
        e.printStackTrace();
      }

      // Add logger
      circuitBreaker.addPropertyChangeListener(
          new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
              if (evt.getPropertyName().equals("status")) {
                if (evt.getNewValue().equals(CircuitBreaker.Status.on))
                  LoggerFactory.getLogger(CircuitBreakerManagement.class)
                      .info("Circuit breaker " + name + " is now on");
                else
                  LoggerFactory.getLogger(CircuitBreakerManagement.class)
                      .error("Circuit breaker " + name + " is now off");
              }
            }
          });
    }
  /**
   * Starts the MBean
   *
   * @return
   */
  public static boolean create(LocationService locationService) {

    MBeanServer mbs = SleeContainer.lookupFromJndi().getMBeanServer();
    ObjectName on = null;
    try {
      on = new ObjectName(MBEAN_NAME);
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      if (mbs.getObjectInstance(on) != null) {
        mbs.unregisterMBean(on);
      }
    } catch (InstanceNotFoundException e) {
      // ignore
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    try {
      mbs.registerMBean(new LocationServiceManagement(locationService), on);
    } catch (InstanceAlreadyExistsException e) {

      e.printStackTrace();
      return false;
    } catch (MBeanRegistrationException e) {

      e.printStackTrace();
      return false;
    } catch (NotCompliantMBeanException e) {

      e.printStackTrace();
      return false;
    }

    return true;
  }
Ejemplo n.º 5
0
 /**
  * Registers a logger to the management server.
  *
  * @param logger A logger to be registered for management.
  */
 public static synchronized void register(Logger logger) {
   try {
     ManagementFactory.getPlatformMBeanServer()
         .createMBean(
             "com.hapiware.jmx.log4j.Logging",
             new ObjectName(LOGGING_NAME),
             new Object[] {logger.getLoggerRepository()},
             new String[] {"org.apache.log4j.spi.LoggerRepository"});
   } catch (MBeanException e) {
     e.printStackTrace();
   } catch (InstanceAlreadyExistsException e) {
     e.printStackTrace();
   } catch (NotCompliantMBeanException e) {
     e.printStackTrace();
   } catch (MalformedObjectNameException e) {
     e.printStackTrace();
   } catch (ReflectionException e) {
     e.printStackTrace();
   } catch (NullPointerException e) {
     e.printStackTrace();
   }
 }
  public static PRTGInterface getAndRegisterBean(String beanname) {
    // Find an agent from this JVM. Null argument will return
    // a list of all MBeanServer instances.
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();

    ObjectName name = getJMXObjectName(getObjectName(beanname));
    PRTGInterface retVal = getBean(beanname);
    try {
      if (server.isRegistered(name)) {
        server.unregisterMBean(name);
      }
      // register the MBean
      server.registerMBean(retVal, name);

      // Invoke the printInfo operation on an
      // uninitialized MBean instance.
      Object result =
          server.invoke(
              name, // MBean name
              "printInfo", // operation name
              null, // no parameters
              null // void signature
              );
    } catch (InstanceNotFoundException e) {
      e.printStackTrace();
    } catch (MBeanException e) {
      e.getTargetException().printStackTrace();
    } catch (ReflectionException e) {
      e.printStackTrace();
    } catch (InstanceAlreadyExistsException e) {
      e.printStackTrace();
    } catch (NotCompliantMBeanException e) {
      e.printStackTrace();
    }
    return retVal;
  }