Example usage for javax.management MBeanServer registerMBean

List of usage examples for javax.management MBeanServer registerMBean

Introduction

In this page you can find the example usage for javax.management MBeanServer registerMBean.

Prototype

public ObjectInstance registerMBean(Object object, ObjectName name)
        throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException;

Source Link

Document

Registers a pre-existing object as an MBean with the MBean server.

Usage

From source file:com.twitter.hraven.hadoopJobMonitor.jmx.WhiteList.java

public static void main(String[] args) throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("com.twitter.hraven.hadoopJobMonitor.jmx:type=WhiteList");
    WhiteList.init("/tmp");
    WhiteList mbean = WhiteList.getInstance();
    mbs.registerMBean(mbean, name);
    System.out.println("Waiting forever...");
    Thread.sleep(Long.MAX_VALUE);
}

From source file:com.app.server.Main.java

/**
 * This is the start of the all the services in web server
 * @param args/*from ww  w.  j a  v a2s. c om*/
 * @throws IOException 
 * @throws SAXException 
 */
public static void main(String[] args) throws IOException, SAXException {

    long startTime = System.currentTimeMillis();
    Hashtable urlClassLoaderMap = new Hashtable();
    Hashtable executorServicesMap = new Hashtable();
    Hashtable ataMap = new Hashtable<String, ATAConfig>();
    Hashtable messagingClassMap = new Hashtable();
    ConcurrentHashMap servletMapping = new ConcurrentHashMap();
    Vector deployerList = new Vector(), serviceList = new Vector();
    DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {
        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/serverconfig-rules.xml")));
            } catch (FileNotFoundException e) {
                log.error("Error in loading the xml rules ./config/serverconfig-rules.xml", e);
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }

        }
    });
    Digester serverdigester = serverdigesterLoader.newDigester();
    final ServerConfig serverconfig = (ServerConfig) serverdigester
            .parse(new InputSource(new FileInputStream("./config/serverconfig.xml")));

    //log.info(messagingconfig);
    ////log.info(serverconfig.getDeploydirectory());
    PropertyConfigurator.configure("log4j.properties");
    /*MemcachedClient cache=new MemcachedClient(
        new InetSocketAddress("localhost", 1000));*/

    // Store a value (async) for one hour
    //c.set("someKey", 36, new String("arun"));
    // Retrieve a value.        
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    ExecutorService executor = java.util.concurrent.Executors.newCachedThreadPool();

    MBeanServer mbs = MBeanServerFactory.createMBeanServer("singham");
    ObjectName name = null;
    try {
        mbs.registerMBean(serverconfig, new ObjectName("com.app.server:type=serverinfo"));
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    /*try {
       name = new ObjectName("com.app.server:type=WarDeployer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    WarDeployer warDeployer=new WarDeployer(serverconfig.getDeploydirectory(),serverconfig.getFarmWarDir(),serverconfig.getClustergroup(),urlClassLoaderMap,executorServicesMap,messagingClassMap,servletMapping,messagingconfig,sessionObjects);
    warDeployer.setPriority(MIN_PRIORITY);
    try {
       mbs.registerMBean(warDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    //warDeployer.start();
    executor.execute(warDeployer);*/

    final byte[] shutdownBt = new byte[50];
    /*WebServerRequestProcessor webserverRequestProcessor=new WebServer().new WebServerRequestProcessor(servletMapping,urlClassLoaderMap,serverSocketChannel,serverconfig.getDeploydirectory(),Integer.parseInt(serverconfig.getShutdownport()),1);
      webserverRequestProcessor.setPriority(MIN_PRIORITY);
    try {
       name = new ObjectName("com.app.server:type=WebServerRequestProcessor");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(webserverRequestProcessor, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //webserverRequestProcessor.start();
      executor.execute(webserverRequestProcessor);
            
            
      for(int i=0;i<10;i++){
       WebServerRequestProcessor webserverRequestProcessor1=new WebServer().new WebServerRequestProcessor(servletMapping,urlClassLoaderMap,serverSocketChannel,serverconfig.getDeploydirectory(),Integer.parseInt(serverconfig.getShutdownport()),2);
    webserverRequestProcessor1.setPriority(MIN_PRIORITY);
       try {
    name = new ObjectName("com.app.server:type=WebServerRequestProcessor"+(i+1));
       } catch (MalformedObjectNameException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
       } 
               
       try {
    mbs.registerMBean(webserverRequestProcessor1, name);
       } catch (InstanceAlreadyExistsException | MBeanRegistrationException
       | NotCompliantMBeanException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
       }
               
         executor.execute(webserverRequestProcessor1);
      }*/

    /*ExecutorServiceThread executorService=new ExecutorServiceThread(serverSocketChannelServices,executorServicesMap,Integer.parseInt(serverconfig.getShutdownport()),ataMap,urlClassLoaderMap,serverconfig.getDeploydirectory(),serverconfig.getServicesdirectory(),serverconfig.getEarservicesdirectory(),serverconfig.getNodesport());
            
    try {
       name = new ObjectName("com.app.services:type=ExecutorServiceThread");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(executorService, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //executorService.start();
    executor.execute(executorService);
            
            
    for(int i=0;i<10;i++){
       ExecutorServiceThread executorService1=new ExecutorServiceThread(serverSocketChannelServices,executorServicesMap,Integer.parseInt(serverconfig.getShutdownport()),ataMap,urlClassLoaderMap,serverconfig.getDeploydirectory(),serverconfig.getServicesdirectory(),serverconfig.getEarservicesdirectory(),serverconfig.getNodesport());
               
       try {
    name = new ObjectName("com.app.services:type=ExecutorServiceThread"+(i+1));
       } catch (MalformedObjectNameException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
       } 
               
       try {
    mbs.registerMBean(executorService1, name);
       } catch (InstanceAlreadyExistsException | MBeanRegistrationException
       | NotCompliantMBeanException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
       }
               
       executor.execute(executorService1);
    }*/

    /*WebServerHttpsRequestProcessor webserverHttpsRequestProcessor=new WebServer().new WebServerHttpsRequestProcessor(servletMapping,urlClassLoaderMap,Integer.parseInt(serverconfig.getHttpsport()),serverconfig.getDeploydirectory(),Integer.parseInt(serverconfig.getShutdownport()),serverconfig.getHttpscertificatepath(),serverconfig.getHttpscertificatepasscode(),1);
    try {
       name = new ObjectName("com.app.server:type=WebServerHttpsRequestProcessor");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(webserverHttpsRequestProcessor, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    webserverHttpsRequestProcessor.setPriority(MAX_PRIORITY);
    //webserverRequestProcessor.start();
      executor.execute(webserverHttpsRequestProcessor);*/

    /* for(int i=0;i<2;i++){
        webserverHttpsRequestProcessor=new WebServer().new WebServerHttpsRequestProcessor(urlClassLoaderMap,Integer.parseInt(serverconfig.getHttpsport())+(i+1),serverconfig.getDeploydirectory(),Integer.parseInt(serverconfig.getShutdownport()),serverconfig.getHttpscertificatepath(),serverconfig.getHttpscertificatepasscode(),1);
              
      try {
    name = new ObjectName("com.app.server:type=WebServerHttpsRequestProcessor"+(i+1));
      } catch (MalformedObjectNameException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
      } 
              
      try {
    mbs.registerMBean(webserverHttpsRequestProcessor, name);
      } catch (InstanceAlreadyExistsException | MBeanRegistrationException
       | NotCompliantMBeanException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
      }
              
      executor.execute(webserverHttpsRequestProcessor);
    }*/

    /*ATAServer ataServer=new ATAServer(serverconfig.getAtaaddress(),serverconfig.getAtaport(),ataMap);
            
    try {
       name = new ObjectName("com.app.services:type=ATAServer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(ataServer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
            
    ataServer.start();*/

    /*ATAConfigClient ataClient=new ATAConfigClient(serverconfig.getAtaaddress(),serverconfig.getAtaport(),serverconfig.getServicesport(),executorServicesMap);
            
    try {
       name = new ObjectName("com.app.services:type=ATAConfigClient");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(ataClient, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    ataClient.start();*/

    /*MessagingServer messageServer=new MessagingServer(serverconfig.getMessageport(),messagingClassMap);
            
    try {
       name = new ObjectName("com.app.messaging:type=MessagingServer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(messageServer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    //messageServer.start();
    executor.execute(messageServer);*/

    /*RandomQueueMessagePicker randomqueuemessagepicker=new RandomQueueMessagePicker(messagingClassMap);
            
    try {
       name = new ObjectName("com.app.messaging:type=RandomQueueMessagePicker");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(randomqueuemessagepicker, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //randomqueuemessagepicker.start();
    executor.execute(randomqueuemessagepicker);*/

    /*RoundRobinQueueMessagePicker roundrobinqueuemessagepicker=new RoundRobinQueueMessagePicker(messagingClassMap);
            
    try {
       name = new ObjectName("com.app.messaging:type=RoundRobinQueueMessagePicker");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(roundrobinqueuemessagepicker, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //roundrobinqueuemessagepicker.start();
    executor.execute(roundrobinqueuemessagepicker);*/

    /*TopicMessagePicker topicpicker= new TopicMessagePicker(messagingClassMap);
            
    try {
       name = new ObjectName("com.app.messaging:type=TopicMessagePicker");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(topicpicker, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    //topicpicker.start();
    executor.execute(topicpicker);*/

    try {
        name = new ObjectName(SARDeployer.OBJECT_NAME);
    } catch (Exception e) {
        log.error("Error in creating the object name " + SARDeployer.OBJECT_NAME, e);
        // TODO Auto-generated catch block
        //e1.printStackTrace();
    }
    log.info(System.getProperty("user.dir"));
    try {
        mbs.createMBean("com.app.server.SARDeployer", name);
        mbs.invoke(name, "init", new Object[] { deployerList }, new String[] { Vector.class.getName() });
        mbs.invoke(name, "init", new Object[] { serviceList, serverconfig, mbs }, new String[] {
                Vector.class.getName(), ServerConfig.class.getName(), MBeanServer.class.getName() });
        mbs.invoke(name, "start", null, null);
        mbs.invoke(name, "deploy",
                new Object[] {
                        new URL("file:///" + new File("").getAbsolutePath() + "/config/mbean-service.xml") },
                new String[] { URL.class.getName() });
        /*name=new ObjectName("com.app.server:type=deployer,service=WARDeployer");
        mbs.invoke(name, "deploy", new Object[]{new URL("file:///"+serverconfig.getDeploydirectory()+"/TestDollar2.war")}, new String[]{URL.class.getName()});
        mbs.invoke(name, "deploy", new Object[]{new URL("file:///"+serverconfig.getDeploydirectory()+"/StrutsProj.war")}, new String[]{URL.class.getName()});*/
        //mbs.registerMBean("", name);
    } catch (Exception ex) {
        log.error("Error in SAR DEPLOYER ", ex);
        // TODO Auto-generated catch block
        //e1.printStackTrace();
    }

    //scanner.deployPackages(new File[]{new File(serverconfig.getDeploydirectory()+"/AppServer.war")});

    try {
        new NodeResourceReceiver(mbs).start();
    } catch (Exception ex) {
        log.error("Node Receiver start", ex);
        //e2.printStackTrace();
    }
    long endTime = System.currentTimeMillis();
    log.info("Server started in " + ((endTime - startTime) / 1000) + " seconds");
    /*try {
       mbs.invoke(name, "startDeployer", null, null);
    } catch (InstanceNotFoundException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } catch (ReflectionException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } catch (MBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    */
    /*JarDeployer jarDeployer=new JarDeployer(registry,serverconfig.getServicesdirectory(), serverconfig.getServiceslibdirectory(),serverconfig.getCachedir(),executorServicesMap, urlClassLoaderMap);
    try {
       name = new ObjectName("com.app.server:type=JarDeployer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(jarDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //jarDeployer.start();
    executor.execute(jarDeployer);*/

    /*EARDeployer earDeployer=new EARDeployer(registry,serverconfig.getEarservicesdirectory(),serverconfig.getDeploydirectory(),executorServicesMap, urlClassLoaderMap,serverconfig.getCachedir(),warDeployer);
    try {
       name = new ObjectName("com.app.server:type=EARDeployer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(earDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //earDeployer.start();
    executor.execute(earDeployer);*/

    /*JVMConsole jvmConsole=new JVMConsole(Integer.parseInt(serverconfig.getJvmConsolePort()));
            
    try {
       name = new ObjectName("com.app.server:type=JVMConsole");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(jvmConsole, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    executor.execute(jvmConsole);*/

    /*ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
     XMLDeploymentScanner xmlDeploymentScanner =new XMLDeploymentScanner(serverconfig.getDeploydirectory(),serverconfig.getServiceslibdirectory());
    exec.scheduleAtFixedRate(xmlDeploymentScanner, 0, 1000, TimeUnit.MILLISECONDS);*/

    /*EmbeddedJMS embeddedJMS=null;
    try {
       embeddedJMS=new EmbeddedJMS();
       embeddedJMS.start();
    } catch (Exception ex) {
       // TODO Auto-generated catch block
       ex.printStackTrace();
    }   
             
     EJBDeployer ejbDeployer=new EJBDeployer(serverconfig.getServicesdirectory(),registry,Integer.parseInt(serverconfig.getServicesregistryport()),embeddedJMS);
    try {
       name = new ObjectName("com.app.server:type=EJBDeployer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(ejbDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //jarDeployer.start();
    executor.execute(ejbDeployer);*/

    new Thread() {
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(Integer.parseInt(serverconfig.getShutdownport()));
                while (true) {
                    Socket sock = serverSocket.accept();
                    InputStream istream = sock.getInputStream();
                    istream.read(shutdownBt);
                    String shutdownStr = new String(shutdownBt);
                    String[] shutdownToken = shutdownStr.split("\r\n\r\n");
                    //log.info(shutdownStr);
                    if (shutdownToken[0].startsWith("shutdown WebServer")) {
                        synchronized (shutDownObject) {
                            shutDownObject.notifyAll();
                        }
                    }
                }
            } catch (Exception e) {
                log.error("Shutdown error", e);
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }
        }
    }.start();

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            log.info("IN shutdown Hook");
            synchronized (shutDownObject) {
                shutDownObject.notifyAll();
            }
        }
    }));
    try {
        synchronized (shutDownObject) {
            shutDownObject.wait();
        }
        //executor.shutdownNow();
        //serverSocketChannelServices.close();
        //embeddedJMS.stop();

    } catch (Exception e) {
        log.error("Error in object wait ", e);
        // TODO Auto-generated catch block
        //e1.printStackTrace();
    }
    try {
        mbs.invoke(name, "stop", null, null);
        mbs.invoke(name, "destroy", null, null);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    log.info("Halting JVM");
    log.info("Exiting JVM");
    /*try{
       Thread.sleep(10000);
    }
    catch(Exception ex){
               
    }*/

    //webserverRequestProcessor.stop();
    //webserverRequestProcessor1.stop();

    /*warDeployer.stop();
    executorService.stop();
    //ataServer.stop();
    //ataClient.stop();
    messageServer.stop();
    randomqueuemessagepicker.stop();
    roundrobinqueuemessagepicker.stop();
    topicpicker.stop();*/
    /*try {
       mbs.invoke(new ObjectName("com.app.server:type=SARDeployer"), "destroyDeployer", null, null);
    } catch (InstanceNotFoundException | MalformedObjectNameException
    | ReflectionException | MBeanException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/
    //earDeployer.stop();
    System.exit(0);
}

From source file:com.web.server.WebServer.java

/**
 * This is the start of the all the services in web server
 * @param args/*  w  w  w. j  a  va2  s.  co m*/
 * @throws IOException 
 * @throws SAXException 
 */
public static void main(String[] args) throws IOException, SAXException {

    Hashtable urlClassLoaderMap = new Hashtable();
    Hashtable executorServicesMap = new Hashtable();
    Hashtable ataMap = new Hashtable<String, ATAConfig>();
    Hashtable messagingClassMap = new Hashtable();
    ConcurrentHashMap servletMapping = new ConcurrentHashMap();
    DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/serverconfig-rules.xml")));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
    Digester serverdigester = serverdigesterLoader.newDigester();
    final ServerConfig serverconfig = (ServerConfig) serverdigester
            .parse(new InputSource(new FileInputStream("./config/serverconfig.xml")));
    DigesterLoader messagingdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/messagingconfig-rules.xml")));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
    Digester messagingdigester = messagingdigesterLoader.newDigester();
    MessagingElem messagingconfig = (MessagingElem) messagingdigester
            .parse(new InputSource(new FileInputStream("./config/messaging.xml")));
    //System.out.println(messagingconfig);
    ////System.out.println(serverconfig.getDeploydirectory());
    PropertyConfigurator.configure("log4j.properties");
    /*MemcachedClient cache=new MemcachedClient(
        new InetSocketAddress("localhost", 1000));*/

    // Store a value (async) for one hour
    //c.set("someKey", 36, new String("arun"));
    // Retrieve a value.        
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    ExecutorService executor = java.util.concurrent.Executors.newCachedThreadPool();

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = null;
    try {
        name = new ObjectName("com.web.server:type=WarDeployer");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    WarDeployer warDeployer = new WarDeployer(serverconfig.getDeploydirectory(), serverconfig.getFarmWarDir(),
            serverconfig.getClustergroup(), urlClassLoaderMap, executorServicesMap, messagingClassMap,
            servletMapping, messagingconfig, sessionObjects);
    warDeployer.setPriority(MIN_PRIORITY);
    try {
        mbs.registerMBean(warDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //warDeployer.start();
    executor.execute(warDeployer);

    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

    serverSocketChannel.bind(new InetSocketAddress("0.0.0.0", Integer.parseInt(serverconfig.getPort())));

    serverSocketChannel.configureBlocking(false);

    final byte[] shutdownBt = new byte[50];
    WebServerRequestProcessor webserverRequestProcessor = new WebServer().new WebServerRequestProcessor(
            servletMapping, urlClassLoaderMap, serverSocketChannel, serverconfig.getDeploydirectory(),
            Integer.parseInt(serverconfig.getShutdownport()), 1);
    webserverRequestProcessor.setPriority(MIN_PRIORITY);
    try {
        name = new ObjectName("com.web.server:type=WebServerRequestProcessor");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(webserverRequestProcessor, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //webserverRequestProcessor.start();
    executor.execute(webserverRequestProcessor);

    for (int i = 0; i < 10; i++) {
        WebServerRequestProcessor webserverRequestProcessor1 = new WebServer().new WebServerRequestProcessor(
                servletMapping, urlClassLoaderMap, serverSocketChannel, serverconfig.getDeploydirectory(),
                Integer.parseInt(serverconfig.getShutdownport()), 2);
        webserverRequestProcessor1.setPriority(MIN_PRIORITY);
        try {
            name = new ObjectName("com.web.server:type=WebServerRequestProcessor" + (i + 1));
        } catch (MalformedObjectNameException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            mbs.registerMBean(webserverRequestProcessor1, name);
        } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        executor.execute(webserverRequestProcessor1);
    }

    ServerSocketChannel serverSocketChannelServices = ServerSocketChannel.open();

    serverSocketChannelServices
            .bind(new InetSocketAddress("0.0.0.0", Integer.parseInt(serverconfig.getServicesport())));

    serverSocketChannelServices.configureBlocking(false);

    ExecutorServiceThread executorService = new ExecutorServiceThread(serverSocketChannelServices,
            executorServicesMap, Integer.parseInt(serverconfig.getShutdownport()), ataMap, urlClassLoaderMap,
            serverconfig.getDeploydirectory(), serverconfig.getServicesdirectory(),
            serverconfig.getEarservicesdirectory(), serverconfig.getNodesport());

    try {
        name = new ObjectName("com.web.services:type=ExecutorServiceThread");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(executorService, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //executorService.start();
    executor.execute(executorService);

    for (int i = 0; i < 10; i++) {
        ExecutorServiceThread executorService1 = new ExecutorServiceThread(serverSocketChannelServices,
                executorServicesMap, Integer.parseInt(serverconfig.getShutdownport()), ataMap,
                urlClassLoaderMap, serverconfig.getDeploydirectory(), serverconfig.getServicesdirectory(),
                serverconfig.getEarservicesdirectory(), serverconfig.getNodesport());

        try {
            name = new ObjectName("com.web.services:type=ExecutorServiceThread" + (i + 1));
        } catch (MalformedObjectNameException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            mbs.registerMBean(executorService1, name);
        } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        executor.execute(executorService1);
    }

    WebServerHttpsRequestProcessor webserverHttpsRequestProcessor = new WebServer().new WebServerHttpsRequestProcessor(
            servletMapping, urlClassLoaderMap, Integer.parseInt(serverconfig.getHttpsport()),
            serverconfig.getDeploydirectory(), Integer.parseInt(serverconfig.getShutdownport()),
            serverconfig.getHttpscertificatepath(), serverconfig.getHttpscertificatepasscode(), 1);
    try {
        name = new ObjectName("com.web.server:type=WebServerHttpsRequestProcessor");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(webserverHttpsRequestProcessor, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    webserverHttpsRequestProcessor.setPriority(MAX_PRIORITY);
    //webserverRequestProcessor.start();
    executor.execute(webserverHttpsRequestProcessor);

    /* for(int i=0;i<2;i++){
        webserverHttpsRequestProcessor=new WebServer().new WebServerHttpsRequestProcessor(urlClassLoaderMap,Integer.parseInt(serverconfig.getHttpsport())+(i+1),serverconfig.getDeploydirectory(),Integer.parseInt(serverconfig.getShutdownport()),serverconfig.getHttpscertificatepath(),serverconfig.getHttpscertificatepasscode(),1);
              
      try {
    name = new ObjectName("com.web.server:type=WebServerHttpsRequestProcessor"+(i+1));
      } catch (MalformedObjectNameException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
      } 
              
      try {
    mbs.registerMBean(webserverHttpsRequestProcessor, name);
      } catch (InstanceAlreadyExistsException | MBeanRegistrationException
       | NotCompliantMBeanException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
      }
              
      executor.execute(webserverHttpsRequestProcessor);
    }*/

    /*ATAServer ataServer=new ATAServer(serverconfig.getAtaaddress(),serverconfig.getAtaport(),ataMap);
            
    try {
       name = new ObjectName("com.web.services:type=ATAServer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(ataServer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
            
    ataServer.start();*/

    /*ATAConfigClient ataClient=new ATAConfigClient(serverconfig.getAtaaddress(),serverconfig.getAtaport(),serverconfig.getServicesport(),executorServicesMap);
            
    try {
       name = new ObjectName("com.web.services:type=ATAConfigClient");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(ataClient, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    ataClient.start();*/

    MessagingServer messageServer = new MessagingServer(serverconfig.getMessageport(), messagingClassMap);

    try {
        name = new ObjectName("com.web.messaging:type=MessagingServer");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(messageServer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    //messageServer.start();
    executor.execute(messageServer);

    RandomQueueMessagePicker randomqueuemessagepicker = new RandomQueueMessagePicker(messagingClassMap);

    try {
        name = new ObjectName("com.web.messaging:type=RandomQueueMessagePicker");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(randomqueuemessagepicker, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //randomqueuemessagepicker.start();
    executor.execute(randomqueuemessagepicker);

    RoundRobinQueueMessagePicker roundrobinqueuemessagepicker = new RoundRobinQueueMessagePicker(
            messagingClassMap);

    try {
        name = new ObjectName("com.web.messaging:type=RoundRobinQueueMessagePicker");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(roundrobinqueuemessagepicker, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //roundrobinqueuemessagepicker.start();
    executor.execute(roundrobinqueuemessagepicker);

    TopicMessagePicker topicpicker = new TopicMessagePicker(messagingClassMap);

    try {
        name = new ObjectName("com.web.messaging:type=TopicMessagePicker");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(topicpicker, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    //topicpicker.start();
    executor.execute(topicpicker);

    try {
        name = new ObjectName("com.web.server:type=SARDeployer");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    SARDeployer sarDeployer = SARDeployer.newInstance(serverconfig.getDeploydirectory());
    try {
        mbs.registerMBean(sarDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    executor.execute(sarDeployer);
    /*try {
       mbs.invoke(name, "startDeployer", null, null);
    } catch (InstanceNotFoundException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } catch (ReflectionException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } catch (MBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
    */
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
    System.setProperty(Context.PROVIDER_URL, "rmi://localhost:" + serverconfig.getServicesregistryport());
    ;
    Registry registry = LocateRegistry.createRegistry(Integer.parseInt(serverconfig.getServicesregistryport()));

    /*JarDeployer jarDeployer=new JarDeployer(registry,serverconfig.getServicesdirectory(), serverconfig.getServiceslibdirectory(),serverconfig.getCachedir(),executorServicesMap, urlClassLoaderMap);
    try {
       name = new ObjectName("com.web.server:type=JarDeployer");
    } catch (MalformedObjectNameException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    } 
            
    try {
       mbs.registerMBean(jarDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException
    | NotCompliantMBeanException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    //jarDeployer.start();
    executor.execute(jarDeployer);*/

    EARDeployer earDeployer = new EARDeployer(registry, serverconfig.getEarservicesdirectory(),
            serverconfig.getDeploydirectory(), executorServicesMap, urlClassLoaderMap,
            serverconfig.getCachedir(), warDeployer);
    try {
        name = new ObjectName("com.web.server:type=EARDeployer");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(earDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //earDeployer.start();
    executor.execute(earDeployer);

    JVMConsole jvmConsole = new JVMConsole(Integer.parseInt(serverconfig.getJvmConsolePort()));

    try {
        name = new ObjectName("com.web.server:type=JVMConsole");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(jvmConsole, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    executor.execute(jvmConsole);

    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    XMLDeploymentScanner xmlDeploymentScanner = new XMLDeploymentScanner(serverconfig.getDeploydirectory(),
            serverconfig.getServiceslibdirectory());
    exec.scheduleAtFixedRate(xmlDeploymentScanner, 0, 1000, TimeUnit.MILLISECONDS);

    EmbeddedJMS embeddedJMS = null;
    try {
        embeddedJMS = new EmbeddedJMS();
        embeddedJMS.start();
    } catch (Exception ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    }

    EJBDeployer ejbDeployer = new EJBDeployer(serverconfig.getServicesdirectory(), registry,
            Integer.parseInt(serverconfig.getServicesregistryport()), embeddedJMS);
    try {
        name = new ObjectName("com.web.server:type=EJBDeployer");
    } catch (MalformedObjectNameException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        mbs.registerMBean(ejbDeployer, name);
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //jarDeployer.start();
    executor.execute(ejbDeployer);

    new Thread() {
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(Integer.parseInt(serverconfig.getShutdownport()));
                while (true) {
                    Socket sock = serverSocket.accept();
                    InputStream istream = sock.getInputStream();
                    istream.read(shutdownBt);
                    String shutdownStr = new String(shutdownBt);
                    String[] shutdownToken = shutdownStr.split("\r\n\r\n");
                    //System.out.println(shutdownStr);
                    if (shutdownToken[0].startsWith("shutdown WebServer")) {
                        synchronized (shutDownObject) {
                            shutDownObject.notifyAll();
                        }
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            System.out.println("IN shutdown Hook");
            synchronized (shutDownObject) {
                shutDownObject.notifyAll();
            }
        }
    }));
    try {
        synchronized (shutDownObject) {
            shutDownObject.wait();
        }
        executor.shutdownNow();
        serverSocketChannel.close();
        serverSocketChannelServices.close();
        embeddedJMS.stop();

    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    System.out.println("IN shutdown Hook1");
    /*try{
       Thread.sleep(10000);
    }
    catch(Exception ex){
               
    }*/

    //webserverRequestProcessor.stop();
    //webserverRequestProcessor1.stop();

    /*warDeployer.stop();
    executorService.stop();
    //ataServer.stop();
    //ataClient.stop();
    messageServer.stop();
    randomqueuemessagepicker.stop();
    roundrobinqueuemessagepicker.stop();
    topicpicker.stop();*/
    /*try {
       mbs.invoke(new ObjectName("com.web.server:type=SARDeployer"), "destroyDeployer", null, null);
    } catch (InstanceNotFoundException | MalformedObjectNameException
    | ReflectionException | MBeanException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }*/
    //earDeployer.stop();
    System.exit(0);
}

From source file:com.amazonaws.client.metrics.jmx.MBeans.java

/**
 * Registers the given MBean under the given object name to the first
 * registered MBean server, or the platform MBean server if there is no
 * explicitly registered MBean server.//from  ww  w  .  ja  v  a2 s .  c  o  m
 * 
 * @return true if the registration succeeded, or false if an MBean already
 *         exists under the given object name.
 * @throws MBeanRegistrationException
 *             The preRegister (MBeanRegistration interface) method of the
 *             MBean has thrown an exception. The MBean will not be
 *             registered.
 */
public static <T> boolean registerMBean(String objectName, T mbean) throws MBeanRegistrationException {
    MBeanServer server = getMBeanServer();
    try {
        server.registerMBean(mbean, new ObjectName(objectName));
    } catch (MalformedObjectNameException e) {
        throw new IllegalArgumentException(e);
    } catch (NotCompliantMBeanException e) {
        throw new IllegalArgumentException(e);
    } catch (InstanceAlreadyExistsException e) {
        LogFactory.getLog(MBeans.class).debug("Failed to register mbean " + objectName, e);
        return false;
    }
    return true;
}

From source file:org.apache.streams.util.ComponentUtils.java

/**
 * Attempts to register an object with local MBeanServer.  Throws runtime exception on errors.
 * @param name name to register bean with
 * @param mbean mbean to register/*from  ww  w . jav a  2s .  co m*/
 */
public static <V> void registerLocalMBean(String name, V mbean) {
    try {
        ObjectName objectName = new ObjectName(name);
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        mbs.registerMBean(mbean, objectName);
    } catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException
            | NotCompliantMBeanException e) {
        LOGGER.error("Failed to register MXBean : {}", e);
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Register the MBean using our standard MBeanName format
 * "hadoop:service=<serviceName>,name=<nameName>"
 * Where the <serviceName> and <nameName> are the supplied parameters
 *    /* w  ww  .jav  a 2  s  .  c o m*/
 * @param serviceName
 * @param nameName
 * @param theMbean - the MBean to register
 * @return the named used to register the MBean
 */
static public ObjectName registerMBean(final String serviceName, final String nameName, final Object theMbean) {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = getMBeanName(serviceName, nameName);
    try {
        mbs.registerMBean(theMbean, name);
        return name;
    } catch (InstanceAlreadyExistsException ie) {
        // Ignore if instance already exists 
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:co.runrightfast.core.utils.JmxUtils.java

static void registerApplicationMBean(final String domain, @NonNull final Object mbean,
        final Class<?> mbeanType) {
    final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    try {/* w  w  w.j  av  a 2 s . c  o m*/
        mbeanServer.registerMBean(mbean, applicationMBeanObjectName(domain, mbeanType));
    } catch (final InstanceAlreadyExistsException | MBeanRegistrationException
            | NotCompliantMBeanException ex) {
        throw new ApplicationException(
                String.format("registerApplicationMBean() failed for: %s", mbean.getClass().getName()), ex);
    }
}

From source file:com.zavakid.mushroom.util.MBeans.java

/**
 * Register the MBean using our standard MBeanName format "hadoop:service=<serviceName>,name=<nameName>" Where the
 * <serviceName> and <nameName> are the supplied parameters
 * /*w w w  . java2  s. c om*/
 * @param serviceName
 * @param nameName
 * @param theMbean - the MBean to register
 * @return the named used to register the MBean
 */
static public ObjectName register(String serviceName, String nameName, Object theMbean) {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = getMBeanName(serviceName, nameName);
    try {
        mbs.registerMBean(theMbean, name);
        return name;
    } catch (InstanceAlreadyExistsException ie) {
        LOG.warn(name, ie);
    } catch (Exception e) {
        LOG.warn("Error registering " + name, e);
    }
    return null;
}

From source file:org.apache.jxtadoop.metrics.util.MBeanUtil.java

/**
 * Register the MBean using our standard MBeanName format
 * "hadoop:service=<serviceName>,name=<nameName>"
 * Where the <serviceName> and <nameName> are the supplied parameters
 *    /* w  w w . j a  v a 2s . c o  m*/
 * @param serviceName
 * @param nameName
 * @param theMbean - the MBean to register
 * @return the named used to register the MBean
 */
static public ObjectName registerMBean(final String serviceName, final String nameName, final Object theMbean) {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

    ObjectName name = getMBeanName(serviceName, nameName);
    try {
        mbs.registerMBean(theMbean, name);
        return name;
    } catch (InstanceAlreadyExistsException ie) {
        // Ignore if instance already exists 
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.netflix.config.jmx.ConfigJMXManager.java

public static ConfigMBean registerConfigMbean(AbstractConfiguration config) {
    StandardMBean mbean = null;/*from  ww w .j av  a 2  s  .  c o  m*/
    ConfigMBean bean = null;
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        bean = new BaseConfigMBean(config);
        mbean = new StandardMBean(bean, ConfigMBean.class);
        mbs.registerMBean(mbean, getJMXObjectName(config, bean));
    } catch (NotCompliantMBeanException e) {
        throw new RuntimeException("NotCompliantMBeanException", e);
    } catch (InstanceAlreadyExistsException e) {
        throw new RuntimeException("InstanceAlreadyExistsException", e);
    } catch (MBeanRegistrationException e) {
        throw new RuntimeException("MBeanRegistrationException", e);
    } catch (Exception e) {
        throw new RuntimeException("registerConfigMbeanException", e);
    }
    return bean;
}