Samples JDK
Server.java
1 package com.freemindcafe.jmx.sample2.server;
2 
3 import java.io.IOException;
4 import java.lang.management.ManagementFactory;
5 import java.net.ServerSocket;
6 import java.rmi.registry.LocateRegistry;
7 import java.rmi.registry.Registry;
8 import java.util.HashMap;
9 import java.util.Random;
10 
11 import javax.management.MBeanServer;
12 import javax.management.MBeanServerFactory;
13 import javax.management.ObjectName;
14 import javax.management.remote.JMXConnectorServer;
15 import javax.management.remote.JMXConnectorServerFactory;
16 import javax.management.remote.JMXServiceURL;
17 import javax.management.remote.rmi.RMIConnectorServer;
18 
19 import com.sun.jndi.rmi.registry.RegistryContextFactory;
20 
21 public class Server {
22 
23  private static final Random random = new Random();
24 
25  public static void main(String[] args)
26  throws Exception {
27 
28  int jmxPort = 1919;
29  LocateRegistry.createRegistry(jmxPort);
30 
31  //MBeanServer mBeanServer = MBeanServerFactory.newMBeanServer("ENERGYIPAPP");
32  MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
33 
34  // create JMXConnectorServer MBean
35  JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:"+jmxPort+"/EIPRMIConnector");
36  HashMap<String, String> environment = new HashMap<String, String>();
37  environment.put("java.naming.factory.initial", RegistryContextFactory.class.getName());
38  environment.put(RMIConnectorServer.JNDI_REBIND_ATTRIBUTE,"true");
39  JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(address, environment, mBeanServer);
40  connector.start();
41 
42  ObjectName mbeanName = new ObjectName("com.eMeter.localhost.appName.appInstance:name=EIP.IHelloService");
43  HelloService helloService = new HelloService();
44  mBeanServer.registerMBean(helloService, mbeanName);
45 
46  System.out.println("Waiting forever...");
47  Thread.sleep(Long.MAX_VALUE);
48  }
49 
50  private static int findFreePort(int minPortNumber, int maxPortNumber) {
51  if (minPortNumber >= maxPortNumber) {
52  throw new RuntimeException("Invalid port number range min ["
53  + minPortNumber + "] max [" + maxPortNumber + "]");
54  }
55  for (int i = 0; i < 20; i++) {
56  int port = createAcceptablePort(minPortNumber, maxPortNumber);
57  if (checkPortIsFree(port)) {
58  return port;
59  }
60  }
61  throw new RuntimeException("Unable to find a free port");
62  }
63 
64  private static int createAcceptablePort(int minPortNumer, int maxPortNumber) {
65  synchronized (random) {
66  int randomInt = random.nextInt();
67  final int portWithoutOffset = Math.abs(randomInt
68  % (maxPortNumber - minPortNumer + 1));
69  return portWithoutOffset + minPortNumer;
70  }
71 
72  }
73 
74  private static boolean checkPortIsFree(int port) {
75  ServerSocket socket;
76  try {
77  socket = new ServerSocket(port);
78  int localPort = socket.getLocalPort();
79  socket.close();
80  return true;
81  } catch (IOException e) {
82  return false;
83  }
84  }
85 }