Archive for October, 2015

How to run jboss-cli using maven plugins in WildFly 10 CR3 ?

Hi,

As part of the demo we will see how to execute the WildFLy jboss-cli.sh task using maven plugin. This demo will be useful for the developers who are more comfortable with Maven and wants to accomplish general monitoring/configuration tasks on WildFly 10 using the Maven based approach. We can execute any CLI command from the below mentioned maven task.

In this demo we will see how to monitor the EJB3 subsystem details using maven based approach which internally invokes the “jboss-cli.sh” script.   (Java 8 is needed to run this demo on WildFly 10 see bottom of this article.)

Step-1). Create a directory somewhere in your filesystem to place the “test.cli” WildFLy script and the maven based “pom.xml”.

  $ mkdir Running_CLI_Using_Maven
  $ cd Running_CLI_Using_Maven

Step-2). Create a simple “Running_CLI_Using_Maven/test.cli” script as following (We can have any kind of JBoss CLI command inside this script file, Like creating DataSource, deploying/undeploying applications ..etc)

    /subsystem=ejb3/:read-resource(include-runtime=true)

Step-3). Now the main part is to write a simple Maven based “Running_CLI_Using_Maven/pom.xml” file as following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	     
	<modelVersion>4.0.0</modelVersion>
	<groupId>middleware.magic</groupId>
	<artifactId>RunningCLI_ViaMaven</artifactId>
	<packaging>jar</packaging>
	<version>1.0</version>
	
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <wildfly.home>/PATH/TO//wildfly-10.0.0.CR3-SNAPSHOT</wildfly.home> <!-- CHANGE ME !!! -->
        <wildfly.hostname>localhost</wildfly.hostname>   <!-- CHANGE ME !!! -->
        <wildfly.user>admin</wildfly.user>  <!-- CHANGE ME !!! -->
        <wildfly.pass>admin@123</wildfly.pass>  <!-- CHANGE ME !!! -->
        <wildfly.port>9990</wildfly.port>  <!-- CHANGE ME !!! -->
    </properties>

	<build>
          <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <!-- http://ant-contrib.sourceforge.net/
                                <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath" /> -->
                                <exec dir="${basedir}" executable="${wildfly.home}/bin/jboss-cli.sh" failonerror="true">
                                  <arg line="--controller=${wildfly.hostname}:${wildfly.port} --user=${wildfly.user} --password=${wildfly.pass} -c file=${basedir}/test.cli" />
                                </exec>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                
                <dependencies>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>20020829</version>
                    </dependency>
                </dependencies>
            </plugin>

	 </plugins>
     </build>
</project>

Step-4). Now we will run the maven project as following (please make sure to start the WildFly first). Open a terminal and then set the following variables. We can run the following command: “mvn antrun:run”

$ export M2_HOME=/PATH/TO/apache_maven_3.2.3
$ export JAVA_HOME=/PATH/TO/jdk1.8.0_60
$ export PATH=$JAVA_HOME/bin:/PATH/TO/apache_maven_3.2.3/bin:$PATH

$ cd Running_CLI_Using_Maven
$ mvn clean install antrun:run


