java/sql-dk/src/main/java/info/globalcode/sql/dk/jmx/ManagementUtils.java
branchv_0
changeset 238 4a1864c3e867
parent 179 236332caeb29
child 250 aae5009bd0af
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/jmx/ManagementUtils.java	Mon Mar 04 20:15:24 2019 +0100
@@ -0,0 +1,68 @@
+/**
+ * SQL-DK
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package info.globalcode.sql.dk.jmx;
+
+import java.lang.management.ManagementFactory;
+import java.util.Hashtable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class ManagementUtils {
+
+	private static final Logger log = Logger.getLogger(ManagementUtils.class.getName());
+	public static final String DEFAULT_CONNECTION_JMX_NAME = "main";
+
+	/**
+	 * @see #registerMBean(java.lang.String, java.lang.String) with default JMX name
+	 */
+	public static ConnectionManagement registerMBean(String dbName) {
+		return registerMBean(dbName, DEFAULT_CONNECTION_JMX_NAME);
+	}
+
+	/**
+	 *
+	 * @param dbName database name
+	 * @param jmxName name of JMX bean
+	 * @return registered JMX bean | or null if registration fails (should not)
+	 */
+	public static ConnectionManagement registerMBean(String dbName, String jmxName) {
+		try {
+			ConnectionManagement mbean = new ConnectionManagement(dbName);
+			MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+			Hashtable<String, String> objectProperties = new Hashtable<>();
+			objectProperties.put("type", "Connection");
+			objectProperties.put("name", jmxName);
+			ObjectName objectName = new ObjectName("info.globalcode.sql.dk", objectProperties);
+			mbs.registerMBean(mbean, objectName);
+			log.log(Level.FINE, "JMX MBean was registered as: {0}", objectName);
+			return mbean;
+		} catch (Exception e) {
+			log.log(Level.WARNING, "Unable to register JMX MBean", e);
+			return null;
+		}
+	}
+
+	private ManagementUtils() {
+	}
+}