Tag: MBeans

Listing All MBeans present inside JBossAS7 programatically

Hi,

In this demonstration we will see ow we can get all the MBeans present inside JBoss AS7 CR1b, because many of the developers want to talk to JBoss using their own mbean based java code. In this example we will see that we need not to add any additional JAVA_OPTS in our JBoss configuration files in order to access these MBeans.

Once you get to know the ObjectName of the MBean present inside JBoss AS7 then it becomes very easy to programatically access it’s various operations and attributes. You can refer to some of the previous demos present in middlewaremagic for further research on this direction. For Example: Getting WebService Statistics using MBean Code

JBoss side Configuration

Step1). Open the “/home/userone/jboss-as-7.1.0.CR1b/standalone/configuration/standalone-full.xml” file and edit the subsystem “urn:jboss:domain:jmx:1.1” as following:

        <subsystem xmlns="urn:jboss:domain:jmx:1.1">
            <show-model value="true"/>
            <jmx-connector registry-binding="jmx-connector-registry" server-binding="jmx-connector-server"/>
        </subsystem>

Step2). Restart your JBoss Profile from “/home/userone/jboss-as-7.1.0.CR1b/bin” directory like following:

[userone@localhost bin]$ ./standalone.sh -c standalone-full.xml

Step3). Now Somewhere in your filesystem (like /home/userone/MBeanDemo directory) write a Java Program “TestMBeanServiceMonitor.java” as following:

import java.util.*;
import java.util.Hashtable;
import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import javax.management.*;
import javax.management.openmbean.CompositeDataSupport;
 
public class TestMBeanServiceMonitor
{
    private static MBeanServerConnection connection;
    private static JMXConnector connector;
    public static void Connection(String hostname, String port) throws IOException
    {
        Integer portInteger = Integer.valueOf(port);
        Hashtable h = new Hashtable();
        JMXServiceURL address = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://"+hostname+":"+port+"/jmxrmi");
        connector = JMXConnectorFactory.connect(address,null);
        connection = connector.getMBeanServerConnection();
        System.out.println("GOT THE MBeanServerConnection---SUCCESSFULLY");
    }
  
    private static void listAllJBossAS7MBeans() throws Exception
    {
        //ObjectName serviceRef=new ObjectName("jboss.as:*");
        ObjectName serviceRef=new ObjectName("*.*:*");
        Set<ObjectName> mbeans = connection.queryNames(serviceRef, null);
        for (ObjectName on : mbeans) {
                  System.out.println("t ObjectName : "+on);
        }
    }

    private static void getThreadDetails() throws Exception
    {
        ObjectName serviceRef=new ObjectName("java.lang:type=Threading");
        Integer daemonThreadCount=(Integer)connection.getAttribute(serviceRef, "DaemonThreadCount");
        System.out.println("t daemonThreadCount : "+daemonThreadCount);
    }

    public static void main(String[] args) throws Exception
    {
        String hostname = args[0];
        String port = args[1];    
        /*
        ***** The port binding should be defined inside your "standalone-full.xml" file.  *****
        *  
        <subsystem xmlns="urn:jboss:domain:jmx:1.1">
            <show-model value="true"/>
            <jmx-connector registry-binding="jmx-connector-registry" server-binding="jmx-connector-server"/>
        </subsystem>
        */

        Connection(hostname, port); 
        System.out.println("nt All JBoss AS7 MBean Listing nn");
        listAllJBossAS7MBeans() ;
    }
}

Step4). Now we will write a simple Ant build script to build and execute the above program. So write an Ant build script “/home/userone/MBeanDemo/build.xml” file in the same directory where the above program is placed.

<project name="JBoss_Monitor" default="run">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.CR1b/" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="base.dir" value="." />
 
   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>
     </fileset>
   </path>
 
   <target name="run">
       <javac srcdir="${base.dir}" destdir="${base.dir}"  includes="TestMBeanServiceMonitor.java" classpathref="jboss.classpath"/>
       <java classname="TestMBeanServiceMonitor">
           <classpath>
              <pathelement location="${base.dir}"/>
              <path refid="jboss.classpath"/>
           </classpath>
           <arg line="localhost 1090"/>
       </java>
   </target>
</project>

