3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.jmx;
19 import java.lang.management.ManagementFactory;
20 import java.util.Hashtable;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import javax.management.MBeanServer;
24 import javax.management.ObjectName;
28 * @author Ing. František Kučera (frantovo.cz)
30 public class ManagementUtils {
32 private static final Logger log = Logger.getLogger(ManagementUtils.class.getName());
33 public static final String DEFAULT_CONNECTION_JMX_NAME = "main";
36 * @see #registerMBean(java.lang.String, java.lang.String) with default JMX name
38 public static ConnectionManagement registerMBean(String dbName) {
39 return registerMBean(dbName, DEFAULT_CONNECTION_JMX_NAME);
44 * @param dbName database name
45 * @param jmxName name of JMX bean
46 * @return registered JMX bean | or null if registration fails (should not)
48 public static ConnectionManagement registerMBean(String dbName, String jmxName) {
50 ConnectionManagement mbean = new ConnectionManagement(dbName);
51 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
52 Hashtable<String, String> objectProperties = new Hashtable<>();
53 objectProperties.put("type", "Connection");
54 objectProperties.put("name", jmxName);
55 ObjectName objectName = new ObjectName("info.globalcode.sql.dk", objectProperties);
56 mbs.registerMBean(mbean, objectName);
57 log.log(Level.FINE, "JMX MBean was registered as: {0}", objectName);
59 } catch (Exception e) {
60 log.log(Level.WARNING, "Unable to register JMX MBean", e);
65 private ManagementUtils() {