[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building RunningCLI_ViaMaven 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ RunningCLI_ViaMaven ---
[INFO] Deleting /PATH/TO//Running_CLI_Using_Maven/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ RunningCLI_ViaMaven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ RunningCLI_ViaMaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ RunningCLI_ViaMaven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /PATH/TO/Running_CLI_Using_Maven/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ RunningCLI_ViaMaven ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ RunningCLI_ViaMaven ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ RunningCLI_ViaMaven ---
[INFO] Building jar: /PATH/TO/Running_CLI_Using_Maven/target/RunningCLI_ViaMaven-1.0.jar
[INFO] 
[INFO] --- maven-antrun-plugin:1.7:run (default) @ RunningCLI_ViaMaven ---
[INFO] Executing tasks

main:
     [exec] {
     [exec]     "outcome" => "success",
     [exec]     "result" => {
     [exec]         "default-clustered-sfsb-cache" => undefined,
     [exec]         "default-distinct-name" => undefined,
     [exec]         "default-entity-bean-instance-pool" => undefined,
     [exec]         "default-entity-bean-optimistic-locking" => undefined,
     [exec]         "default-mdb-instance-pool" => undefined,
     [exec]         "default-missing-method-permissions-deny-access" => true,
     [exec]         "default-resource-adapter-name" => "activemq-ra",
     [exec]         "default-security-domain" => "other",
     [exec]         "default-sfsb-cache" => "simple",
     [exec]         "default-sfsb-passivation-disabled-cache" => "simple",
     [exec]         "default-singleton-bean-access-timeout" => 5000L,
     [exec]         "default-slsb-instance-pool" => "slsb-strict-max-pool",
     [exec]         "default-stateful-bean-access-timeout" => 5000L,
     [exec]         "disable-default-ejb-permissions" => true,
     [exec]         "enable-statistics" => undefined,
     [exec]         "in-vm-remote-interface-invocation-pass-by-value" => true,
     [exec]         "log-system-exceptions" => true,
     [exec]         "cache" => {
     [exec]             "simple" => undefined,
     [exec]             "distributable" => undefined
     [exec]         },
     [exec]         "cluster-passivation-store" => undefined,
     [exec]         "file-passivation-store" => undefined,
     [exec]         "mdb-delivery-group" => undefined,
     [exec]         "passivation-store" => {"infinispan" => undefined},
     [exec]         "remoting-profile" => undefined,
     [exec]         "service" => {
     [exec]             "timer-service" => undefined,
     [exec]             "remote" => undefined,
     [exec]             "async" => undefined
     [exec]         },
     [exec]         "strict-max-bean-instance-pool" => {
     [exec]             "slsb-strict-max-pool" => undefined,
     [exec]             "mdb-strict-max-pool" => undefined
     [exec]         },
     [exec]         "thread-pool" => {"default" => undefined}
     [exec]     }
     [exec] }
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ RunningCLI_ViaMaven ---
[INFO] Installing /PATH/TO/Running_CLI_Using_Maven/target/RunningCLI_ViaMaven-1.0.jar to /Users/jsensharma/.m2/repository/middleware/magic/RunningCLI_ViaMaven/1.0/RunningCLI_ViaMaven-1.0.jar
[INFO] Installing /PATH/TO/Running_CLI_Using_Maven/pom.xml to /Users/jsensharma/.m2/repository/middleware/magic/RunningCLI_ViaMaven/1.0/RunningCLI_ViaMaven-1.0.pom
[INFO] 
[INFO] --- maven-antrun-plugin:1.7:run (default-cli) @ RunningCLI_ViaMaven ---
[INFO] No ant target defined - SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.096 s
[INFO] Finished at: 2015-10-20T18:02:02+05:30
[INFO] Final Memory: 13M/309M
[INFO] ------------------------------------------------------------------------

NOTE: By any chance if you get the following error message while running the above project or jboss-cli.sh script then please make sure to use JDK 1.8, Because Java 7 support has been discontinued allowing for deeper integration with the Java 8 runtime.

This application requires Java specification version 1.8 or later to run 
(this Java virtual machine implements specification version 1.7)

Source Code:
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WildFly/General/Running_CLI_Using_Maven

Regards
Jay SenSharma


Using Container interceptors for EJBs in WildFly10

Hi,

As part of this example we will be discussion about usage and functionality of the WildFly specific “Container interceptors” for our EJBs. There is another kind of interceptor known as “Java EE interceptors” which are expected to run after the container has done necessary invocation processing which involves security context propagation, transaction management and other such duties. But these these Java EE interceptors come too late into the picture.

In General, The interceptor’s are executed before the actual EJB’s method execution. When the interceptor method executes, it is passed as an InvocationContext object. This object provides information relating to the state of the interceptor and the target.

The WildFly allows us to use the Java EE interceptors (which are just POJO classes with lifecycle callback annotations) to be used as container interceptors. So that we do not need any wildfly/jboss specific dependency or libraries to use them. These container specific interceptors can be configured *ONLY* via deployment descriptors. Where as the EE Specific interceptors can be configured using @javax.interceptor.Interceptors annotation as well.

Goal of this demo

Lets start developing a simple EJB Based application which can help us in understanding what is the actual purpose of Container specific EJB Interceptors. Where exactly it can be used. In this example our interceptor does two following things:

1. Our interceptor intercepts the helloWorld(String) method parameter and then Alters the parameter value by appending some “XYZ” data to it.
2. This interceptor also calculates the overall ejb method invocation time taken to process the request.

WildFly 10 Setup:

Create a user in the “ApplicationRealm” of WildFly 10 standalone container by simply running the following command:

cd wildfly-10.0.0.CR3-SNAPSHOT/bin
./add-user.sh -a ejbuser ejbuser@1

Because of the “-a” flag the user will be created in the “ApplicationRealm” of WildFly10 with username “ejbuser” and password as “ejbuser@1”.

Developing the application:

Step-1). Create the following directory structure where we will be putting our Java and XML files. The “src/EAR_Data” directory will contain all the EJB java code and the deployment descriptors needed to build the EAR application. Similarly the “src/client” will be used to place the EJB Client artifacts.