NOTE: The only change in the above file you need to do is to change the “jboss.home” directory path in the second line of the above script is to point to your own JBoss AS7 directory home directory.

NOTE: Also Make sure that you are writing the correct HostName and Port in the above ant script of your JBoss AS7 here arg line=”localhost 1090″

Step5). Now before running your ANT script to build and deploy the above webapplication you should have the ANT as well as JAVA set in the $PATH variable of the Shell / command prompt as following:

For Unix Based OS:
export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/apache-ant-1.8.2/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/apache-ant-1.8.2/bin;%PATH%

Step7). Run the command “ant” or “ant run” which will internally build/compile and run our MBean Code

[userone@localhost Monitoring]$ ant
Buildfile: /home/userone/jboss-as-7.1.0.CR1b/standalone/MBeanDemo/build.xml

run:
    [javac] /home/userone/jboss-as-7.1.0.CR1b/standalone/MBeanDemo/build.xml:13: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
      GOT THE MBeanServerConnection---SUCCESSFULLY
      
      	 All JBoss AS7 MBean Listing 
      
      
      	 ObjectName : jboss.as:core-service=server-environment
      	 ObjectName : jboss.as:extension=org.jboss.as.web
      	 ObjectName : jboss.as:extension=org.jboss.as.remoting
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,remote-destination-outbound-socket-binding=mail-smtp
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=jacorb-ssl
      	 ObjectName : jboss.as:subsystem=ejb3,cache=simple
      	 ObjectName : jboss.as:core-service=management,management-interface=native-interface
      	 ObjectName : jboss.as:deployment=EE6_BeanValidationDemo.war
      	 ObjectName : jboss.as:subsystem=security,security-domain=jboss-web-policy,authorization=classic
      	 ObjectName : jboss.as:extension=org.jboss.as.weld
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.aries.util
      	 ObjectName : jboss.as:subsystem=naming
      	 ObjectName : jboss.as:extension=org.jboss.as.connector
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,security-setting=#,role=guest
      	 ObjectName : jboss.as:subsystem=jca,workmanager=default
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate
      	 ObjectName : jboss.as:subsystem=logging,logger=org.apache.tomcat.util.modeler
      	 ObjectName : java.lang:type=MemoryPool,name=Code Cache
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,connection-factory=RemoteConnectionFactory
      	 ObjectName : jboss.as:deployment=Log4jDemo.war
      	 ObjectName : jboss.as:subsystem=ee
      	 ObjectName : jboss.as:subsystem=ejb3,thread-pool=default
      	 ObjectName : jboss.as:subsystem=webservices,endpoint-config=Standard-Endpoint-Config
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=entity
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=jacorb
      	 ObjectName : jboss.as:subsystem=datasources,data-source=H2DS
      	 ObjectName : jboss.as:subsystem=ejb3,service=async
      	 ObjectName : jboss.as:subsystem=jca,bean-validation=bean-validation
      	 ObjectName : jboss.as:subsystem=security,security-domain=jboss-ejb-policy
      	 ObjectName : jboss.as:extension=org.jboss.as.jpa
      	 ObjectName : java.lang:type=MemoryPool,name=PS Eden Space
      	 ObjectName : java.lang:type=Memory
      	 ObjectName : jboss.as:deployment=MyDemoMDB.jar
      	 ObjectName : jboss.as:subsystem=logging,console-handler=CONSOLE
      	 ObjectName : jboss.as:subsystem=ejb3,cache=passivating
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=jmx-connector-server
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=remoting
      	 ObjectName : jboss.as:extension=org.jboss.as.security
      	 ObjectName : jboss.as:extension=org.jboss.as.ejb3
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.aries.jmx
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.xerces
      	 ObjectName : jboss.as:extension=org.jboss.as.transactions
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-connector=netty-throughput,param=batch-delay
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets
      	 ObjectName : jboss.as:subsystem=deployment-scanner
      	 ObjectName : jboss.as:deployment=MyDemoMDB.jar,subsystem=ejb3,message-driven-bean=MyMDB
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,pooled-connection-factory=hornetq-ra
      	 ObjectName : jboss.as:extension=org.jboss.as.logging
      	 ObjectName : jboss.as:subsystem=jaxrs
      	 ObjectName : jboss.modules:type=ModuleLoader,name=LocalModuleLoader-3
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=management-native
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default
      	 ObjectName : jboss.as:subsystem=jca,archive-validation=archive-validation
      	 ObjectName : jboss.jsr77:j2eeType=JVM,name=default,J2EEServer=default
      	 ObjectName : jboss.as:deployment=TestWebApp.war,subsystem=web,servlet=servlets.TestServlet
      	 ObjectName : jboss.jta:type=ObjectStore
      	 ObjectName : java.lang:type=MemoryPool,name=PS Survivor Space
      	 ObjectName : jboss.as:extension=org.jboss.as.ee
      	 ObjectName : jboss.as:subsystem=logging,periodic-rotating-file-handler=FILE
      	 ObjectName : jboss.modules:type=ModuleLoader,name=ModuleLoaderIntegration-7
      	 ObjectName : jboss.as:subsystem=web,connector=http
      	 ObjectName : java.lang:type=Compilation
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,core-address=jms.topic.testTopic
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.webconsole
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,core-address=jms.queue.testQueue
      	 ObjectName : jboss.as:subsystem=osgi,configuration=org.apache.felix.webconsole.internal.servlet.OsgiManager
      	 ObjectName : jboss.as:subsystem=deployment-scanner,scanner=default
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,jms-queue=testQueue
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.as.osgi.configadmin
      	 ObjectName : jboss.modules:type=ModuleLoader,name=ServiceModuleLoader-5
      	 ObjectName : jboss.as:subsystem=jca,bootstrap-context=default
      	 ObjectName : jboss.as:subsystem=mail
      	 ObjectName : java.lang:type=Runtime
      	 ObjectName : jboss.as:deployment=TestWebApp.war
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.netty
      	 ObjectName : jboss.as:deployment=EE6_BeanValidationDemo.war,subsystem=web,servlet=Faces_Servlet
      	 ObjectName : jboss.as:subsystem=pojo
      	 ObjectName : jboss.as:extension=org.jboss.as.messaging
      	 ObjectName : jboss.ws:service=ServerConfig
      	 ObjectName : jboss.as:management-root=server
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-acceptor=netty-throughput,param=batch-delay
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=timestamps,singleton=eviction
      	 ObjectName : jboss.as:subsystem=jca,workmanager=default,long-running-threads=default-long-running-threads
      	 ObjectName : jboss.as:subsystem=jca,cached-connection-manager=cached-connection-manager
      	 ObjectName : jboss.as:subsystem=ejb3,service=iiop
      	 ObjectName : jboss.as:extension=org.jboss.as.pojo
      	 ObjectName : jboss.as:subsystem=osgi,capability=javax.transaction.api
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.felix.eventadmin
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.osgi.compendium
      	 ObjectName : jboss.jsr77:j2eeType=WebModule,name=TestWebApp.war,J2EEServer=default
      	 ObjectName : jboss.as:subsystem=threads
      	 ObjectName : jboss.jsr77:j2eeType=WebModule,name=Log4jDemo.war,J2EEServer=default
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.felix.configadmin
      	 ObjectName : jboss.as:extension=org.jboss.as.sar
      	 ObjectName : jboss.jsr77:j2eeType=WebModule,name=EE6_BeanValidationDemo.war,J2EEServer=default
      	 ObjectName : java.util.logging:type=Logging
      	 ObjectName : jboss.as:extension=org.jboss.as.mail
      	 ObjectName : jboss.msc:type=container,name=jboss-as
      	 ObjectName : jboss.as:subsystem=infinispan
      	 ObjectName : com.sun.management:type=HotSpotDiagnostic
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-connector=netty-throughput
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=local-query
      	 ObjectName : java.lang:type=GarbageCollector,name=PS Scavenge
      	 ObjectName : jboss.as:subsystem=ejb3,strict-max-bean-instance-pool=mdb-strict-max-pool
      	 ObjectName : jboss.as:extension=org.jboss.as.jsr77
      	 ObjectName : jboss.as:subsystem=transactions
      	 ObjectName : jboss.as:extension=org.jboss.as.jdr
      	 ObjectName : jboss.as:subsystem=security,security-domain=other,authentication=classic
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=messaging
      	 ObjectName : jboss.as:subsystem=security,security-domain=other
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-acceptor=netty-throughput
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.projectodd.stilts
      	 ObjectName : jboss.as:subsystem=ejb3,strict-max-bean-instance-pool=slsb-strict-max-pool
      	 ObjectName : jboss.as:subsystem=webservices,endpoint-config=Recording-Endpoint-Config
      	 ObjectName : jboss.as:core-service=management,security-realm=ManagementRealm,authentication=properties
      	 ObjectName : jboss.as:deployment=BeanValidation_EJB_Demo.ear
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,security-setting=#
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,in-vm-acceptor=in-vm
      	 ObjectName : jboss.as:subsystem=osgi,capability=javax.servlet.api
      	 ObjectName : jboss.as:subsystem=security,security-domain=jboss-ejb-policy,authorization=classic
      	 ObjectName : jboss.as:deployment=BeanValidation_EJB_Demo.ear,subdeployment=localEJB.jar,subsystem=ejb3,stateless-session-bean=CallerName
      	 ObjectName : jboss.as:subsystem=cmp
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=entity,singleton=eviction
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=txn-recovery-environment
      	 ObjectName : jboss.as:extension=org.jboss.as.jaxrs
      	 ObjectName : jboss.as:subsystem=ejb3,service=timer-service
      	 ObjectName : jboss.as:extension=org.jboss.as.webservices
      	 ObjectName : java.lang:type=MemoryPool,name=PS Perm Gen
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=local-query,singleton=expiration
      	 ObjectName : jboss.as:subsystem=webservices
      	 ObjectName : jboss.as:subsystem=jpa
      	 ObjectName : jboss.as:core-service=management
      	 ObjectName : jboss.as:subsystem=logging,logger=jacorb
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.http
      	 ObjectName : java.lang:type=MemoryPool,name=PS Old Gen
      	 ObjectName : jboss.as:subsystem=web,configuration=jsp-configuration
      	 ObjectName : jboss.as:subsystem=web,configuration=static-resources
      	 ObjectName : jboss.as:subsystem=sar
      	 ObjectName : jboss.as:core-service=management,security-realm=ManagementRealm
      	 ObjectName : java.lang:type=GarbageCollector,name=PS MarkSweep
      	 ObjectName : jboss.as:subsystem=jca,workmanager=default,short-running-threads=default-short-running-threads
      	 ObjectName : jboss.as:deployment=BeanValidation_EJB_Demo.ear,subdeployment=localEJB.jar
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=txn-status-manager
      	 ObjectName : jboss.as:deployment=EE6_BeanValidationDemo.war,subsystem=web
      	 ObjectName : jboss.as:subsystem=osgi,property=org.osgi.framework.startlevel.beginning
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,in-vm-connector=in-vm
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,address-setting=#
      	 ObjectName : jboss.as:subsystem=ejb3
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.felix.metatype
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.felix.scr
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.felix.log
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.blueprint
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-acceptor=netty
      	 ObjectName : jboss.as:subsystem=logging
      	 ObjectName : jboss.as:subsystem=jca
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=osgi-http
      	 ObjectName : jboss.as:subsystem=datasources
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.webapp
      	 ObjectName : jboss.jsr77:j2eeType=J2EEDomain,name=jboss.jsr77
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=jmx-connector-registry
      	 ObjectName : jboss.as:subsystem=security
      	 ObjectName : jboss.as:subsystem=jdr
      	 ObjectName : java.lang:type=ClassLoading
      	 ObjectName : jboss.as:subsystem=datasources,jdbc-driver=h2
      	 ObjectName : java.lang:type=Threading
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.logging
      	 ObjectName : jboss.as:subsystem=security,security-domain=jboss-web-policy
      	 ObjectName : jboss.as:subsystem=jmx
      	 ObjectName : jboss.as:subsystem=logging,logger=sun.rmi
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,jms-topic=testTopic
      	 ObjectName : jboss.as:subsystem=ejb3,file-passivation-store=file
      	 ObjectName : jboss.as:deployment=Log4jDemo.war,subsystem=web,servlet=servlets.TestServlet
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=messaging-throughput
      	 ObjectName : jboss.as:subsystem=resource-adapters
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=management-http
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=https
      	 ObjectName : jboss.as:subsystem=jacorb
      	 ObjectName : jboss.as:core-service=service-container
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-acceptor=netty-throughput,param=direct-deliver
      	 ObjectName : jboss.as:extension=org.jboss.as.osgi
      	 ObjectName : jboss.jsr77:j2eeType=J2EEApplication,name=BeanValidation_EJB_Demo.ear,J2EEServer=default
      	 ObjectName : jboss.as:subsystem=remoting,connector=remoting-connector
      	 ObjectName : jboss.as:interface=public
      	 ObjectName : jboss.as:interface=management
      	 ObjectName : jboss.as:extension=org.jboss.as.jacorb
      	 ObjectName : java.lang:type=MemoryManager,name=CodeCacheManager
      	 ObjectName : jboss.as:extension=org.jboss.as.clustering.infinispan
      	 ObjectName : jboss.as:subsystem=ejb3,service=remote
      	 ObjectName : jboss.as:subsystem=weld
      	 ObjectName : jboss.as:deployment=Log4jDemo.war,subsystem=web
      	 ObjectName : jboss.jsr77:j2eeType=J2EEServer,name=default
      	 ObjectName : jboss.as:subsystem=logging,logger=com.arjuna
      	 ObjectName : jboss.as:subsystem=logging,root-logger=ROOT
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.apache.felix.webconsole
      	 ObjectName : jboss.as:deployment=MyDemoMDB.jar,subsystem=ejb3
      	 ObjectName : jboss.as:extension=org.jboss.as.cmp
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=local-query,singleton=eviction
      	 ObjectName : jboss.as:subsystem=remoting
      	 ObjectName : jboss.as:subsystem=logging,logger=jacorb.config
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=entity,singleton=expiration
      	 ObjectName : jboss.as:subsystem=web,virtual-server=default-host
      	 ObjectName : jboss.as:extension=org.jboss.as.deployment-scanner
      	 ObjectName : jboss.as:deployment=BeanValidation_EJB_Demo.ear,subdeployment=localEJB.jar,subsystem=ejb3
      	 ObjectName : jboss.as:extension=org.jboss.as.threads
      	 ObjectName : jboss.as:core-service=management,management-interface=http-interface
      	 ObjectName : jboss.as:extension=org.jboss.as.naming
      	 ObjectName : java.lang:type=OperatingSystem
      	 ObjectName : jboss.as:subsystem=osgi
      	 ObjectName : jboss.as:extension=org.jboss.as.jmx
      	 ObjectName : jboss.as:subsystem=messaging
      	 ObjectName : jboss.as:subsystem=osgi,capability=org.jboss.osgi.jmx
      	 ObjectName : jboss.as:subsystem=infinispan,cache-container=hibernate,local-cache=timestamps
      	 ObjectName : jboss.as:deployment=TestWebApp.war,subsystem=web
      	 ObjectName : jboss.as:subsystem=osgi,capability=javax.api
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,connection-factory=InVmConnectionFactory
      	 ObjectName : jboss.as:socket-binding-group=standard-sockets,socket-binding=http
      	 ObjectName : jboss.as:subsystem=messaging,hornetq-server=default,remote-connector=netty
      	 ObjectName : jboss.as:subsystem=web
      	 ObjectName : jboss.as:subsystem=mail,mail-session=java%x3ajboss/mail/Default
      	 ObjectName : jboss.as:subsystem=jsr77
      	 ObjectName : jboss.as:subsystem=jmx,connector=jmx
      	 ObjectName : jboss.as:subsystem=web,configuration=container

