Hi,

This Demonstration is based on “jboss-as-7.1.0.Beta1″ it might require some additional work to be deployed without any issue in other version of JBossAS7, as JBoss will be stable after JBossAS7.1.0Final

Based on the query posted by “http://middlewaremagic.com/jboss/?p=366#comment-40 (kc7bfi)” we are writing this article.
In the previous article “http://middlewaremagic.com/jboss/?p=366” we saw how to create a SAR archive to publish our MBeans inside the JBoss AS7, But many times people face an issue while placing a “Jar” file inside their SAR file on JBoss AS7. As by default the JAR files present inside a SAR file is not loaded automatically in JBoss AS7 so we see many NoClassDeffoundErrors and missing dependencies errors.

In previous article we saw the SAR inside JBoss AS 7 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.

Point-1). In this article we will see what is the use of “jboss-deployment-structure.xml” file.
Point-2). How to place/use a JAR file inside a SAR file.
Point-3). Why “jboss-deployment-structure.xml” file is needed.

More information on file usages “jboss-deployment-structure.xml” can be found in the following link: https://docs.jboss.org/author/display/AS7/Class+Loading+in+AS7

Developing SAR file with JARs

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");
      }
  }

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). Now compile the “MyServerMonitor.java” and “MyServerMonitorMBean.java” programs and then make a JAR containing these class files. Suppose the Jar file name is “Test.jar”. So place this “Test.jar” file inside the SAR file inside as following “/home/userone/ServerMonitorService.sar/Test.jar”

Step6). Now create a file “jboss-deployment-structure.xml” inside “/home/userone/ServerMonitorService.sar/META-INF/jboss-deployment-structure.xml” as following:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <dependencies>
      <module name="deployment.ServerMonitorServiceWorking.sar.Test.jar" />
    </dependencies>
    <resources>
      <resource-root path="Test.jar" />
    </resources>
  </deployment>
</jboss-deployment-structure>

Step7).
Deploy the “ServerMonitorService.sar” inside the “jboss-as-7.1.0.Beta1/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

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

NOTE: If you don’t want to see the message which is displayed in Step7) and don’t want to follow Step8).. 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

Related Issues: http://community.jboss.org/thread/173071

TestCase for JBoss AS7.1.1.Final can be downloaded from Github

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/ServerMonitorServiceWorking.sar

(***NOTE***) In JBoss AS7.1.1.Final i used the following “jboss.deployment-strcture.xml” file Which WORKED for Me…

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
     <resources>
         <resource-root path="Test.jar">
         </resource-root>
      </resources>
  </deployment>
</jboss-deployment-structure>

.
.
.
Thanks
Middleware Magic Team