mkdir -p EJB_Container_Interceptor_In_WildFly/src/EAR_Data 
mkdir -p EJB_Container_Interceptor_In_WildFly/src/client

Step-2). We will first see the interceptor class code “EJB_Container_Interceptor_In_WildFly/src/EAR_Data/InterceptorOne.java”, Which will use the “@javax.interceptor.AroundInvoke” annotation which should be invoked prior to the EJB method execution.

package interceptors;
public class InterceptorOne {

    @javax.interceptor.AroundInvoke
    private Object iAmAround(final javax.interceptor.InvocationContext invocationContext) throws Exception {
        String invokedMethod = invocationContext.getMethod().getName();
        System.out.println("\n\t[InterceptorOne]** iAmAround(final InvocationContext invocationContext) invoked");
        System.out.println("\t[InterceptorOne]** iAmAround, The EJB Method which will be processed is: " + invokedMethod);
        
        long startTime = System.currentTimeMillis();
        String originalParam = (String)invocationContext.getParameters()[0];
        
        // Altering the param value which was passed to the EJB method by the user While during invocation.
        String alteredParam = originalParam+"XYZ";
        invocationContext.setParameters(new String[]{alteredParam});
 
        try {
              return this.getClass().getName() + " " + invocationContext.proceed();
        } catch(Exception ex) {
            throw ex;
        }
        finally {
            long totalTime = System.currentTimeMillis() - startTime;
            System.out.println("\n\n\t[InterceptorOne]** iAmAround, Total Time taken for method execution : " + invokedMethod + " is " + totalTime + "-ms");
        }
    }
}

Step-3). We can have as many interceptors so lets create another one “EJB_Container_Interceptor_In_WildFly/src/EAR_Data/InterceptorTwo.java”.

package interceptors;
public class InterceptorTwo {
    @javax.interceptor.AroundInvoke
    private Object iAmAround(final javax.interceptor.InvocationContext invocationContext) throws Exception {
        System.out.println("\n\t[InterceptorTwo] iAmAround(final InvocationContext invocationContext) invoked");
        return this.getClass().getName() + " " + invocationContext.proceed();
    }
}

Step-4). Now lets create a Simple Stateless Bean “EJB_Container_Interceptor_In_WildFly/src/EAR_Data/CommonServiceBean.java” and it’s remote Interface “EJB_Container_Interceptor_In_WildFly/src/EAR_Data/CommonService.java” as following:

package ejb.service.one;
import javax.ejb.Local;

public interface CommonServiceOne {
     public String helloWorld(String name);
}

And

package ejb.service;
import javax.ejb.*;
import javax.naming.*;

@Stateless  
@Remote(CommonService.class)   
public class CommonServiceBean implements CommonService {
     public String helloWorld(String name) {
		   System.out.println("\n\n\t[CommonServiceBean] helloWorld(String "+name+") invoked.");
		   return "[CommonServiceBean] returned Hello "+name;
	    }
 }

Step-5). As we discussed that the container specific interceptors can be defined only via the deployable descriptors hence we will create “EJB_Container_Interceptor_In_WildFly/src/EAR_Data/jboss-ejb3.xml” file, During build it should be placed inside the “$EJB_JAR/META-INF” directory:

<jboss xmlns="http://www.jboss.com/xml/ns/javaee"
       xmlns:jee="http://java.sun.com/xml/ns/javaee"
       xmlns:ci ="urn:container-interceptors:1.0">
    <jee:assembly-descriptor>
        <ci:container-interceptors>
            <!-- Class level container-interceptor -->
            <jee:interceptor-binding>
                <ejb-name>*</ejb-name> 
                <interceptor-class>interceptors.InterceptorOne</interceptor-class>
                <interceptor-class>interceptors.InterceptorTwo</interceptor-class>
            </jee:interceptor-binding>
        </ci:container-interceptors>
    </jee:assembly-descriptor>
</jboss>

NOTE: We can also configure the “interceptor-order” in which the interceptor should be invoked. See https://docs.jboss.org/author/display/WFLY10/Container+interceptors

Step-6). Lets write the Client code which will use the “http-remoting” protocol and the http port 8080 of WildFly to invoke the EJB deployed to it. Write a client code as following: “EJB_Container_Interceptor_In_WildFly/src/client/Client.java”

package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
import ejb.service.CommonService;

