001: // $Id: JmxServer.java 1023 2007-03-16 16:27:41Z grro $
002: /*
003: * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
020: * The latest copy of this software may be found on http://www.xsocket.org/
021: */
022: package distributedcache;
023:
024: import java.io.IOException;
025: import java.lang.management.ManagementFactory;
026: import java.net.InetAddress;
027: import java.rmi.registry.LocateRegistry;
028: import java.rmi.registry.Registry;
029:
030: import javax.management.MBeanServer;
031: import javax.management.remote.JMXConnectorServer;
032: import javax.management.remote.JMXConnectorServerFactory;
033: import javax.management.remote.JMXServiceURL;
034:
035: /**
036: * Helper class to run a JMXConnectorServer by using rmi
037: *
038: * @author grro@xsocket.org
039: */
040: public final class JmxServer {
041:
042: private JMXConnectorServer server = null;
043:
044: /**
045: * start the server
046: *
047: * @param name the name space
048: */
049: public void start(String name) {
050: start(name, 1199);
051: }
052:
053: /**
054: * start the server
055: *
056: * @param name the name space
057: * @param rmiPort the rmi port to use
058: * @return jmxservice url
059: */
060: public JMXServiceURL start(String name, int rmiPort) {
061: try {
062: Registry registry = LocateRegistry.createRegistry(rmiPort);
063: registry.unbind(name);
064: } catch (Exception ignore) {
065: }
066:
067: try {
068: JMXServiceURL url = new JMXServiceURL(
069: "service:jmx:rmi:///jndi/rmi://"
070: + InetAddress.getLocalHost().getHostName()
071: + ":" + rmiPort + "/" + name);
072:
073: MBeanServer mbeanSrv = ManagementFactory
074: .getPlatformMBeanServer();
075: server = JMXConnectorServerFactory.newJMXConnectorServer(
076: url, null, mbeanSrv);
077: server.start();
078: System.out
079: .println("JMX RMI Agent has been bound on address");
080: System.out.println(url);
081:
082: return url;
083: } catch (Exception e) {
084: e.printStackTrace();
085: return null;
086: }
087: }
088:
089: /**
090: * stops the server
091: *
092: */
093: public void stop() {
094: try {
095: server.stop();
096: } catch (IOException ioe) {
097: // ignore
098: }
099: }
100: }
|