1 2 package examples.jmx; 3 4 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import javax.management.MBeanServerConnection ; 32 import javax.management.Notification ; 33 import javax.management.NotificationListener ; 34 import javax.management.ObjectName ; 35 import javax.management.remote.JMXConnectionNotification ; 36 import javax.management.remote.JMXConnector ; 37 import javax.management.remote.JMXConnectorFactory ; 38 import javax.management.remote.JMXServiceURL ; 39 import javax.naming.Context ; 40 41 import org.continuent.sequoia.common.jmx.JmxConstants; 42 43 50 public class SequoiaNotificationListener implements NotificationListener 51 { 52 53 private String port = "8091"; 54 55 private String host = "localhost"; 56 57 private String virtualDB = "rubis"; 58 59 private String adminUserName = "admin"; 60 61 private String adminPassword = "sequioa"; 62 63 private JMXConnector connector; 64 65 private MBeanServerConnection server; 66 67 private Logger logger = Logger.global; 68 69 private void setLogger(Logger pLogger) 70 { 71 if (pLogger == null) 72 { 73 throw new Error ("Logger may not be null"); 74 } 75 logger = pLogger; 76 } 77 78 84 public void handleNotification(Notification notification, Object handback) 85 { 86 logger.info("\nReceived sequoia notification: " + notification); 87 88 if (notification instanceof JMXConnectionNotification ) 89 { 90 JMXConnectionNotification connectionNotification = (JMXConnectionNotification ) notification; 91 logger.info("sequoia jmx connection id " 92 + connectionNotification.getConnectionId() + ", type=" 93 + notification.getType()); 94 } 95 96 if (JMXConnectionNotification.FAILED.equals(notification.getType()) 97 || JMXConnectionNotification.CLOSED.equals(notification.getType())) 98 { 99 try 100 { 101 logger.info("sequoia about to reconnect because of " 102 + notification.getType()); 103 reconnect(); 104 logger.info("sequoia jmx reconnect ok"); 105 } 106 catch (Exception e) 107 { 108 logger.log(Level.INFO, "jmx reconnect failed", e); 109 } 110 } 111 } 112 113 119 public void reconnect() throws Exception 120 { 121 long waitTime = 15000; 122 for (int retries = 0; retries < 10; retries++) 123 { 124 try 125 { 126 connect(); 127 logger.info("jmx sequoia reconnect successful"); 128 return; 129 } 130 catch (Exception e) 131 { 132 logger.log(Level.INFO, "jmx sequoia reconnect failed", e); 133 System.out.println("reconnect failed. (" + retries + ")"); 134 } 135 System.out.println("waiting for next try " + waitTime / 1000 + " sec "); 136 Thread.sleep(waitTime); 137 138 waitTime = waitTime * 2; 140 } 141 throw new Exception ( 142 "we stop trying to reconnect maximal number of attempts exceeded"); 143 } 144 145 150 public void connect() throws Exception 151 { 152 JMXServiceURL address = new JMXServiceURL ("rmi", host, 0, "/jndi/jrmp"); 153 154 Map environment = new HashMap (); 155 environment.put(Context.INITIAL_CONTEXT_FACTORY, 156 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 157 environment.put(Context.PROVIDER_URL, "rmi://" + host + ":" + port); 158 159 166 connector = JMXConnectorFactory.connect(address, environment); 167 168 logger.info("new jmx connection established to sequoia controller on port " 169 + port); 170 171 connector.addConnectionNotificationListener(this, null, null); 173 server = connector.getMBeanServerConnection(); 174 175 ObjectName db = JmxConstants.getVirtualDataBaseObjectName(virtualDB); 177 178 server.addNotificationListener(db, this, null, null); 181 logger.info("addNotificationListener for " + db.toString()); 182 } 183 184 191 public static void main(String [] args) throws Exception 192 { 193 SequoiaNotificationListener monitor = new SequoiaNotificationListener(); 194 monitor.connect(); 195 Thread.sleep(5 * 60000); 197 } 198 199 202 public String getAdminPassword() 203 { 204 return adminPassword; 205 } 206 207 210 public void setAdminPassword(String adminPassword) 211 { 212 this.adminPassword = adminPassword; 213 } 214 215 218 public String getAdminUserName() 219 { 220 return adminUserName; 221 } 222 223 226 public void setAdminUserName(String adminUserName) 227 { 228 this.adminUserName = adminUserName; 229 } 230 231 234 public String getHost() 235 { 236 return host; 237 } 238 239 242 public void setHost(String host) 243 { 244 this.host = host; 245 } 246 247 250 public String getPort() 251 { 252 return port; 253 } 254 255 258 public void setPort(String port) 259 { 260 this.port = port; 261 } 262 } 263 | Popular Tags |