public class Client {
      public static void main(String ar[]) throws Exception {
          Context context=null;
          try {
                Properties props = new Properties();
                props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
                props.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");   // NOTICE: "http-remoting" and port "8080"
                props.put(Context.SECURITY_PRINCIPAL, "ejbuser");                    // ApplicationRealm user
                props.put(Context.SECURITY_CREDENTIALS, "ejbuser@1");
                props.put("jboss.naming.client.ejb.context", true);
                context = new InitialContext(props);	
	            System.out.println("\n\tGot initial Context: "+context);		
           } catch (Exception e) {
                e.printStackTrace();
           }

            // Lookup Format will be 
            // <app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
   
          CommonService remote=(CommonService)context.lookup("testEAR/testEJB/CommonServiceBean!ejb.service.CommonService");
          System.out.println("\n\t remote.helloWorld(\"RedHat!!!\") = "+remote.helloWorld("MiddlewareMagic!!!"));
          // Notice our interceptor will change the Parameter "MiddlewareMagic!!!" to ""MiddlewareMagic!!!"XYZ"
       }
  }

Step-7). Finally lets write the ANT based “EJB_Container_Interceptor_In_WildFly/build.xml” file which will compile all the sources and pack them inside the EJB Ear and will deploy it to the WildFly10 and run the client.

<project name="EJB_Interceptor_Demo_WildFly" default="all">
<property name="jboss.home" value="/Users/jsensharma/NotBackedUp/Installed/wildfly-10.0.0.CR3-SNAPSHOT" />
<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="testEAR.ear" />
<property name="ejb.jar.name" value="testEJB.jar" />
<property name="war.name" value="LocalWebApp.war" />
<property name="client.jar.name" value="remoteEJBClient.jar" />

        <path id="jboss.classpath">
           <fileset dir="${jboss.module.dir}/system/layers/base/javax/">
               <include name="**/*.jar"/>
           </fileset>  
           <fileset dir="${tmp.dir}">
               <include name="${ejb.jar.name}" />
           </fileset> 
        </path>

        <!-- Client Needs the following Jar to be present in the CLASSPATH including -->
        <path id="jboss.new.client.classpath">
           <fileset dir="${jboss.home}/bin/client">
               <include name="jboss-client.jar" />
           </fileset>  
        </path>
	 
        <target name="all" depends="message" />

        <target name="build_ear">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}/META-INF" />
           <javac srcdir="${src.dir}/EAR_Data" destdir="${tmp.dir}"  includes="Common*.java,Interceptor*.java" classpathref="jboss.classpath"/>
           <copy file="${src.dir}/EAR_Data/CommonServiceBean.java" todir="${tmp.dir}/ejb/service"/>
           <copy file="${src.dir}/EAR_Data/CommonService.java" todir="${tmp.dir}/ejb/service"/>
           <copy file="${src.dir}/EAR_Data/CommonServiceOneBean.java" todir="${tmp.dir}/ejb/service/one"/>
           <copy file="${src.dir}/EAR_Data/CommonServiceOne.java" todir="${tmp.dir}/ejb/service/one"/>
           <copy file="${src.dir}/EAR_Data/CommonServiceTwoBean.java" todir="${tmp.dir}/ejb/service/two"/>
           <copy file="${src.dir}/EAR_Data/CommonServiceTwo.java" todir="${tmp.dir}/ejb/service/two"/>
           <copy file="${src.dir}/EAR_Data/InterceptorOne.java" todir="${tmp.dir}/interceptors"/>
           <copy file="${src.dir}/EAR_Data/InterceptorTwo.java" todir="${tmp.dir}/interceptors"/>
           <copy file="${src.dir}/EAR_Data/InterceptorThree.java" todir="${tmp.dir}/interceptors"/>

           <copy file="${src.dir}/EAR_Data/jboss-ejb3.xml" todir="${tmp.dir}/META-INF"/>
           <jar jarfile="${tmp.dir}/${ejb.jar.name}" basedir="${tmp.dir}" compress="true" />
           <delete includeEmptyDirs="true">
              <fileset dir="${tmp.dir}/META-INF"/>
              <fileset dir="${tmp.dir}/ejb"/>
              <fileset dir="${tmp.dir}/interceptors"/>
           </delete> 

           <mkdir dir="${tmp.dir}/META-INF"/>
           <copy todir="${tmp.dir}/META-INF">
                <fileset dir="${src.dir}/EAR_Data/">
                  <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.name}"/>

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


        <target name="message" depends="build_ear">
            <echo message="*******************  ******************* *********************" />  
            <copy file="${output.dir}/${ear.name}" tofile="${jboss.home}/standalone/deployments/${ear.name}"/>
            <echo message="********** ${output.dir}/${ear.name} Build and Deployed Successfully **********" />  
            <echo message="Deploy the above EARs on your desired servers using CLI script or using Manaagement Console" />  
            <echo message="*******************  ******************* *********************" />  
        </target>


        <target name="run">
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}" />
           <javac srcdir="${src.dir}/client" destdir="${tmp.dir}"  includes="CommonService.java,Client.java" classpathref="jboss.classpath"/> 
           <copy file="${src.dir}/client/CommonService.java" todir="${tmp.dir}/ejb/service"/>
           <copy file="${src.dir}/client/Client.java" todir="${tmp.dir}/client"/>        
           <jar jarfile="${output.dir}/${client.jar.name}" basedir="${tmp.dir}" compress="true" />
           <delete dir="${tmp.dir}"/>

           <java classname="client.Client" fork="true">
               <classpath>
                  <pathelement location="${output.dir}/${client.jar.name}"/>
                  <path refid="jboss.new.client.classpath"/>
               </classpath>
           </java>
        </target>        