BUILD SUCCESSFUL
Total time: 0 seconds

So like this we got all the list of MBean ObjectName present inside JBoss AS7.
.
.
Thanks 🙂
MiddlewareMagic Team


How to create a Service Archieve SAR file in JBoss AS7

Hi,

SAR files are called as Service Archives. The extension of SAR files are *.sar which contains a “META-INF/jboss-service.xml” file, this file describes the custom MBeans which has to be exposed at the time of deployment or JBoss Startup. JBoss’s service archive architecture is based on the JMX and the SAR files which are basically JBoss specific can be deployed in the jboss inorder to publish your MBean based services. At the time of JBoss startup these service archive deployer (SARDeployer) instantiates the JBoss service classes and exposes them as manageable beans through JMX. These beans can be viewed and edited from the jmx-console as well.

You can refer to the sample of creating service archives in JBoss AS6 in the following link: http://middlewaremagic.com/jboss/?p=307 .

But in JBoss AS 7 it is little different as the class “org.jboss.system.ServiceMBean” and “org.jboss.system.ServiceMBeanSupport” are not present in JBoss AS7. Also the JBoss AS 7 service descriptor (jboss-service.xml) file requires XSD declaration, And without the XSD declaration inside jboss-service.xml file you will see the following kind of Exception while deploying your SAR file.

