Tag: sar

SAR dependency alternate approach using @Singleton in JBossAS7.1.2

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.

In previous release of JBoss 5/6 the SAR provided mbeans dependency could be very easily defined using <depends> tag, 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.jca:service=DataSourceBinding,name=MySqlDS</depends>
    </mbean>
</server>

@org.jboss.ejb3.annotation.Service and @org.jboss.ejb3.annotation.Management are no longer supported which could be used to define Services. However in JBossAS7 this approach of defining the dependency is not good and you may see following kind of exceptions at the time of server startup:

JBAS014775:    New missing/unsatisfied dependencies:
      service jboss.mbean.service."jboss.as:subsystem=datasources,data-source=MySqlDS".create (missing) dependents: [service jboss.mbean.service."service.server.monitor:service=MyMonitor".create]
      service jboss.mbean.service."jboss.as:subsystem=datasources,data-source=MySqlDS".start (missing) dependents: [service jboss.mbean.service."service.server.monitor:service=MyMonitor".start]

JBossAS7 is fully EE6 certified application server and it provides a much better approach to create a Service with the help of EJB3.1 specific “javax.ejb.Startup” and “javax.ejb.Singleton” annotations. This kind of service can be made dependent to other services like DataSource. In this example we will see What is the better approach to define a service (without using container specific SAR feature).

The Source code for this demo can be found in the following github link:
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/SingletonServiceDemo

Creating DataSource in JBossAS7.1.2

As we are going make our Service dependent on a DataSource, sothat we can see how the dependency goes.

Configuration-1). So here first of all we will create a DataSource by running the following CLI script in batch mode. So create a file with name “createDataSource.cli” somewhere in your filesystem like “/home/jaysensharma/SingletonServiceDemo/createDataSource.cli” as following:

deploy  /home/jaysensharma/SingletonServiceDemo/mysql-connector-java-5.1.13-bin.jar

/subsystem=datasources/data-source="MySqlDS":add(jndi-name="java:jboss/datasources/MySqlDS",driver-name="mysql-connector-java-5.1.13-bin.jar",connection-url="jdbc:mysql://localhost:3306/testDB",user-name="root",password="redhat")

Configuration-2). Now start your “$JBOSS_HOME/bin/jboss-cli.sh” script in order to run the above deploy the mysql driver and create DataSource as following:

./jboss-cli.sh -c --controller=10.10.10.10:9999 --file=/home/jaysensharma/SingletonServiceDemo/createDataSource.cli

Configuration-3). As soon as you will run the cli script as mentioned above you will see that the following kind of datasource is created on your standalone*.xml.

                <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS">
                    <connection-url>jdbc:mysql://localhost:3306/testDB</connection-url>
                    <driver>mysql-connector-java-5.1.13-bin.jar</driver>
                    <security>
                        <user-name>root</user-name>
                        <password>redhat</password>
                    </security>
                </datasource>

Creating @Singleton service dependent on the DataSource

Step1). First of all we will create a directory somewhere in our file system as “/home/jaysensharma/SingletonServiceDemo” and then we will create a directory with name “src” inside “/home/jaysensharma/SingletonServiceDemo”:

Step2). Now write a @Startup @Singleton EJB3.1 service using “SingletonServiceBean.java” inside “/home/jaysensharma/SingletonServiceDemo/src” as following:

package sar.dependency.alternative;
import javax.ejb.Startup;
import javax.ejb.Singleton;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

//commons logging
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

// DataSource related
import javax.sql.DataSource;
import java.sql.Connection;

// Resource Injection related
import javax.annotation.Resource;

@Startup
@Singleton

public class SingletonServiceBean
 {
     private static Log logger = LogFactory.getLog(SingletonServiceBean.class);
     Connection con=null;


     public SingletonServiceBean()
            {
               logger.info("SingletonServiceBean constructor invoked.");
            }


     @Resource(name = "java:jboss/datasources/MySqlDS")
     DataSource ds;
     /* This @PostConstruct annotation is required to tell JBoss to invoke the "performOperations" immediately after the "SingletonServiceBean" creation. */
     @PostConstruct
     public void performOperations()
	    {
               logger.info("SingletonServiceBean performOperations() invoked.");
               try{
                     con=ds.getConnection();
                     logger.info("***** DataSource = "+ds+" . Connection con = "+con+" retrieved from the ConnectionPool.*****");
                  }
               catch(Exception e)
                 {
                     logger.error("Unable to get Connection From DataSource = "+ds+" ERROR: "+e.getMessage());
                     e.printStackTrace();
                 }
	    }


	@PreDestroy
	public void cleanUp() throws Exception {
	  logger.info("cleaning up connections.");
          if(con!=null)
            {
               try{
                     con.close();
                     logger.info("Connection con = "+con+" is successfully returned to the ConnectionPool.");
                  }
               catch(Exception e)
                 {
                     logger.error("Unable to Close Connection con = "+con+" ERROR: "+e.getMessage());
                     e.printStackTrace();
                 }
               finally{
                        try{   con.close(); }
                        catch(Exception ee) { ee.printStackTrace(); }
                      }
            }
	}
 }

