ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/datatestexconfig.htm

225 lines
9.0 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!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 = &quot;8880&quot;;
// String port = &quot;8879&quot;;
String host = &quot;localhost&quot;;
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 = &quot;cells/cat/nodes/cat:resources.xml#DataSource_1&quot;;
// eg a 4.0 DataSource declared at the node level for base
// private static final String resURI = &quot;cells/cat/nodes/cat:resources.xml#WAS40DataSource_1&quot;;
// eg Cloudscape DataSource declared at the server level for base
//private static final String resURI = &quot;cells/cat/nodes/cat/servers/server1/resources.xml#DataSource_6&quot;;
// eg node level DataSource for ND
//private static final String resURI = &quot;cells/catNetwork/nodes/cat:resources.xml#DataSource_1&quot;;
// eg server level DataSource for ND
//private static final String resURI =
// &quot;cells/catNetwork/nodes/cat/servers/server1:resources.xml#DataSource_4&quot;;
// eg cell level DataSource for ND
//private static final String resURI = &quot;cells/catNetwork:resources.xml#DataSource_1&quot;;
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(&quot;Connecting to the application server.......&quot;);
/*************************************************************************/
/** 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(&quot;NLS: Cannot make a connection to the application server\n&quot;);
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(&quot;WebSphere:type=DataSourceCfgHelper,*&quot;);
// for ND you need to specify which node/process you would like to test from
// eg run in the server
//ObjectName queryName = new ObjectName(&quot;WebSphere:cell=catNetwork,node=cat,
process=server1,type=DataSourceCfgHelper,*&quot;);
// eg run in the node agent
//ObjectName queryName = new ObjectName(&quot;WebSphere:cell=catNetwork,node=cat,
process=nodeagent,type=DataSourceCfgHelper,*&quot;);
// eg run in the Deployment Manager
//ObjectName queryName = new ObjectName(&quot;WebSphere:cell=catNetwork,node=catManager,
process=dmgr,type=DataSourceCfgHelper,*&quot;);
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(&quot;Found this -&gt;&quot; + handle);
}
if (handle == null) {
System.out.println(&quot;NLS: Did not find this MBean&gt;&gt;&quot; + queryName);
System.exit(1);
}
} catch (MalformedObjectNameException mone) {
System.out.println(&quot;Check the program variable queryName&quot; + mone);
} catch (com.ibm.websphere.management.exception.ConnectorException ce) {
System.out.println(&quot;Cannot connect to the application server&quot; + ce);
}
/*************************************************************************/
/** Build parameters to pass to Mbean */
/*************************************************************************/
String[] signature = { &quot;java.lang.String&quot; };
Object[] params = { resURI };
Object result = null;
if (verbose) {
System.out.println(&quot;\nTesting connection to the database using &quot; + 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, &quot;testConnection&quot;, 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(&quot;\nNLS:Mbean Exception was received contains &quot; + ex);
ex.printStackTrace();
System.exit(1);
}
} catch (InstanceNotFoundException infe) {
System.out.println(&quot;Cannot find &quot; + infe);
} catch (RuntimeMBeanException rme) {
Exception ex = rme.getTargetException();
ex.printStackTrace(System.out);
throw ex;
} catch (Exception ex) {
System.out.println(&quot;\nUnexpected Exception occurred: &quot; + 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(&quot;Result= &quot; + result);
} catch (RuntimeOperationsException roe) {
Exception ex = roe.getTargetException();
ex.printStackTrace(System.out);
} catch (Exception ex) {
System.out.println(&quot;General exception occurred&quot;);
ex.printStackTrace(System.out);
}
}
}
</pre>
<p><strong>Note: </strong>Example may be wrapped for display purposes.</p>
</body>
</html>