23:10:52,007 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC00001: Failed to start service jboss.deployment.unit."ServerMonitorService.sar".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."ServerMonitorService.sar".PARSE: Failed to process phase PARSE of deployment "ServerMonitorService.sar"
	at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:121)
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1765)
	at org.jboss.msc.service.ServiceControllerImpl$ClearTCCLTask.run(ServiceControllerImpl.java:2291)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_21]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_21]
	at java.lang.Thread.run(Thread.java:619) [:1.6.0_21]
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: Failed to parse service xml ["/home/userone/jboss-as-7.0.1.Final/standalone/deployments/ServerMonitorService.sar/META-INF/jboss-service.xml"]
	at org.jboss.as.service.ServiceDeploymentParsingProcessor.deploy(ServiceDeploymentParsingProcessor.java:94)
	at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:115)
	... 5 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[3,1]
Message: Unexpected element 'server'
	at org.jboss.staxmapper.XMLMapperImpl.processNested(XMLMapperImpl.java:98)
	at org.jboss.staxmapper.XMLMapperImpl.parseDocument(XMLMapperImpl.java:59)
	at org.jboss.as.service.ServiceDeploymentParsingProcessor.deploy(ServiceDeploymentParsingProcessor.java:87)

Also in JBoss AS7 we need to implement the start() and stop() service if we want to perform some specific operations while jboss starts and stops the service. So here we will try to develop a Service Archive in this sample.

