Example: Invoking showPoolContents using the Java Management Extensions API

This example Java program uses the Java Management Extensions (JMX) application programming interface (API) to invoke showPoolContents.

/**
 * Sample program
 * This sample program is provided AS IS and may be used, executed, copied and 
 * modified without royalty payment by customer (a) for its own instruction and 
 * study, (b) in order to develop applications designed to run with an IBM 
 * WebSphere product, either for customer's own internal use 

 * COPYRIGHT International Business Machines Corp. 2010
 * All Rights Reserved * Licensed Materials - Property of IBM
 * 
 * This program will display the connection pool contents of a datasource using
 * the JMX API.
 * 
 * To run this program
 * 
 *  1. call "%WAS_HOME%/bin/setupCmdLine.bat"
 * 
 *  2. "%JAVA_HOME%\bin\java" "%CLIENTSAS%" "-Dwas.install.root=%WAS_HOME%" "
 *     -Dwas.repository.root=%CONFIG_ROOT%" 
 *     -Dcom.ibm.CORBA.BootstrapHost=%COMPUTERNAME% 
 *     -classpath "%WAS_CLASSPATH%;%WAS_HOME%\runtimes
\com.ibm.ws.admin.client_6.1.0.jar;%WAS_HOME%\plugins
\com.ibm.ws.security.crypto_6.1.0.jar;."
 *     ShowPoolContents DataSource_mbean_name SOAP_port_number
 */

import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.management.ObjectName;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;

public class ShowPoolContents {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
	if (args.length != 2) {
            System.out.println("Must specify DataSource name and server SOAP port number");
            System.exit(-1);
	}
	try {
	    // create an ObjectName pattern for the specified datasource
	    ObjectName on = new ObjectName(String.format("*:type=DataSource,name=%s,*", args[0])); 
            // Set up a Properties object for the JMX connector attributes
            Properties connectProps = new Properties();
            connectProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
            connectProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
            connectProps.setProperty(AdminClient.CONNECTOR_PORT, args[1]);
	    // Create an admin client connection
            AdminClient adminClient = AdminClientFactory.createAdminClient(connectProps);
	    // find the specified MBean
	    Set dataSourceMBeans = adminClient.queryNames(on, null);
	    if (dataSourceMBeans.isEmpty()) {
		System.out.println("Error: No DataSource MBean with name "+ args[0]);
	    }
	    else {
		// iterate through the Set
		Iterator iter = dataSourceMBeans.iterator();
		while (iter.hasNext()) {
                    // invoke the showPoolContents operation on the MBean, it takes no parameters
                    Object poolContents = adminClient.invoke((ObjectName)iter.next(),
                    	"showPoolContents", null, null);
                    System.out.println("Pool contents:" + poolContents);
		}		
	    }	    
	}
	catch (Exception ex) {
	    System.out.println("Exception: " + ex.toString());
	    ex.printStackTrace(System.out);
	}
    }
}



Related tasks
Changing connection pool settings with the wsadmin tool
Related reference
Example: Accessing MBean connection factory and data sources using wsadmin
Reference topic Reference topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Aug 31, 2013 2:56:59 AM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=pix&product=was-nd-dist&topic=rdat_adexjava
File name: rdat_adexjava.html