java/sql-dk/src/main/java/info/globalcode/sql/dk/jmx/ConnectionManagement.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/ConnectionManagement.java	Mon Mar 04 20:15:24 2019 +0100
@@ -0,0 +1,97 @@
+/**
+ * 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.util.EnumMap;
+import java.util.Map;
+
+/**
+ * JMX management bean for progress reporting.
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class ConnectionManagement implements ConnectionManagementMBean {
+
+	private final String databaseName;
+	private final Map<COUNTER, Integer> counters = new EnumMap(COUNTER.class);
+
+	public ConnectionManagement(String databaseName) {
+		this.databaseName = databaseName;
+		for (COUNTER c : COUNTER.values()) {
+			counters.put(c, 0);
+		}
+	}
+
+	public enum COUNTER {
+
+		COMMAND,
+		RECORD_CURRENT,
+		RECORD_TOTAL
+	};
+
+	public void incrementCounter(COUNTER counter) {
+		synchronized (counters) {
+			int old = counters.get(counter);
+			counters.put(counter, old + 1);
+		}
+	}
+
+	public void resetCounter(COUNTER counter) {
+		synchronized (counters) {
+			counters.put(counter, 0);
+		}
+	}
+
+	public static void incrementCounter(ConnectionManagement mbean, COUNTER counter) {
+		if (mbean != null) {
+			mbean.incrementCounter(counter);
+		}
+	}
+
+	public static void resetCounter(ConnectionManagement mbean, COUNTER counter) {
+		if (mbean != null) {
+			mbean.resetCounter(counter);
+		}
+	}
+
+	@Override
+	public String getDatabaseName() {
+		return databaseName;
+	}
+
+	@Override
+	public int getCommandCount() {
+		synchronized (counters) {
+			return counters.get(COUNTER.COMMAND);
+		}
+	}
+
+	@Override
+	public int getCurrentRecordCount() {
+		synchronized (counters) {
+			return counters.get(COUNTER.RECORD_CURRENT);
+		}
+	}
+	
+	@Override
+	public int getTotalRecordCount() {
+		synchronized (counters) {
+			return counters.get(COUNTER.RECORD_TOTAL);
+		}
+	}
+}