</project>

Step-8). Lets build the project and deploy the EAR and run the client, Open a terminal and do the following:

export ANT_HOME=/PATH/TO/Support_Tools/apache_ant_1.9.2 
export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH:

cd EJB_Container_Interceptor_In_WildFly

ant

ant run

You need to just make sure that Only one line you need to change in the “build.xml” before running the ant command is to change the following line to point to your own WildFly:

<property name="jboss.home" value="/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT" />

Output on the server side after deployment and access of the EJB method:
Step-9).


15:28:42,476 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0027: Starting deployment of "testEAR.ear" (runtime-name: "testEAR.ear")
15:28:42,482 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0207: Starting subdeployment (runtime-name: "testEJB.jar")
15:28:42,495 INFO  [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0003: Processing weld deployment testEAR.ear
15:28:42,501 INFO  [org.jboss.weld.deployer] (MSC service thread 1-8) WFLYWELD0003: Processing weld deployment testEJB.jar
15:28:42,502 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-8) WFLYEJB0473: JNDI bindings for session bean named 'CommonServiceTwoBean' in deployment unit 'subdeployment "testEJB.jar" of deployment "testEAR.ear"' are as follows:

	java:global/testEAR/testEJB/CommonServiceTwoBean!ejb.service.two.CommonServiceTwo
	java:app/testEJB/CommonServiceTwoBean!ejb.service.two.CommonServiceTwo
	java:module/CommonServiceTwoBean!ejb.service.two.CommonServiceTwo
	java:jboss/exported/testEAR/testEJB/CommonServiceTwoBean!ejb.service.two.CommonServiceTwo
	java:global/testEAR/testEJB/CommonServiceTwoBean
	java:app/testEJB/CommonServiceTwoBean
	java:module/CommonServiceTwoBean

15:28:42,502 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-8) WFLYEJB0473: JNDI bindings for session bean named 'CommonServiceOneBean' in deployment unit 'subdeployment "testEJB.jar" of deployment "testEAR.ear"' are as follows:

	java:global/testEAR/testEJB/CommonServiceOneBean!ejb.service.one.CommonServiceOne
	java:app/testEJB/CommonServiceOneBean!ejb.service.one.CommonServiceOne
	java:module/CommonServiceOneBean!ejb.service.one.CommonServiceOne
	java:jboss/exported/testEAR/testEJB/CommonServiceOneBean!ejb.service.one.CommonServiceOne
	java:global/testEAR/testEJB/CommonServiceOneBean
	java:app/testEJB/CommonServiceOneBean
	java:module/CommonServiceOneBean

15:28:42,502 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-8) WFLYEJB0473: JNDI bindings for session bean named 'CommonServiceBean' in deployment unit 'subdeployment "testEJB.jar" of deployment "testEAR.ear"' are as follows:

	java:global/testEAR/testEJB/CommonServiceBean!ejb.service.CommonService
	java:app/testEJB/CommonServiceBean!ejb.service.CommonService
	java:module/CommonServiceBean!ejb.service.CommonService
	java:jboss/exported/testEAR/testEJB/CommonServiceBean!ejb.service.CommonService
	java:global/testEAR/testEJB/CommonServiceBean
	java:app/testEJB/CommonServiceBean
	java:module/CommonServiceBean

15:28:42,504 INFO  [org.jboss.weld.deployer] (MSC service thread 1-6) WFLYWELD0006: Starting Services for CDI deployment: testEAR.ear
15:28:42,505 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) WFLYWELD0009: Starting weld service for deployment testEAR.ear
15:28:42,697 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0016: Replaced deployment "testEAR.ear" with deployment "testEAR.ear"
15:28:42,698 INFO  [org.jboss.as.repository] (DeploymentScanner-threads - 2) WFLYDR0002: Content removed from location /Users/jsensharma/NotBackedUp/Installed/wildfly-10.0.0.CR3-SNAPSHOT/standalone/data/content/fb/9270aa3f5a76f881034149b2b36dc510809f34/content



