KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > jmx > SequoiaNotificationListener


1
2 package examples.jmx;
3
4 /**
5  * Sequoia: Database clustering technology.
6  * Copyright (C) 2002-2004 French National Institute For Research In Computer
7  * Science And Control (INRIA).
8  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
9  * Contact: sequoia@continuent.org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Initial developer(s): Marc Wick.
24  * Contributor(s):
25  */

26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.management.MBeanServerConnection JavaDoc;
32 import javax.management.Notification JavaDoc;
33 import javax.management.NotificationListener JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35 import javax.management.remote.JMXConnectionNotification JavaDoc;
36 import javax.management.remote.JMXConnector JavaDoc;
37 import javax.management.remote.JMXConnectorFactory JavaDoc;
38 import javax.management.remote.JMXServiceURL JavaDoc;
39 import javax.naming.Context JavaDoc;
40
41 import org.continuent.sequoia.common.jmx.JmxConstants;
42
43 /**
44  * This class defines a jmx notification listener. It will register with the
45  * sequoia controller and recieve jmx notifications about all sequioa events.
46  *
47  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
48  * @version 1.0
49  */

50 public class SequoiaNotificationListener implements NotificationListener JavaDoc
51 {
52
53   private String JavaDoc port = "8091";
54
55   private String JavaDoc host = "localhost";
56
57   private String JavaDoc virtualDB = "rubis";
58
59   private String JavaDoc adminUserName = "admin";
60
61   private String JavaDoc adminPassword = "sequioa";
62
63   private JMXConnector JavaDoc connector;
64
65   private MBeanServerConnection JavaDoc server;
66
67   private Logger JavaDoc logger = Logger.global;
68
69   private void setLogger(Logger JavaDoc pLogger)
70   {
71     if (pLogger == null)
72     {
73       throw new Error JavaDoc("Logger may not be null");
74     }
75     logger = pLogger;
76   }
77
78   /**
79    * {@inheritDoc}
80    *
81    * @see javax.management.NotificationListener#handleNotification(javax.management.Notification,
82    * java.lang.Object)
83    */

84   public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
85   {
86     logger.info("\nReceived sequoia notification: " + notification);
87
88     if (notification instanceof JMXConnectionNotification JavaDoc)
89     {
90       JMXConnectionNotification JavaDoc connectionNotification = (JMXConnectionNotification JavaDoc) 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 JavaDoc e)
107       {
108         logger.log(Level.INFO, "jmx reconnect failed", e);
109       }
110     }
111   }
112
113   /**
114    * reconnect to the controller in case the controller was shut down. We try to
115    * reconnect for some time.
116    *
117    * @throws Exception if an error occurs
118    */

119   public void reconnect() throws Exception JavaDoc
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 JavaDoc 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       // duplicate wait time with every iteration
139
waitTime = waitTime * 2;
140     }
141     throw new Exception JavaDoc(
142         "we stop trying to reconnect maximal number of attempts exceeded");
143   }
144
145   /**
146    * connect to the sequoia controller and register this class as listener
147    *
148    * @throws Exception if an error occurs
149    */

150   public void connect() throws Exception JavaDoc
151   {
152     JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc("rmi", host, 0, "/jndi/jrmp");
153
154     Map JavaDoc environment = new HashMap JavaDoc();
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     // use username and password for authentication of connections
160
// with the controller, the values are compared to the ones
161
// specified in the controller.xml config file.
162
// this line is not required if no username/password has been configered
163
// environment.put(JMXConnector.CREDENTIALS, PasswordAuthenticator
164
// .createCredentials("jmxuser", "jmxpassword"));
165

166     connector = JMXConnectorFactory.connect(address, environment);
167
168     logger.info("new jmx connection established to sequoia controller on port "
169         + port);
170
171     // we want to be informed about connection problems
172
connector.addConnectionNotificationListener(this, null, null);
173     server = connector.getMBeanServerConnection();
174
175     // ObjectName db = JmxConstants.getVirtualDbObjectName(virtualDB);
176
ObjectName JavaDoc db = JmxConstants.getVirtualDataBaseObjectName(virtualDB);
177
178     // we are registering for notifications emitted by the controller
179
// for example if a backend is disabled
180
server.addNotificationListener(db, this, null, null);
181     logger.info("addNotificationListener for " + db.toString());
182   }
183
184   /**
185    * main class as example. In a real application this part will probably be
186    * included in the application itself or in some other monitoring program.
187    *
188    * @param args command line arguments (ignored)
189    * @throws Exception if an error occurs
190    */

191   public static void main(String JavaDoc[] args) throws Exception JavaDoc
192   {
193     SequoiaNotificationListener monitor = new SequoiaNotificationListener();
194     monitor.connect();
195     // monitor for five minutes as a test.
196
Thread.sleep(5 * 60000);
197   }
198
199   /**
200    * @return Returns the adminPassword.
201    */

202   public String JavaDoc getAdminPassword()
203   {
204     return adminPassword;
205   }
206
207   /**
208    * @param adminPassword The adminPassword to set.
209    */

210   public void setAdminPassword(String JavaDoc adminPassword)
211   {
212     this.adminPassword = adminPassword;
213   }
214
215   /**
216    * @return Returns the adminUserName.
217    */

218   public String JavaDoc getAdminUserName()
219   {
220     return adminUserName;
221   }
222
223   /**
224    * @param adminUserName The adminUserName to set.
225    */

226   public void setAdminUserName(String JavaDoc adminUserName)
227   {
228     this.adminUserName = adminUserName;
229   }
230
231   /**
232    * @return Returns the host.
233    */

234   public String JavaDoc getHost()
235   {
236     return host;
237   }
238
239   /**
240    * @param host The host to set.
241    */

242   public void setHost(String JavaDoc host)
243   {
244     this.host = host;
245   }
246
247   /**
248    * @return Returns the port.
249    */

250   public String JavaDoc getPort()
251   {
252     return port;
253   }
254
255   /**
256    * @param port The port to set.
257    */

258   public void setPort(String JavaDoc port)
259   {
260     this.port = port;
261   }
262 }
263
Popular Tags