Step1). Create a directory somewhere in your file system like “/home/userone/ServerMonitorService.sar”.

Step2). Create an MBean interface “MyServerMonitorMBean.java” inside the “/home/userone/ServerMonitorService.sar” directory.

package custom.mbean;
public interface MyServerMonitorMBean
  {
      public void setFrequency(String frequency);
      public String getFrequency();
  }

Step3). Now provide an implementation class of the above MBean interface as “MyServerMonitor.java” inside the directory “/home/userone/ServerMonitorService.sar”. And implement the start() and stop() service if we want to perform some specific operations while jboss starts and stops the service.

package custom.mbean;
import javax.management.*;
import java.io.*;
import java.util.*;
import java.rmi.*;
import javax.naming.*;
public class MyServerMonitor implements MyServerMonitorMBean
  {
      boolean flag=true;
      public String Frequency;

      public MyServerMonitor()
      {
         System.out.println("nnt ServiceMonitorMBean is activated...inside ServiceMonitor() constructor--setting default Frequency=10000 Miliseconds");;
      }

      public void setFrequency(String Frequency)
      {
          System.out.println("nt Server Watch Frequency is set to : "+Frequency+"-Milliseconds");
          this.Frequency=Frequency;
      }

      public String getFrequency()
      {
          System.out.println("nt Server Watch Frequency is set to : "+Frequency+"-Milliseconds");
          return this.Frequency;
      }

     public void start() throws Exception
      {
            System.out.println("nntStarting start() MyServerMonitor invoked");
            Frequency="3000";
      }

     public void stop() throws Exception
      {
            System.out.println("nntStopping stop() MyServerMonitor  invoked");
      }
  }