15:29:58,025 INFO  [org.jboss.ejb.client] (pool-1-thread-1) JBoss EJB Client version 2.1.2.Final
15:29:58,147 INFO  [stdout] (EJB default - 1) 
15:29:58,147 INFO  [stdout] (EJB default - 1) 	[InterceptorOne]** iAmAround(final InvocationContext invocationContext) invoked
15:29:58,148 INFO  [stdout] (EJB default - 1) 	[InterceptorOne]** iAmAround, The EJB Method which will be processed is: helloWorld
15:29:58,148 INFO  [stdout] (EJB default - 1) 
15:29:58,148 INFO  [stdout] (EJB default - 1) 	[InterceptorTwo] iAmAround(final InvocationContext invocationContext) invoked
15:29:58,168 INFO  [stdout] (EJB default - 1) 
15:29:58,168 INFO  [stdout] (EJB default - 1) 
15:29:58,168 INFO  [stdout] (EJB default - 1) 	[CommonServiceBean] helloWorld(String MiddlewareMagic!!!XYZ) invoked.
15:29:58,168 INFO  [stdout] (EJB default - 1) 
15:29:58,169 INFO  [stdout] (EJB default - 1) 
15:29:58,169 INFO  [stdout] (EJB default - 1) 	[InterceptorOne]** iAmAround, Total Time taken for method execution : helloWorld is 20-ms

Source Code: https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WildFly/EJB/EJB_Container_Interceptor_In_WildFly

Regards
Jay SenSharma


Using Apache Camel to send messages to WildFly 10 default ActiveMQ broker

Hi,

As we all know that the HornetQ codebase was donated to the Apache ActiveMQ project, and the HornetQ community joined to build a next-generation messaging broker. ActiveMQ Artemis includes many new features, and also retains protocol compatibility with the HornetQ broker. WildFly 10 includes this new exciting project as its JMS broker, and due to the protocol compatility, it fully replaces the HornetQ project. So as part of this simple demo we will see how we can connect to the ActiveMQ broker present inside the WildFLy10 via Standalone Apache Camel Route.

Find more about “Apache ActiveMQ Artemis” in http://hornetq.blogspot.in/2015/06/hornetq-apache-donation-and-apache.html
AND
http://activemq.apache.org/artemis/

Lets start WildFly 10 “standalone-full.xml” profile which has the “messaging-activemq” subsystem.

WildFLy 10 side configuration

Step-1). Now create a simple JMS Queue “TestQ” by running the following CLI command:

$ cd /PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/bin

$ ./jboss-cli.sh -c 
[standalone@localhost:9990 /] /subsystem=messaging-activemq/server=default/jms-queue=TestQ/:add(entries=["java:jboss/exported/TestQ"])
{"outcome" => "success"}

Following is the XML version of the same configuration which we just did via JBoss CLI:

        <subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
            <server name="default">
                <security-setting name="#">
                    <role name="guest" delete-non-durable-queue="true" create-non-durable-queue="true" consume="true" send="true"/>
                </security-setting>
                <address-setting name="#" message-counter-history-day-limit="10" page-size-bytes="2097152" max-size-bytes="10485760" expiry-address="jms.queue.ExpiryQueue" dead-letter-address="jms.queue.DLQ"/>
                <http-connector name="http-connector" endpoint="http-acceptor" socket-binding="http"/>
                <http-connector name="http-connector-throughput" endpoint="http-acceptor-throughput" socket-binding="http">
                    <param name="batch-delay" value="50"/>
                </http-connector>
                <in-vm-connector name="in-vm" server-id="0"/>
                <http-acceptor name="http-acceptor" http-listener="default"/>
                <http-acceptor name="http-acceptor-throughput" http-listener="default">
                    <param name="batch-delay" value="50"/>
                    <param name="direct-deliver" value="false"/>
                </http-acceptor>
                <in-vm-acceptor name="in-vm" server-id="0"/>
                <jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
                <jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
                
                <!-- NOTICE BELOW -->
                <jms-queue name="TestQ" entries="java:jboss/exported/TestQ"/>
                
                <connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
                <connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
                <pooled-connection-factory name="activemq-ra" transaction="xa" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm"/>
            </server>
        </subsystem>

==============

Step-2). Now next step is to add a user “jmsuser” with password “jmsuser@1” as part of “guest” role in the “ApplicationRealm” using the “$WildFly_Home/bin/add-user.sh” script

./add-user.sh 

What type of user do you wish to add? 
 a) Management User (mgmt-users.properties) 
 b) Application User (application-users.properties)
