/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.cultuzz.java.utils.jdbc; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.management.ObjectName; import javax.management.*; import com.sun.appserv.management.client.AppserverConnectionSource; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import java.sql.*; import javax.sql.*; import javax.naming.*; /** * * @author sanjeev * Cultuzz India Pvt. Ltd. */ public class ConnectionsTest extends HttpServlet { public static final long serialVersionUID = -200806031139L; public static final HashMap properties = new HashMap(); public static final String USER_PASSWORD = "adminadmin"; public static final int JMX_PORT = 8686; // 8754; public static final String DOTTED_NAME_GET = "dottedNameGet"; public static final String USER = "admin"; public static final String CONFIG = "server-config"; public static final String HOST_NAME = "localhost"; public static final String connectionPoolName = "csp-pool"; protected void updateProperties() { properties.put("Number of Current Connections Used", "numconnused-current"); properties.put("Maximum Number of Connections Used", "numconnused-highwatermark"); properties.put("Number of Connections Destroyed by pool since last sampling", "numconndestroyed-count"); properties.put("Number of Connections Created by pool since last sampling", "numconncreated-count"); properties.put("Number of Connections Acquired from the pool", "numconnacquired-count"); properties.put("Number of Connections Released to the pool", "numconnreleased-count"); properties.put("Average Connection wait time", "averageconnwaittime-count"); properties.put("Wait time for last connection request serviced by pool", "connrequestwaittime-current"); properties.put("Maximum wait time for request serviced by pool", "connrequestwaittime-highwatermark"); properties.put("Number of Connections in pool, failed validation", "numconnfailedvalidation-count"); properties.put("Number of Connections didn't match", "numconnnotsuccessfullymatched-count"); properties.put("Number of Connections matched successfully", "numconnsuccessfullymatched-count"); properties.put("Number of Connections timed out", "numconntimedout-count"); properties.put("Length of wait queue", "waitqueuelength-count"); properties.put("Number of potential connection leaks", "numpotentialconnleak-count"); properties.put("Number of free connections", "numconnfree-current"); properties.put("Maximum Number of free connections", "numconnfree-highwatermark"); } /** * Processes requests for both HTTP GET and POST methods. * @param request servlet request * @param response servlet responseprintln * @throws ServletException * @throws IOException */ private int testcase = 1; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); updateProperties(); if(request.getParameter("count") != null) { NUM_CONN = Integer.parseInt(request.getParameter("count")); } if(request.getParameter("testcase") != null) { testcase = Integer.parseInt(request.getParameter("testcase")); } try { out.println(""); out.println(""); out.println("Servlet PoolMonitor"); out.println(""); out.println(""); try { runTest(out); } catch (Exception ex) { ex.printStackTrace(); } out.println(""); out.println(""); } finally { out.close(); } } // /** * Handles the HTTP GET method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; }// private int NUM_CONN = 100; public void runTest(final PrintWriter out) throws Exception { class Exec extends Thread { private int i; public Exec(int i) { this.i = i; } public void run() { makeConnection(i, out); } } for(int i=0; i"); out.write(""); String x = "--------------------------------\r\n"; while(valuesit.hasNext()) { String property = (String) valuesit.next(); try { int result = getMonitorablePropertyOfConnectionPool(connectionPoolName, property, HOST_NAME, JMX_PORT, USER, USER_PASSWORD, CONFIG); out.write(""); // out.write(""); out.write(""); x += property + ":" + result + "\r\n"; } catch (Exception e) { e.printStackTrace(); } } out.write("
" + keyit.next() + out.write("" + property + " : " + result + "
"); x += "--------------------------------\r\n"; System.out.println(x); } } /** * * @param poolName * @param property * @param hostName * @param JMX_PORT * @param user * @param password * @param configName * @return * @throws java.lang.Exception */ public int getMonitorablePropertyOfConnectionPool(String poolName, String property, String hostName, int JMX_PORT, String user, String password, String configName) throws Exception { AppserverConnectionSource appserver = new AppserverConnectionSource(AppserverConnectionSource.PROTOCOL_RMI, hostName, JMX_PORT, user, password, null); MBeanServerConnection connection = appserver.getJMXConnector(false).getMBeanServerConnection(); ObjectName objectName = new ObjectName("amx:j2eeType=X-MonitoringDottedNames,name=na"); String params[] = new String[]{String.class.getName()}; Object values[] = new Object[]{"server.resources." + poolName + "." + property}; javax.management.Attribute returnValue = null; try { returnValue = (javax.management.Attribute) connection.invoke(objectName, DOTTED_NAME_GET, values, params); return Integer.parseInt(returnValue.getValue().toString()); } catch (Exception ex) { System.out.println("Property not found: " + property); return -1; } } }