Step3). Now the most important part of building / deploying the Singleton Service which we created, so in order to achieve that we will create a simple ant build script, So Create a “build.xml” file inside the “/home/jaysensharma/SingletonServiceDemo” directory as following:

<project name="SingletonStartupService" default="all">
<property name="jboss.home" value="/home/jaysensharma/jboss-as-7.1.2.Final" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="src.dir" value="src" />
<property name="output.dir" value="build" />
<property name="ear.name" value="TestSingletonService.ear" />
<property name="ejb.jar" value="singletonServiceEJB.jar" />


        <path id="jboss.classpath">
           <fileset dir="${jboss.module.dir}">
               <include name="**/*.jar"/>
           </fileset>
        </path>

        <target name="all" depends="deploy" />

        <target name="build_ear">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}" destdir="${tmp.dir}"  includes="*.java" classpathref="jboss.classpath"/>

           <jar jarfile="${tmp.dir}/${ejb.jar}" basedir="${tmp.dir}" compress="true" />

           <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}/sar"/>
           </delete>
           <mkdir dir="${tmp.dir}/META-INF"/>
           <copy todir="${tmp.dir}/META-INF">
                <fileset dir="${src.dir}/">
                  <include name="application.xml"/>
                </fileset>
           </copy>
           <jar jarfile="${tmp.dir}/${ear.name}" basedir="${tmp.dir}" compress="true" />
           <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}/META-INF"/>
           </delete>
           <delete file="${tmp.dir}/${ejb.jar}"/>

           <copy file="${tmp.dir}/${ear.name}" tofile="${output.dir}/${ear.name}"/>
           <delete dir="${tmp.dir}"/>
        </target>


        <target name="deploy" depends="build_ear">
            <echo message="*******************  Deploying the EAR file ${ear.name} *********************" />
            <echo message="********** ${output.dir}/${ear.name} to ${jboss.home}/standalone/deployments **********" />
            <copy todir="${jboss.home}/standalone/deployments/">
                <fileset dir="${output.dir}/">
                  <include name="${ear.name}"/>
                </fileset>
            </copy>
            <echo message="*******************  Deployed Successfully   *********************" />
        </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 to point to your own JBoss AS7 directory.

Step-4). 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/jaysensharma/jdk1.6.0_21/bin:/home/jaysensharma/org.apache.ant_1.6.5/bin:$PATH

For Windows Based OS:
set PATH=C:/jdk1.6.0_21/bin;C:/org.apache.ant_1.6.5/bin;%PATH%

Step-5). Now once the PATH is set In the command/Shell prompt you can move inside the directory “/home/jaysensharma/SingletonServiceDemo” and then run the ant to build and deploy the EJB based EAR applicationon your JBoss Standalone full profile, by running the command “ant deploy”

[jaysensharma@localhost SingletonServiceDemo]$ ant
Buildfile: build.xml

build_ear:
    [mkdir] Created dir: /home/jaysensharma/SingletonServiceDemo/tmp
    [javac] Compiling 1 source file to /home/jaysensharma/SingletonServiceDemo/tmp
      [jar] Building jar: /home/jaysensharma/SingletonServiceDemo/tmp/singletonServiceEJB.jar
    [mkdir] Created dir: /home/jaysensharma/SingletonServiceDemo/tmp/META-INF
      [jar] Building jar: /home/jaysensharma/SingletonServiceDemo/tmp/TestSingletonService.ear
   [delete] Deleting: /home/jaysensharma/SingletonServiceDemo/tmp/singletonServiceEJB.jar
     [copy] Copying 1 file to /home/jaysensharma/SingletonServiceDemo/build
   [delete] Deleting directory /home/jaysensharma/SingletonServiceDemo/tmp

deploy:
     [echo] *******************  Deploying the EAR file TestSingletonService.ear *********************
     [echo] ********** build/TestSingletonService.ear to /home/jaysensharma/jboss-as-7.1.2.Final/standalone/deployments **********
     [copy] Copying 1 file to /home/jaysensharma/jboss-as-7.1.2.Final/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************

all:

BUILD SUCCESSFUL
Total time: 1 second

Step-6). As the application is deployed on your JBoss so now try restarting your JBoss and then see whether the Above service us successfully get the DataSource and JDBC Connection or not. If the Dependency is correctly working then after restarting your JBoss you will see the following kind of message on your JBoss console / logs.

22:41:51,569 INFO  [sar.dependency.alternative.SingletonServiceBean] (MSC service thread 1-1) ***** DataSource = org.jboss.jca.adapters.jdbc.WrapperDataSource@43877c42 . Connection con = org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@5424bf0 retrieved from the ConnectionPool.*****

.
.
Thanks
MiddlewareMagic Team 🙂


Using JAR file placed inside a SAR in JBoss AS7

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


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


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