(a): b

Enter the details of the new user to add.
Using realm 'ApplicationRealm' as discovered from the existing property files.
Username : jmsuser
Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file.
 - The password should be different from the username
 - The password should not be one of the following restricted values {root, admin, administrator}
 - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s)
Password : 
Re-enter Password : 
What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: guest
About to add user 'jmsuser' for realm 'ApplicationRealm'
Is this correct yes/no? yes
Added user 'jmsuser' to file '/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/standalone/configuration/application-users.properties'
Added user 'jmsuser' to file '/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/domain/configuration/application-users.properties'
Added user 'jmsuser' with groups guest to file '/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/standalone/configuration/application-roles.properties'
Added user 'jmsuser' with groups guest to file '/PATH/TO/wildfly-10.0.0.CR3-SNAPSHOT/domain/configuration/application-roles.properties'
Is this new user going to be used for one AS process to connect to another AS process? 
e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls.
yes/no? no

Apache Camel Route Setup

Step-3). Now we will create a very simple Apache Camel based route which will send some messages (XML files) present in the local file system to the ActiveMQ “TestQ”.

So create the following directory structure in your file system:

$ mkdir -p Camel_With_ActiveMQ_On_WildFly10/src/data
$ mkdir -p Camel_With_ActiveMQ_On_WildFly10/src/main/resources/META-INF/spring

Step-4). We will use the following kind of “Camel_With_ActiveMQ_On_WildFly10/pom.xml” file to define the necessary repositories and the camel version along with the necessary plugins.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>middleware.magic</groupId>
	<artifactId>ApacheCamel_With_ActiveMQ_WilfFly</artifactId>
	<packaging>jar</packaging>
	<version>1.0</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<camel.version>2.15.3</camel.version>
		<wildfly.jms.client.bom>10.0.0.CR2</wildfly.jms.client.bom>
	</properties>

   <repositories>
        <repository>
            <id>jboss-enterprise-techpreview-group</id>
            <name>JBoss Enterprise Technology Preview  Maven Repository Group</name>
            <url>http://maven.repository.redhat.com/techpreview/all/</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
            <layout>default</layout>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>${camel.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-jms</artifactId>
			<version>${camel.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version>${camel.version}</version>
		</dependency>
		
		<!-- logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>


		<!-- testing -->
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-test-spring</artifactId>
			<version>${camel.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.wildfly</groupId>
			<artifactId>wildfly-jms-client-bom</artifactId>
			<version>${wildfly.jms.client.bom}</version>
			<type>pom</type>
		</dependency>
	</dependencies>

	<build>
		<defaultGoal>install</defaultGoal>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

			<!-- allows the route to be ran via 'mvn camel:run' -->
			<plugin>
				<groupId>org.apache.camel</groupId>
				<artifactId>camel-maven-plugin</artifactId>
				<version>${camel.version}</version>
			</plugin>
		</plugins>
	</build>

</project>

Step-5). Now we will define the Camel Context inside the “Camel_With_ActiveMQ_On_WildFly10/src/main/resources/META-INF/spring/camel-context.xml” file. In this file we are referring to the “jmsQueueConnectionFactory” connection Factory and the “TestQ” which is present inside the Default Broker of WildFLy10:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <!-- Configuring Spring Jndi Template -->	
	<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
		<property name="environment">
			<props>
				<prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</prop>
				<prop key="java.naming.provider.url">http-remoting://localhost:8080</prop>
			</props>
		</property>
	</bean>
	
    <!-- Getting ConnectionFactory using Spring Jndi Template -->	
	<bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiTemplate">
			<ref bean="jndiTemplate" />
		</property>
		<property name="jndiName">
			<value>jms/RemoteConnectionFactory</value>
		</property>
	</bean>

    <!-- Spring based Security Part, Those are the users created in ApplicationRealm of WildFLy -->
	<bean id="authenticatedConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
		<property name="targetConnectionFactory" ref="jmsQueueConnectionFactory" />
		<property name="username" value="jmsuser" />
		<property name="password" value="jmsuser@1" />
	</bean>


	<bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
		<property name="connectionFactory" ref="authenticatedConnectionFactory" />
	</bean>

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<route>
			<from uri="file:src/data?noop=true" />  <!-- Any file placed in this dir will be send to TestQ -->
			<to uri="jms:TestQ" />
		</route>
	</camelContext>
</beans>

Step-6). Lets put some XML files like “Camel_With_ActiveMQ_On_WildFly10/src/data/message.xml” which will be sent to the ActiveMQ broker using the Camel Route. As our Apache Camel Route is using the “?noop=true” so it will keep scanning the “src/data” directory for any new file processing. So as soon as you put any file in this directory it will be processed.

<?xml version="1.0" encoding="UTF-8"?>
<company>
   <name>MiddlewareMagic</name>
   <country>India</country>
   <city>Bangalore</city>
</company>

Step-7). Open a terminal and build the above mentioned Maven project. and then run the camel context as following:

