How to List & Test DataSources using Native Management API based java code in EAP6

Solution Unverified - Updated -

Environment

  • JBoss Enterprise Application Platform (EAP)
    • 6.x

Issue

  • Is there a way in JBoss EAP6 to use Java based code so we can List all the available XA and Non_XA ?.

  • Also is it possible to test the datasources as well using java code, which we can do using the following kind of CLI command:

/subsystem=datasources/data-source=MyDS:test-connection-in-pool

/subsystem=datasources/xa-data-source=YourDS:test-connection-in-pool

Resolution

  • A simple ModelControllerClient based java code which is capable of doing the same. See the Attached Code "TestDataSource_Detached.zip"

  • The attached code basically uses "ModelControllerClient" APIs and the "ModelNode" in order to fetch this data from JBoss.

Example Snippet

      public static void testNonXADataSource(String dataSourceName) throws Exception
       {
                 ModelNode op = new ModelNode(); 
                 op.get("operation").set("test-connection-in-pool"); 
                 ModelNode address = op.get("address"); 
                 address.add("subsystem", "datasources"); 
                 address.add("data-source", dataSourceName); 
                 op.get("operations").set(true);

                 ModelNode returnVal = client.execute(op); 
                 String result=returnVal.get("result").toString();
                 String path="/subsystem=datasources/data-source="+dataSourceName;
                 if(result.equals("[true]")) 
                    {
                       System.out.println("\n\tNon XA DataSource \""+dataSourceName+"\" is RUNNING Successfully.");
                    }
                 else if(result.equals("[false]")) 
                    {
                      System.out.println("\n\tNon XA DataSource \""+dataSourceName+"\" is test FAILED");
                    }
             else if(result.equals("undefined")) 
                {
                   System.out.println("\n\tDataSource \""+dataSourceName+"\" test FAILED");
                }
         }


      public static void testXADataSource(String xaDataSourceName) throws Exception 
       {
                 ModelNode op = new ModelNode(); 
                 op.get("operation").set("test-connection-in-pool"); 
                 ModelNode address = op.get("address"); 
                 address.add("subsystem", "datasources"); 
                 address.add("xa-data-source", xaDataSourceName); 
                 op.get("operations").set(true);

                 ModelNode returnVal = client.execute(op); 
                 String result=returnVal.get("result").toString();
                 String path="/subsystem=datasources/xa-data-source="+xaDataSourceName;
                 if(result.equals("[true]")) 
                    {
                       System.out.println("\n\tXA DataSource \""+xaDataSourceName+"\" is RUNNING Successfully.");
                    }
                 else if(result.equals("[false]")) 
                    {
                      System.out.println("\n\tXA DataSource \""+xaDataSourceName+"\" is test FAILED");
                    }
                 else if(result.equals("undefined")) 
                    {
                   System.out.println("\n\tXA DataSource \""+xaDataSourceName+"\" test FAILED");
                    }        
         }


      public static List<ModelNode> listNonXADataSource() throws Exception 
       {
                 ModelNode op = new ModelNode(); 
                 op.get("operation").set("read-children-names"); 
                 op.get("child-type").set("data-source");
                 ModelNode address = op.get("address"); 
                 address.add("subsystem", "datasources"); 
                 op.get("operations").set(true);

                 ModelNode returnVal = client.execute(op); 
                 String listOfDataSources=returnVal.get("result").toString();
                 return returnVal.get("result").asList();
         }

      public static List<ModelNode> listXADataSource() throws Exception 
       {
                 ModelNode op = new ModelNode(); 
                 op.get("operation").set("read-children-names"); 
                 op.get("child-type").set("xa-data-source");
                 ModelNode address = op.get("address"); 
                 address.add("subsystem", "datasources"); 
                 op.get("operations").set(true);

                 ModelNode returnVal = client.execute(op); 
                 String listOfDataSources=returnVal.get("result").toString();
                 return returnVal.get("result").asList();
         }
  • Try the attached TestCase to compile & run the code as following:

Step1). Start your JBoss on Management IP Address as :

./standalone.sh -c standalone-full.xml  -bmanagement 10.65.223.46

Step2). Download and edit the TestCase's "build.xml" file second line to point to your own JBoss home:

Step3). Download ANT (as this demo is ANT based) then set the ANT_HOME and then PATH to include the $ANT_HOME/bin. Once it is done then run the ant as following:

[jsenshar@localhost TestDataSource_Detached]$ ant
Buildfile: build.xml

run:
    [mkdir] Created dir: /NotBackedUp/JBoss_All/jboss-eap-6.0-GA/standalone/TestDataSource_Detached/tmp
    [mkdir] Created dir: /NotBackedUp/JBoss_All/jboss-eap-6.0-GA/standalone/TestDataSource_Detached/build
    [javac] Compiling 1 source file to /NotBackedUp/JBoss_All/jboss-eap-6.0-GA/standalone/TestDataSource_Detached/tmp
      [jar] Building jar: /NotBackedUp/JBoss_All/jboss-eap-6.0-GA/standalone/TestDataSource_Detached/build/remoteEJBClient.jar
   [delete] Deleting directory /NotBackedUp/JBoss_All/jboss-eap-6.0-GA/standalone/TestDataSource_Detached/tmp
     [java] log4j:WARN No appenders could be found for logger (org.jboss.logging).
     [java] log4j:WARN Please initialize the log4j system properly.
     [java] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
     [java] 
     [java] 
     [java]     ******Available Non-XA DataSources are: ["ExampleDS", "MysqlDS"]
     [java] 
     [java] 
     [java]     Non XA DataSource "ExampleDS" is RUNNING Successfully.
     [java] 
     [java]     Non XA DataSource "MysqlDS" is RUNNING Successfully.
     [java] 
     [java] 
     [java]     ******Available Non-XA DataSources are: ["OracleXA_DS"]
     [java] 
     [java] 
     [java]     XA DataSource "OracleXA_DS" is RUNNING Successfully.

The native management API
[1] https://docs.jboss.org/author/display/AS72/The+native+management+API

Attachments

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.