java/sql-dk/src/main/java/info/globalcode/sql/dk/jmx/ConnectionManagement.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 Mar 2019 20:15:24 +0100
branchv_0
changeset 238 4a1864c3e867
parent 179 java/sql-dk/src/info/globalcode/sql/dk/jmx/ConnectionManagement.java@236332caeb29
child 250 aae5009bd0af
permissions -rw-r--r--
mavenized: sql-dk
     1 /**
     2  * SQL-DK
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package info.globalcode.sql.dk.jmx;
    19 
    20 import java.util.EnumMap;
    21 import java.util.Map;
    22 
    23 /**
    24  * JMX management bean for progress reporting.
    25  *
    26  * @author Ing. František Kučera (frantovo.cz)
    27  */
    28 public class ConnectionManagement implements ConnectionManagementMBean {
    29 
    30 	private final String databaseName;
    31 	private final Map<COUNTER, Integer> counters = new EnumMap(COUNTER.class);
    32 
    33 	public ConnectionManagement(String databaseName) {
    34 		this.databaseName = databaseName;
    35 		for (COUNTER c : COUNTER.values()) {
    36 			counters.put(c, 0);
    37 		}
    38 	}
    39 
    40 	public enum COUNTER {
    41 
    42 		COMMAND,
    43 		RECORD_CURRENT,
    44 		RECORD_TOTAL
    45 	};
    46 
    47 	public void incrementCounter(COUNTER counter) {
    48 		synchronized (counters) {
    49 			int old = counters.get(counter);
    50 			counters.put(counter, old + 1);
    51 		}
    52 	}
    53 
    54 	public void resetCounter(COUNTER counter) {
    55 		synchronized (counters) {
    56 			counters.put(counter, 0);
    57 		}
    58 	}
    59 
    60 	public static void incrementCounter(ConnectionManagement mbean, COUNTER counter) {
    61 		if (mbean != null) {
    62 			mbean.incrementCounter(counter);
    63 		}
    64 	}
    65 
    66 	public static void resetCounter(ConnectionManagement mbean, COUNTER counter) {
    67 		if (mbean != null) {
    68 			mbean.resetCounter(counter);
    69 		}
    70 	}
    71 
    72 	@Override
    73 	public String getDatabaseName() {
    74 		return databaseName;
    75 	}
    76 
    77 	@Override
    78 	public int getCommandCount() {
    79 		synchronized (counters) {
    80 			return counters.get(COUNTER.COMMAND);
    81 		}
    82 	}
    83 
    84 	@Override
    85 	public int getCurrentRecordCount() {
    86 		synchronized (counters) {
    87 			return counters.get(COUNTER.RECORD_CURRENT);
    88 		}
    89 	}
    90 	
    91 	@Override
    92 	public int getTotalRecordCount() {
    93 		synchronized (counters) {
    94 			return counters.get(COUNTER.RECORD_TOTAL);
    95 		}
    96 	}
    97 }