export M2_HOME=/PATH/TO/apache_maven_3.2.3

export PATH=$JAVA_HOME/bin:/PATH/TO/apache_maven_3.2.3/bin:$SOAPUI_HOME/bin:$PATH:

cd Camel_With_ActiveMQ_On_WildFly10

$ mvn clean install

$ mvn camel:run
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ApacheCamel_With_ActiveMQ_WilfFly 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> camel-maven-plugin:2.15.3:run (default-cli) > test-compile @ ApacheCamel_With_ActiveMQ_WilfFly >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ApacheCamel_With_ActiveMQ_WilfFly ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ ApacheCamel_With_ActiveMQ_WilfFly ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ApacheCamel_With_ActiveMQ_WilfFly ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /PATH/TO/Camel_With_ActiveMQ_On_WildFly10/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ ApacheCamel_With_ActiveMQ_WilfFly ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< camel-maven-plugin:2.15.3:run (default-cli) < test-compile @ ApacheCamel_With_ActiveMQ_WilfFly <<<
[INFO] 
[INFO] --- camel-maven-plugin:2.15.3:run (default-cli) @ ApacheCamel_With_ActiveMQ_WilfFly ---
[INFO] Using org.apache.camel.spring.Main to initiate a CamelContext
[INFO] Starting Camel ...
[pache.camel.spring.Main.main()] MainSupport                    INFO  Apache Camel 2.15.3 starting
[pache.camel.spring.Main.main()] xnio                           INFO  XNIO version 3.3.2.Final
[pache.camel.spring.Main.main()] nio                            INFO  XNIO NIO Implementation Version 3.3.2.Final
[pache.camel.spring.Main.main()] remoting                       INFO  JBoss Remoting version 4.0.14.Final
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.15.3 (CamelContext: camel-1) is starting
[pache.camel.spring.Main.main()] ManagedManagementStrategy      INFO  JMX is enabled
[pache.camel.spring.Main.main()] DefaultTypeConverter           INFO  Loaded 183 type converters
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
[pache.camel.spring.Main.main()] FileEndpoint                   INFO  Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
[pache.camel.spring.Main.main()] FileEndpoint                   INFO  Using default memory based idempotent repository with cache max size: 1000
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Route: route1 started and consuming from: Endpoint[file://src/data?noop=true]
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Total 1 routes, of which 1 is started.
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.15.3 (CamelContext: camel-1) started in 0.259 seconds

The “mvn camel:run” command will run the camel route and until the route is running any file which is placed inside the “Camel_With_ActiveMQ_On_WildFly10/src/data” directory will be processed by camel route (means will be send to the ActiveMQ Broker)

How to validate on WildFLy Side?

Try running the following “jboss-cli” command on the WildFly to list all the messages sent to the TestQ
List messages using CLI script:

[standalone@localhost:9990 /] /subsystem=messaging-activemq/server=default/jms-queue=TestQ:list-messages
{
    "outcome" => "success",
    "result" => [
        {
            "CamelFileLastModified" => 1393559256000L,
            "CamelFileParent" => "src/data",
            "JMSMessageID" => "ID:32212ff0-6b8b-11e5-8511-e3d2c5e4416f",
            "address" => "jms.queue.TestQ",
            "CamelFilePath" => "src/data/message1.xml",
            "JMSExpiration" => 0,
            "CamelFileLength" => 156,
            "JMSTimestamp" => 1444068129033L,
            "messageID" => 21,
            "JMSDeliveryMode" => "PERSISTENT",
            "CamelFileAbsolute" => false,
            "CamelFileName" => "message1.xml",
            "JMSPriority" => 4,
            "__AMQ_CID" => "32182f3d-6b8b-11e5-8511-e3d2c5e4416f",
            "CamelFileNameConsumed" => "message1.xml",
            "breadcrumbId" => "ID-BANL13bca644a-2-local-57971-1444068127436-0-1",
            "CamelFileRelativePath" => "message1.xml",
            "CamelFileAbsolutePath" => "/Camel_With_HornetQ/camelHornetq/src/data/message1.xml",
            "CamelFileNameOnly" => "message1.xml"
        },

SourceCode: Please find the source code of this project in MiddlewareMagic github repo:

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WildFly/JMS/Camel_With_ActiveMQ_On_WildFly10

Thanks
Jay SenSharma


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