Now compile the above Codes as following

cd   /home/userone/ServerMonitorService.sar/
javac   -d   .   *.java

Step4). Now the most important part, here we need to declare the MBean inside the “/home/userone/ServerMonitorService.sar/META-INF/jboss-service.xml” file as following:

<?xml version="1.0" encoding="UTF-8"?>
<server xmlns="urn:jboss:service:7.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="urn:jboss:service:7.0 jboss-service_7_0.xsd">

    <mbean code="custom.mbean.MyServerMonitor" name="service.server.monitor:service=MyMonitor">
         <attribute name="Frequency">5000</attribute>
    </mbean>

</server>

NOTE: The XSD declaration is required without that you will see parsing error while deploying your service.

Step5). Deploy the “ServerMonitorService.sar” inside the “jboss-as-7.0.1.Final/standalone/deployments” directory of your JBoss. After deploying the service archive you may see the following Message in server’s Console Stdout….Because the “ServerMonitorService.sar” which we are trying to deploy is in Exploded format:

23:19:17,410 INFO  [org.jboss.as.deployment] (DeploymentScanner-threads - 1) Found ServerMonitorService.sar in deployment directory. To trigger deployment create a file called ServerMonitorService.sar.dodeploy

Step6). So create an empty file with name “ServerMonitorService.sar.dodeploy” inside “jboss-as-7.0.1.Final/standalone/deployments” to tell jboss to deploy this Service Archive.

