Saturday 19 October 2013

JBoss EAP 6 JBoss Monitoring Java Client

Objective

To demonstrate a Java Client which can fetch JMS statistics of a JBoss EAP 6 running applications.

Environment

  • Eclipse IDE
  • Java 7
  • jboss-cli-client.jar (can be downloaded from Net)

Development

  • Using Eclipse IDE create a Java Project and a class with following main method:

    public static void main(String[] args) {
            String host;
            int port;
            String username;
            String password;
            String[] jmsHostNodes;
            String[] operations;
            String[] queues;
            CLI cli = null;

            try {

                File f = new File("jmsmonitoring.properties");
                if (f.exists()) {
                    Properties jmsMonProperties = new Properties();
                    FileInputStream in = new FileInputStream(f);
                    jmsMonProperties.load(in);

                    // fetch properties
                    host = jmsMonProperties.getProperty("DomainHost");
                    port = Integer.parseInt(jmsMonProperties
                            .getProperty("DomainPort"));
                    username = jmsMonProperties.getProperty("Username");
                    password = jmsMonProperties.getProperty("Password");
                    jmsHostNodes = jmsMonProperties.getProperty("JmsHostNodes")
                            .split(",");
                    operations = jmsMonProperties.getProperty("Operations").split(
                            ",");
                    queues = jmsMonProperties.getProperty("Queues").split(",");
                } else {
                    System.out.println("Input Proerty file missing");
                    return;
                }

                // Get CLI instance
                cli = CLI.newInstance();
                // connect to domain controller
                cli.connect(host, port, username, password.toCharArray());

                // iterate through JMS Nodes and Queues to fetch the needed data
                for (String jmsNode : jmsHostNodes) {
                    StringBuffer cmdString = new StringBuffer("/");
                    cmdString.append(jmsNode);
                    System.out.println("########### JMS Node: " + jmsNode
                            + " #########");
                    for (String queue : queues) {
                        System.out.println("Queue: " + queue);
                        StringBuffer cmdString2 = new StringBuffer(
                                cmdString.toString())
                                .append("/subsystem=messaging/hornetq-server=default/jms-queue=")
                                .append(queue).append("/:read-attribute(name=");
                        for (String operation : operations) {
                            // execute the command
                            System.out.println("command: " + cmdString2.toString()
                                    + operation + ")");
                            Result result = cli.cmd(cmdString2.toString()
                                    + operation + ")");
                            ModelNode response = result.getResponse();
                            String returnValue = response.get("result").asString();
                            System.out.println(operation + " : " + returnValue);
                        }
                    }
                }
            } catch (Exception e) {
                // print exception trace
                e.printStackTrace();
            } finally {
                // disconnect the CLI instance
                if (cli != null)
                    cli.disconnect();
            }
        }
  • The program takes the input parameters like JMS Node, Server and Queue names from a property file. You can create a property file with name "jmsmonitoring.properties" and following contents: 
DomainHost=10.133.2.68
#check the port jboss.management.native.port in domain.xml
DomainPort=10099
Username=admin
Password=admin01
JmsHostNodes=host=host122/server=salcore-jms-node-122,host=host127/server=salcore-jms-node-127
Operations=messages-added,message-count
Queues=alarms,Inventory,sender

  • The program is very generic in the sense that you can add queue names and operation names in the property file and program will be able to fetch and display them. For sample I added just two operations messages-added and message-count only.

Testing

  • Just run the program and you will see the given queues added messages and total count properties in the console.
  • The source code can also be found here: https://github.com/mchopker/myprojects/tree/master/JBossJMSMonitoring

    Thank You!

No comments:

Post a Comment