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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.