NOTE: If you don’t want to see the message which is displayed in Step5) and don’t want to follow Step6).. then deploy your SAR file as an archive file. (similar to how we create jar/war/ear files using jar utility)

JIRA related to current demonstration: https://issues.jboss.org/browse/AS7-887
.
.
Thanks
Middleware Magic Team


How to create a Service Archieve SAR file in JBoss AS6

Hi,
NOTE: this article is focused on JBoss AS6 and earlier versions. For JBoss AS7 service archives please refer to: http://middlewaremagic.com/jboss/?p=366

SAR files are called as Service Archives. The extension of SAR files are *.sar which contains a “META-INF/jboss-service.xml” file, this file describes the custom MBeans which has to be exposed at the time of deployment or JBoss Startup. JBoss’s service archive architecture is based on the JMX and the SAR files which are basically JBoss specific can be deployed in the jboss inorder to publish your MBean based services. At the time of JBoss startup these service archive deployer (SARDeployer) instantiates the JBoss service classes and exposes them as manageable beans through JMX. These beans can be viewed and edited from the jmx-console as well.

One of the best usage of the service archieve is to monitor the servers activities. In this simple example we will try to get JBoss Server’s basic informations using the JBoss MBean “jboss.system:type=Server” using our SAR file.

Step1). Create a directory somewhere in your file system like “/home/userone/ServerMonitorService.sar”.

Step2). Create an MBean interface by extending the interface “org.jboss.system.ServiceMBean” as following. “MyServerMonitorMBean.java” inside the “/home/userone/ServerMonitorService.sar” directory.

package custom.mbean;
import org.jboss.system.ServiceMBean;
public interface MyServerMonitorMBean extends org.jboss.system.ServiceMBean
  {
      public void setFrequency(String frequency);
      public String getFrequency();
  }

Step3). Now provide an implementation class of the above MBean interface as “MyServerMonitor.java” inside the directory “/home/userone/ServerMonitorService.sar”. Make sure that this class extends the “org.jboss.system.ServiceMBeanSupport” class because this class provides some useful methods like “startService()” and “stopService()” which are the life cycle methids of our beans.

