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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.jmx;
20 import java.lang.management.ManagementFactory;
21 import java.util.Hashtable;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import javax.management.MBeanServer;
25 import javax.management.ObjectName;
29 * @author Ing. František Kučera (frantovo.cz)
31 public class ManagementUtils {
33 private static final Logger log = Logger.getLogger(ManagementUtils.class.getName());
34 public static final String DEFAULT_CONNECTION_JMX_NAME = "main";
37 * @see #registerMBean(java.lang.String, java.lang.String) with default JMX name
39 public static ConnectionManagement registerMBean(String dbName) {
40 return registerMBean(dbName, DEFAULT_CONNECTION_JMX_NAME);
45 * @param dbName database name
46 * @param jmxName name of JMX bean
47 * @return registered JMX bean | or null if registration fails (should not)
49 public static ConnectionManagement registerMBean(String dbName, String jmxName) {
51 ConnectionManagement mbean = new ConnectionManagement(dbName);
52 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
53 Hashtable<String, String> objectProperties = new Hashtable<>();
54 objectProperties.put("type", "Connection");
55 objectProperties.put("name", jmxName);
56 ObjectName objectName = new ObjectName("info.globalcode.sql.dk", objectProperties);
57 mbs.registerMBean(mbean, objectName);
58 log.log(Level.FINE, "JMX MBean was registered as: {0}", objectName);
60 } catch (Exception e) {
61 log.log(Level.WARNING, "Unable to register JMX MBean", e);
66 private ManagementUtils() {