Apache Camel based EJB Client accessing the EJBs deployed on WebLogic 12c

Datetime:2016-08-23 01:57:44          Topic: EJB           Share

Hi,

In our last demo we have seen how to develop and deploy an EJB3.0 based application on WebLogic 12c and how to access those EJBs from a standalone java code. See the following for more details on this: http://middlewaremagic.com/weblogic/?p=8283

However as part of this example we will see how ti use Apache Camel’s “org.apache.camel.component.ejb.EjbComponent” in order to access an EJB deployed on WebLogic12c. In order to see more about the Apache Camel EJB Component, which binds EJBs to Camel message exchanges, please refer to : http://camel.apache.org/ejb.html

So lets begin with writing the EJB Project.

We are assuming that you have already developed and deployed your EJB on WebLogic 12c as described in the article: http://middlewaremagic.com/weblogic/?p=8283 Now we will continue from there and will try developing a simple apache camel based client.

Developing Camel Based EJB Client

Step-1).First of all lets make a directory “Apache_Camel_As_WebLogic_EJB_Client” We will place our client artifacts there.

$ mkdir -p Apache_Camel_As_WebLogic_EJB_Client/src/main/java/ejb3
   $ mkdir -p Apache_Camel_As_WebLogic_EJB_Client/src/main/resources/META-INF/spring

Step-2).As we are assuming that you have already deployed your EJB application on WebLogic 12c. The client needs the EJB remote interface to invoke the EJBs. Hence we will use the “HelloRemote.java” EJB remote interface here. Create a file “HelloRemote.java” as following inside the “Apache_Camel_As_WebLogic_EJB_Client/src/main/java/ejb3” directory.

package ejb3;
import javax.ejb.*;
@Remote
public interface HelloRemote {
        public String sayHello(String name);
}

Step-3).Now we will create the XML based apache camel context route. In order to do that lets create a file “camel-context.xml” inside the “Apache_Camel_As_WebLogic_EJB_Client/src/main/resources/META-INF/spring” directory as following:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:camel="http://camel.apache.org/schema/spring"
	   xmlns:p="http://www.springframework.org/schema/util"
	   xsi:schemaLocation="
	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
	   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
	<p:properties id="jndiProperties">
		<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
		<prop key="java.naming.provider.url">t3://localhost:7001</prop>
		<!--
		<prop key="remote.connection.default.username">ejbuser</prop>
		<prop key="remote.connection.default.password">ejbuser@123</prop>
		-->
	</p:properties>
	<!-- setup Camel EJB component -->
	<bean id="ejbComponent" class="org.apache.camel.component.ejb.EjbComponent">
		<property name="properties" ref="jndiProperties"/>
	</bean>
	<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
	  <camel:route id="testCamelEJBClient">
			<camel:from uri="timer:foo?period=5000"/>
			<setBody>
				<simple>MiddlewareMagic!!!</simple>
			</setBody>
			<!-- NOTE: "java:global/TestApp/TestEJB/HelloBean!ejb3.HelloRemote" is the JNDI name of WLS 12c deployed EJB -->
			<camel:to uri="ejbComponent:java:global/TestApp/TestEJB/HelloBean!ejb3.HelloRemote?method=sayHello" />
			<camel:log message="[EJB Response] ===> ${body}" />
	  </camel:route>
	</camel:camelContext>
</beans>

Step-4).Just in case if we want to provide your own logging configuration to DEBUG the camel code and its execution then we can add the following kind of “log4j.properties” file inside the “Apache_Camel_As_WebLogic_EJB_Client/src/main/resources” directory.

#
# The logging properties used
#
log4j.rootLogger=INFO, out
#log4j.logger.org.apache.camel=DEBUG
log4j.logger.org.springframework=INFO

# CONSOLE appender not used by default
log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n

log4j.throwableRenderer=org.apache.log4j.EnhancedThrowableRenderer

Step-5).Now we will write the most important part which will make the whole project and will define how the execution will happen. Hence lets write the “pom.xml” file inside the “Apache_Camel_As_WebLogic_EJB_Client” location as following:

<?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>Apache_Camel_As_WebLogic12c_EJB_Client</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>Apache Camel As WebLogic 12c Based EJB Client</name>
  <url>http://middlewaremagic.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <middleware.home>/Users/jsensharma/NotBackedUp/Installed/wls12130</middleware.home> <!-- CHANGE ME !!! -->
  </properties>

  <dependencies>   
       <!-- Needed to have access to WebLogic related class access like "weblogic.jndi.WLInitialContextFactory" -->
       <dependency>
        	<groupId>weblogic</groupId>
        	<artifactId>needed.jars</artifactId>
        	<version>1.0</version>
            <scope>system</scope>   
            <systemPath>${middleware.home}/wlserver/server/lib/wlthint3client.jar</systemPath>     	
       </dependency>  
             
       <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.15.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>2.15.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>3.16</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.15</version>
             <exclusions>
                <exclusion>
                     <groupId>com.sun.jmx</groupId>
                     <artifactId>jmxri</artifactId>
                </exclusion>
                <exclusion>
                     <groupId>com.sun.jdmk</groupId>
                     <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                     <groupId>javax.jms</groupId>
                     <artifactId>jms</artifactId>
                </exclusion>
             </exclusions>
           </dependency>
           <dependency>
               <groupId>org.apache.camel</groupId>
               <artifactId>camel-ejb</artifactId>
               <version>2.15.0</version>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>6.0</version>
                <scope>provided</scope>
            </dependency>
   </dependencies>

   <pluginRepositories>
      <pluginRepository>
         <id>apache.snapshots</id>
         <name>Apache Development Snapshot Repository</name>
         <url>https://repository.apache.org/content/repositories/snapshots/</url>
         <releases>
            <enabled>false</enabled>
         </releases>
         <snapshots>
            <enabled>true</enabled>
         </snapshots>
      </pluginRepository>
   </pluginRepositories>

   <repositories>
      <repository>
         <id>apache.snapshots</id>
         <name>Apache Development Snapshot Repository</name>
         <url>https://repository.apache.org/content/repositories/snapshots/</url>
         <releases>
            <enabled>false</enabled>
         </releases>
         <snapshots>
            <enabled>true</enabled>
         </snapshots>
      </repository>
   </repositories>
   
  <build>
    <defaultGoal>install</defaultGoal>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

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

Running the Camel Based EJB Client

Step-6).Now lets run the client code as following:

For Unix Based OS

$ 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 /PATH/TO/Apache_Camel_As_WebLogic_EJB_Client

For Windows Based OS

$ set M2_HOME=C:\PATH\TO\apache_maven_3.2.3
$ set JAVA_HOME=C:\PATH\TO\jdk1.8.0_60
$ set PATH=%JAVA_HOME%/bin;C:\PATH\TO\apache_maven_3.2.3\bin;%PATH%
$ cd C:\Apache_Camel_As_WebLogic_EJB_Client

Step-7).Once the environment is set then run the Apache Camel Based ejb client as following:

$ mvn clean camel:run

..

Raw output of the client execution

$ mvn clean camel:run
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Apache Camel As WebLogic 12c Based EJB Client 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Apache_Camel_As_WebLogic12c_EJB_Client ---
[INFO] 
[INFO] >>> camel-maven-plugin:2.15.4:run (default-cli) > test-compile @ Apache_Camel_As_WebLogic12c_EJB_Client >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Apache_Camel_As_WebLogic12c_EJB_Client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5:compile (default-compile) @ Apache_Camel_As_WebLogic12c_EJB_Client ---
[INFO] Compiling 1 source file to /Apache_Camel_As_WebLogic_EJB_Client/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Apache_Camel_As_WebLogic12c_EJB_Client ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Apache_Camel_As_WebLogic_EJB_Client/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5:testCompile (default-testCompile) @ Apache_Camel_As_WebLogic12c_EJB_Client ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< camel-maven-plugin:2.15.4:run (default-cli) < test-compile @ Apache_Camel_As_WebLogic12c_EJB_Client <<<
[INFO] 
[INFO] --- camel-maven-plugin:2.15.4:run (default-cli) @ Apache_Camel_As_WebLogic12c_EJB_Client ---
[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.0 starting
[pache.camel.spring.Main.main()] ClassPathXmlApplicationContext INFO  Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32ef2304: startup date [Sun Nov 01 21:48:40 IST 2015]; root of context hierarchy
[pache.camel.spring.Main.main()] XmlBeanDefinitionReader        INFO  Loading XML bean definitions from file [/Apache_Camel_As_WebLogic_EJB_Client/target/classes/META-INF/spring/camel-context.xml]
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.15.0 (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()] SpringCamelContext             INFO  Route: testCamelEJBClient started and consuming from: Endpoint[timer://foo?period=5000]
[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.0 (CamelContext: camel-1) started in 0.751 seconds

[mel-1) thread #0 - timer://foo] testCamelEJBClient             INFO  [EJB Response] ===> [HelloBean] Says Hello, MiddlewareMagic!!!, Date: Sun Nov 01 21:48:44 IST 2015

[mel-1) thread #0 - timer://foo] testCamelEJBClient             INFO  [EJB Response] ===> [HelloBean] Says Hello, MiddlewareMagic!!!, Date: Sun Nov 01 21:48:49 IST 2015

[mel-1) thread #0 - timer://foo] testCamelEJBClient             INFO  [EJB Response] ===> [HelloBean] Says Hello, MiddlewareMagic!!!, Date: Sun Nov 01 21:48:54 IST 2015

[mel-1) thread #0 - timer://foo] testCamelEJBClient             INFO  [EJB Response] ===> [HelloBean] Says Hello, MiddlewareMagic!!!, Date: Sun Nov 01 21:48:59 IST 2015

Source Code:

The complete source code of this demo can be found at the following git repo:

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WebLogic/ApacheCamel/Apache_Camel_As_WebLogic_EJB_Client

.

.

Regards

Jay SenSharma





About List