225 lines
9.0 KiB
HTML
225 lines
9.0 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
|
||
|
|
||
|
<title>Example: Test a connection using testConnection(ConfigID)</title>
|
||
|
</head>
|
||
|
|
||
|
<BODY>
|
||
|
<!-- Java sync-link -->
|
||
|
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
|
||
|
|
||
|
<h6><a name="datatestexconfig"></a>Example: Test a connection using testConnection(ConfigID)</h6>
|
||
|
|
||
|
<p>You can pass the configuration ID of a configured data source, rather
|
||
|
than the properties of the data source. This example program uses Java Management Extensions (JMX) to connect
|
||
|
to a running server and invoke the testConnection method on the DataSourceCfgHelper MBean.</p>
|
||
|
|
||
|
<p>See the <a href="codex.htm">Code example disclaimer</a> for legal information about these code examples.</p>
|
||
|
|
||
|
<pre>
|
||
|
/**
|
||
|
* Description
|
||
|
* Resource adapter test program to make sure that the MBean interfaces work.
|
||
|
* Following interfaces are tested
|
||
|
*
|
||
|
* --- testConnection()
|
||
|
*
|
||
|
*
|
||
|
* We need following to run
|
||
|
*
|
||
|
* From an iSeries command line:
|
||
|
*
|
||
|
* -> QSHELL (Start the QSHELL environment)
|
||
|
*
|
||
|
* -> cd (yourDirectory)
|
||
|
* (change current directory to the directory that holds the java source)
|
||
|
*
|
||
|
* -> javac -extdirs /QIBM/ProdData/WebASE51/ASE/lib -d . testDSGUI.java
|
||
|
* (compile the testDSGUI class)
|
||
|
*
|
||
|
* -> . /QIBM/ProdData/WebASE51/ASE/bin/setupClient
|
||
|
* (produce the $JAVA_FLAGS_EXT environment variable)
|
||
|
*
|
||
|
* -> java $JAVA_FLAGS_EXT testDSGUI
|
||
|
* (call the program)
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Locale;
|
||
|
import java.util.Properties;
|
||
|
import java.util.Set;
|
||
|
|
||
|
import javax.management.InstanceNotFoundException;
|
||
|
import javax.management.MBeanException;
|
||
|
import javax.management.MalformedObjectNameException;
|
||
|
import javax.management.ObjectName;
|
||
|
import javax.management.RuntimeMBeanException;
|
||
|
import javax.management.RuntimeOperationsException;
|
||
|
|
||
|
import com.ibm.websphere.management.AdminClient;
|
||
|
import com.ibm.websphere.management.AdminClientFactory;
|
||
|
import com.ibm.ws.rsadapter.exceptions.DataStoreAdapterException;
|
||
|
|
||
|
public class testDSGUI {
|
||
|
|
||
|
//Use port 8880 for base installation or port 8879 for ND installation
|
||
|
String port = "8880";
|
||
|
// String port = "8879";
|
||
|
String host = "localhost";
|
||
|
final static boolean verbose = true;
|
||
|
|
||
|
// eg a configuration ID for 5.0 DataSource declared at the node level for base
|
||
|
private static final String resURI = "cells/cat/nodes/cat:resources.xml#DataSource_1";
|
||
|
|
||
|
// eg a 4.0 DataSource declared at the node level for base
|
||
|
// private static final String resURI = "cells/cat/nodes/cat:resources.xml#WAS40DataSource_1";
|
||
|
|
||
|
// eg Cloudscape DataSource declared at the server level for base
|
||
|
//private static final String resURI = "cells/cat/nodes/cat/servers/server1/resources.xml#DataSource_6";
|
||
|
|
||
|
// eg node level DataSource for ND
|
||
|
//private static final String resURI = "cells/catNetwork/nodes/cat:resources.xml#DataSource_1";
|
||
|
|
||
|
// eg server level DataSource for ND
|
||
|
//private static final String resURI =
|
||
|
// "cells/catNetwork/nodes/cat/servers/server1:resources.xml#DataSource_4";
|
||
|
|
||
|
// eg cell level DataSource for ND
|
||
|
//private static final String resURI = "cells/catNetwork:resources.xml#DataSource_1";
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
testDSGUI cds = new testDSGUI();
|
||
|
cds.run(args);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This method tests the ResourceMbean.
|
||
|
*
|
||
|
* @param args
|
||
|
* @exception Exception
|
||
|
*/
|
||
|
public void run(String[] args) {
|
||
|
|
||
|
try {
|
||
|
|
||
|
System.out.println("Connecting to the application server.......");
|
||
|
|
||
|
/*************************************************************************/
|
||
|
/** Initialize the AdminClient */
|
||
|
/*************************************************************************/
|
||
|
Properties adminProps = new Properties();
|
||
|
adminProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
|
||
|
adminProps.setProperty(AdminClient.CONNECTOR_HOST, host);
|
||
|
adminProps.setProperty(AdminClient.CONNECTOR_PORT, port);
|
||
|
AdminClient adminClient = null;
|
||
|
try {
|
||
|
/* Port 8880 must be listening when this program is called or the SOAP connection will fail */
|
||
|
adminClient = AdminClientFactory.createAdminClient(adminProps);
|
||
|
} catch (com.ibm.websphere.management.exception.ConnectorException ce) {
|
||
|
System.out.println("NLS: Cannot make a connection to the application server\n");
|
||
|
ce.printStackTrace();
|
||
|
System.exit(1);
|
||
|
}
|
||
|
|
||
|
/*************************************************************************/
|
||
|
/** Locate the Mbean */
|
||
|
/*************************************************************************/
|
||
|
ObjectName handle = null;
|
||
|
try {
|
||
|
// Send in a locator string
|
||
|
// eg for a Base installation this is enough
|
||
|
ObjectName queryName = new ObjectName("WebSphere:type=DataSourceCfgHelper,*");
|
||
|
|
||
|
// for ND you need to specify which node/process you would like to test from
|
||
|
// eg run in the server
|
||
|
//ObjectName queryName = new ObjectName("WebSphere:cell=catNetwork,node=cat,
|
||
|
process=server1,type=DataSourceCfgHelper,*");
|
||
|
// eg run in the node agent
|
||
|
//ObjectName queryName = new ObjectName("WebSphere:cell=catNetwork,node=cat,
|
||
|
process=nodeagent,type=DataSourceCfgHelper,*");
|
||
|
// eg run in the Deployment Manager
|
||
|
//ObjectName queryName = new ObjectName("WebSphere:cell=catNetwork,node=catManager,
|
||
|
process=dmgr,type=DataSourceCfgHelper,*");
|
||
|
Set s = adminClient.queryNames(queryName, null);
|
||
|
Iterator iter = s.iterator();
|
||
|
while (iter.hasNext()) {
|
||
|
// use the first MBean that is found
|
||
|
handle = (ObjectName) iter.next();
|
||
|
System.out.println("Found this ->" + handle);
|
||
|
}
|
||
|
if (handle == null) {
|
||
|
System.out.println("NLS: Did not find this MBean>>" + queryName);
|
||
|
System.exit(1);
|
||
|
}
|
||
|
} catch (MalformedObjectNameException mone) {
|
||
|
System.out.println("Check the program variable queryName" + mone);
|
||
|
} catch (com.ibm.websphere.management.exception.ConnectorException ce) {
|
||
|
System.out.println("Cannot connect to the application server" + ce);
|
||
|
}
|
||
|
|
||
|
/*************************************************************************/
|
||
|
/** Build parameters to pass to Mbean */
|
||
|
/*************************************************************************/
|
||
|
String[] signature = { "java.lang.String" };
|
||
|
Object[] params = { resURI };
|
||
|
Object result = null;
|
||
|
|
||
|
if (verbose) {
|
||
|
System.out.println("\nTesting connection to the database using " + handle);
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
/*************************************************************************/
|
||
|
/** Start to test the connection to the database */
|
||
|
/** */
|
||
|
/** Note that the datasource must exist prior to calling this program */
|
||
|
/*************************************************************************/
|
||
|
result = adminClient.invoke(handle, "testConnection", params, signature);
|
||
|
} catch (MBeanException mbe) {
|
||
|
// ****** all user exceptions come in here
|
||
|
if (verbose) {
|
||
|
Exception ex = mbe.getTargetException(); // this is the real exception from the Mbean
|
||
|
System.out.println("\nNLS:Mbean Exception was received contains " + ex);
|
||
|
ex.printStackTrace();
|
||
|
System.exit(1);
|
||
|
}
|
||
|
} catch (InstanceNotFoundException infe) {
|
||
|
System.out.println("Cannot find " + infe);
|
||
|
} catch (RuntimeMBeanException rme) {
|
||
|
Exception ex = rme.getTargetException();
|
||
|
ex.printStackTrace(System.out);
|
||
|
throw ex;
|
||
|
} catch (Exception ex) {
|
||
|
System.out.println("\nUnexpected Exception occurred: " + ex);
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
|
||
|
/*************************************************************************/
|
||
|
/** Process the result. The result will be the number of warnings */
|
||
|
/** issued. A result of 0 indicates a successful connection with */
|
||
|
/** no warnings. */
|
||
|
/*************************************************************************/
|
||
|
|
||
|
//A result of 0 indicates a successful connection with no warnings.
|
||
|
System.out.println("Result= " + result);
|
||
|
|
||
|
} catch (RuntimeOperationsException roe) {
|
||
|
Exception ex = roe.getTargetException();
|
||
|
ex.printStackTrace(System.out);
|
||
|
} catch (Exception ex) {
|
||
|
System.out.println("General exception occurred");
|
||
|
ex.printStackTrace(System.out);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
</pre>
|
||
|
<p><strong>Note: </strong>Example may be wrapped for display purposes.</p>
|
||
|
|
||
|
</body>
|
||
|
</html>
|