Simple example changing eap deployed mbean property programmatically

Start eap standalone

Set up a clean eap installation if needed

cd /opt
sudo unzip jboss-eap-6.3.2-full-build.zip 
sudo chown -R gerjantd:gerjantd jboss-eap-6.3/
sudo ln -s jboss-eap-6.3 eap
  • Add management user (user admin, password jboss!23)
bin/add-user.sh

Configure and run a copy of standalone

cp -pvr /opt/eap/standalone/ /opt/eap/standalone_mbean
/opt/eap/bin/standalone.sh -Djboss.server.base.dir=/opt/eap/standalone_mbean

Deploy mbean helloworld app

Clone the jboss eap quickstarts repo

cd ~
mkdir -p git/github.com/jboss-developer
cd git/github.com/jboss-developer
git clone https://github.com/jboss-developer/jboss-eap-quickstarts.git

Fix dependency issue in pom

cd helloworld-mbean
vi pom.xml
git diff pom.xml
diff --git a/helloworld-mbean/pom.xml b/helloworld-mbean/pom.xml
index 96330eb..5e46ba3 100644
--- a/helloworld-mbean/pom.xml
+++ b/helloworld-mbean/pom.xml
@@ -48,7 +48,7 @@
         <version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin>
 
         <!-- Define the version of the JBoss BOMs we want to import to specify tested stacks. -->
-        <version.jboss.bom.eap>6.2.0-build-7</version.jboss.bom.eap>
+        <version.jboss.bom.eap>6.2.0.GA</version.jboss.bom.eap>
 
         <!-- other plugin versions -->
         <version.jboss.packaging.plugin>2.2</version.jboss.packaging.plugin>

Build and deploy mbean app

mvn clean jboss-as:deploy -s ../settings.xml

Check deployed mbean

/usr/bin/jconsole
  • Check WelcomeMessage attribute of MBean quickstarts:type=MXComponentHelloWorld

Create simple client to change mbean attribute

Copy and adapt example source

cd git/github.com/jboss-developer/quickstarts/helloworld-mbean
vi JMXExample.java
package client;
import javax.management.*;
import javax.management.remote.*;
import javax.management.Attribute;

public class JMXExample {
    public static void main(String[] args) throws Exception {
        String host = "localhost";
        int port = 9999;
        String urlString ="service:jmx:remoting-jmx://" + host + ":" + port;
        System.out.println("\n\n\t****  urlString: "+urlString);;
        JMXServiceURL serviceURL = new JMXServiceURL(urlString);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
        MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

        ObjectName objectName=new ObjectName("quickstarts:type=MXComponentHelloWorld");
        String message = (String) connection.getAttribute(objectName,"WelcomeMessage");
        System.out.println("\n\tCurrent welcome message for hello world component : " + message);

        System.out.println("\n\tTrying to set message to FOO ");
        Attribute welcomeMessageChange = new Attribute("WelcomeMessage", "FOO");
        connection.setAttribute(objectName,welcomeMessageChange);
    
        message = (String) connection.getAttribute(objectName,"WelcomeMessage");
        System.out.println("\n\tAfter setting the new message is : " + message);

        jmxConnector.close();
    }
}

Compile and run

javac -cp /opt/eap/bin/client/jboss-client.jar:. -d . JMXExample.java
java -cp /opt/eap/bin/client/jboss-client.jar:. client.JMXExample

Check deployed mbean again

/usr/bin/jconsole
  • Check WelcomeMessage attribute of MBean quickstarts:type=MXComponentHelloWorld (should be FOO)