KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > simple > Server


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.remote.simple;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14 import javax.management.remote.JMXConnectorServer JavaDoc;
15 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
16 import javax.management.remote.JMXServiceURL JavaDoc;
17
18 /**
19  * This example shows the simplest way to setup a JSR 160 connector server.
20  * It uses the standard JSR 160 RMIConnectorServer, and if you're familiar with
21  * RMI, you'll know that a JNDI server like the rmiregistry is needed
22  * in order to register the server stub that will be looked up by the client.
23  *
24  * @version $Revision: 1.1 $
25  */

26 public class Server
27 {
28    public static void main(String JavaDoc[] args) throws Exception JavaDoc
29    {
30       // The MBeanServer
31
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
32
33       // Register and start the rmiregistry MBean, needed by JSR 160 RMIConnectorServer
34
ObjectName JavaDoc namingName = ObjectName.getInstance("naming:type=rmiregistry");
35       server.createMBean("mx4j.tools.naming.NamingService", namingName, null);
36       server.invoke(namingName, "start", null, null);
37       int namingPort = ((Integer JavaDoc)server.getAttribute(namingName, "Port")).intValue();
38
39       String JavaDoc jndiPath = "/jmxconnector";
40       JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi://localhost/jndi/rmi://localhost:" + namingPort + jndiPath);
41
42       // Create and start the RMIConnectorServer
43
JMXConnectorServer JavaDoc connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
44       connectorServer.start();
45
46       System.out.println("Server up and running");
47    }
48 }
49
Popular Tags