package custom.mbean;
import javax.management.*;
import java.io.*;
import java.util.*;
import java.rmi.*;
import javax.naming.*;
public class MyServerMonitor extends org.jboss.system.ServiceMBeanSupport implements MyServerMonitorMBean
  {
      boolean flag=true;
      public String Frequency;
      private MBeanServerConnection server=null;
      public void setFrequency(String Frequency)
      {
          System.out.println("nt Server Watch Frequency is set to : "+Frequency+"-Milliseconds");
          this.Frequency=Frequency;
      }
      public String getFrequency()
      {
          System.out.println("nt Server Watch Frequency is set to : "+Frequency+"-Milliseconds");
          return this.Frequency;
      }
      public MyServerMonitor()
      {
         System.out.println("nnt ServiceMonitorMBean is activated...inside ServiceMonitor() constructor--setting default Frequency=10000 Miliseconds");;
      }

     public void startService() throws Exception
      {
         log.info("nntStarting MyServerMonitor");
            try{
            Hashtable ht=new Hashtable();
            ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.security.jndi.JndiLoginInitialContextFactory");
            ht.put(Context.PROVIDER_URL,"localhost:1099");
            ht.put(Context.SECURITY_PRINCIPAL,"admin");
            ht.put(Context.SECURITY_CREDENTIALS,"admin");
            System.out.println("nt 1- Gotting InitialContext...... ");
            Context ctx = new InitialContext(ht);
            System.out.println("nt 2- Got InitialContext: "+ctx);
            server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
            }
            catch(Exception e)
            {
                System.out.println("nnt Exception inside MyServerMonitor..."+e);
            }
            Frequency="5000";
            ServerMonitorRunner runner=new ServerMonitorRunner();
      }

      public void monitor() throws Exception
      {
            String data="";
            data=data+"nnt===================MyServerMonitor.monitor()===================";
            data=data+"nBuildOS = "+ (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("BuildOS"));
            data=data+"nVersionName = "+ (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("VersionName"));
            data=data+"nBuildDate = "+ (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("BuildDate"));
            data=data+"nInShutdown = "+ (Boolean)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("InShutdown"));
            data=data+"nStartDate = "+ (java.util.Date)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("StartDate"));
            data=data+"nStarted = "+ (Boolean)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("Started"));
            data=data+"nBuildNumber = "+ (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("BuildNumber"));
            data=data+"nBuildJVM = "+ (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("BuildJVM"));
            data=data+"nVersionNumber = "+ (String)server.getAttribute(new ObjectName("jboss.system:type=Server"), new String("VersionNumber"));
            data=data+"nt===============================================================";
            System.out.println(data);
      }

     public void stopService() throws Exception
      {
         log.info("nntStopping MyServerMonitor");
      }
     class ServerMonitorRunner extends Thread
       {
           ServerMonitorRunner()
            {
                this.start();
            }
           public void run()
             {
                for(; 😉
                  {
                    try{
                          Thread.sleep(Integer.parseInt(Frequency));
                          monitor();
                       }
                    catch(InterruptedException ie){ ie.printStackTrace();}
                    catch(Exception e){ e.printStackTrace();}

                  }
             }
       }
  }

Step4). Now the most important part, here we need to declare the MBean inside the “/home/userone/ServerMonitorService.sar/META-INF/jboss-service.xml” file as following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>
<server>
    <mbean code="custom.mbean.MyServerMonitor" name="service.server.monitor:service=MyMonitor">
         <attribute name="Frequency">5000</attribute>
         <depends>jboss.jmx:type=adaptor,name=Invoker,protocol=jrmp,service=proxyFactory</depends>
    </mbean>
</server>

NOTE: here inside the tag we have used the tag because our MBean is dependent on another MBean “jboss.jmx:type=adaptor,name=Invoker,protocol=jrmp,service=proxyFactory”…this is why because our MBean code is performing ic.lookup() using the “jmx/invoker/RMIAdaptor” so we need to make sure that the “jboss.jmx:type=adaptor,name=Invoker,protocol=jrmp,service=proxyFactory” service gets activated before our MBean. otherwise we will see NullPointerException while performing lookup inside our bean.

Step5). Deploy the “ServerMonitorService.sar” inside the “${PROFILE}/deploy” directory of your JBoss. And then you will start seeing the following kind of messages in the server Console :

12:55:53,949 INFO  [STDOUT]
	 ServiceMonitorMBean is activated...inside ServiceMonitor() constructor--setting default Frequency=10000 Miliseconds
12:55:53,952 INFO  [STDOUT]
	 Server Watch Frequency is set to : 5000-Milliseconds
12:55:53,952 INFO  [MyServerMonitor]

	Starting MyServerMonitor
12:55:53,952 INFO  [STDOUT]
	 1- Gotting InitialContext......
12:55:53,964 INFO  [STDOUT]
	 2- Got InitialContext: javax.naming.InitialContext@588a4b96
12:55:59,167 INFO  [STDOUT]

	===================MyServerMonitor.monitor()===================
BuildOS = Linux(i386,2.6.18-194.8.1.el5)
VersionName = JBoss AS
InShutdown = false
StartDate = Sun Oct 16 12:19:59 IST 2011
Started = true
BuildJVM = 17.0-b16(Sun Microsystems Inc.)
VersionNumber = 6.0.
	===============================================================

The MyServerMonitor.monitor() will be executed after every 5 Seconds as we have set this frequency as 5 seconds inside the jboss-service.xml file.

Step6). If you want to change this frequency to something else then you can login to jmx-console and then search for “service.server.monitor” in the left Panel then by clicking on it you will be able to see the frequency which you can change.

.
.
Thanks
Middleware Magic Team


Copyright © 2010-2012 Middleware Magic. All rights reserved. |