Tag: EJB3

Writing WebLogic 12c based EJB 3.0 project and Standalone client using Maven Build

Hi,

As part of this demo we will see how to write a fully maven based WebLogic12c deployable application which will contain an EJB Project which we will deploy on WebLogic using the 12c specific maven plugin “wls-maven-plugin”. In the same sample we will use the “exec-maven-plugin” plugin as well to run our Remote Weblogic EJB Client code.

As part of this code was we will learn the following things:

1). Writing a simple EJB EAR project using Maven.
2). Deploying the EAR project on WebLogic 12c using “wls-maven-plugin”
3). Running a Simple EJB Client code with the help of “exec-maven-plugin”

So lets begin with writing the EJB Project.

Developing EJB Project

Step-1). First of all lets make a directory “WebLogic_EJB_Demo_Using_Maven” where we will keep both our “EJB_Project” and “EJB_Client_Project”.

    $ mkdir /WebLogic_EJB_Demo_Using_Maven
    $ mkdir -p /WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/src/main/application/META-INF
    $ mkdir -p /WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/src/main/java/ejb3

Step-2). Now we will write the “HelloRemote.java”, the remote interface of our Stateless EJB, Inside the “EJB_Project/TestEJB/src/main/java/ejb3”.

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

Step-3). Similarly we will write the Stateless Bean “HelloBean.java” inside the “EJB_Project/TestEJB/src/main/java/ejb3” as following

package ejb3;
import javax.ejb.*;
@Stateless
public class HelloBean implements HelloRemote {
        public String sayHello(String name) {
           System.out.println("\n\t hello("+name+") invoked, Hello HelloBean Date:  "+new java.util.Date());
           return "Hello, "+name+", Date: "+new java.util.Date();
        }
 }

Step-4). Now we will write the most important part. So lets create a maven build the file “pom.xml” inside the “/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB” directory.

<?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/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>middleware.magic</groupId>
        <artifactId>WebLogic12_EJB_Demo_With_Maven</artifactId>
        <version>1.0</version>
    </parent>

    <properties>
        <ear.app.name>TestApp</ear.app.name>
        <ejb.jar.name>TestEJB</ejb.jar.name>
    </properties>
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>middleware.magic</groupId>
    <artifactId>TestEJB</artifactId>
    <version>1.0</version>
    <packaging>ejb</packaging>


    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
        	<groupId>javax.ejb</groupId>
        	<artifactId>ejb-api</artifactId>
        	<version>3.0</version>
            <scope>provided</scope>        	
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                    <archive>
                        <manifest>
                            <useUniqueVersions>false</useUniqueVersions>
                            <addClasspath>false</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step-5). Similarly we will write another “pom.xml” inside the “/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp” 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>middleware.magic</groupId>
        <artifactId>WebLogic12_EJB_Demo_With_Maven</artifactId>
        <version>1.0</version>
    </parent>

    <properties>
        <ear.app.name>TestApp</ear.app.name>
        <ejb.jar.name>TestEJB</ejb.jar.name>
    </properties>
    
    <groupId>middleware.magic</groupId>
    <artifactId>TestApp</artifactId>
    <name>${artifactId}</name>
    <version>1.0</version>
    <packaging>ear</packaging>

    <dependencies>
        <dependency>
            <groupId>middleware.magic</groupId>
            <artifactId>TestEJB</artifactId>
            <version>1.0</version>
            <type>ejb</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <applicationName>${artifactId}</applicationName>
                    <finalName>${ear.app.name}</finalName>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                    <fileNameMapping>no-version</fileNameMapping>
                    <skinnyWars>true</skinnyWars>
                    <modules>
                        <ejbModule>
                            <groupId>middleware.magic</groupId>
                            <artifactId>${ejb.jar.name}</artifactId>
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Step-6). So far we have written the child project pom.xml, Now lets write the main “pom.xml” file inside the “/WebLogic_EJB_Demo_Using_Maven/EJB_Project” 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>middleware.magic</groupId>
    <artifactId>WebLogic12_EJB_Demo_With_Maven</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <middleware.home>/PATH/TO/Installed/wls12130</middleware.home>  <!— CHANGE ME!!! —>
   	<admin.server.url>t3://localhost:7001</admin.server.url>  <!— CHANGE ME!!! —>
        <admin.username>weblogic</admin.username>                 <!— CHANGE ME!!! —>
        <admin.password>weblogic1</admin.password>                <!— CHANGE ME!!! —>
        <ear.app.name>TestApp</ear.app.name>                      
        <ejb.jar.name>TestEJB</ejb.jar.name>                       
    </properties>
    
    <modules>
        <module>${ejb.jar.name}</module>
        <module>${ear.app.name}</module>
    </modules>
    
    
    <dependencies>
      <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  
  
    <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>2.3.2</version>
               <configuration>
                  <source>1.7</source>
                  <target>1.7</target>
               </configuration>
            </plugin>

            <plugin>
               <groupId>com.oracle.weblogic</groupId> 
               <artifactId>wls-maven-plugin</artifactId> 
               <version>12.1.3.0</version> 
               <configuration> 
                 <middlewareHome>${middleware.home}</middlewareHome>
                 <adminurl>${admin.server.url}</adminurl>
                 <user>${admin.username}</user>
                 <password>${admin.password}</password>
                 <name>${ear.app.name}</name>
                 <source>${ear.app.name}/target/${ear.app.name}.ear</source>          
               </configuration> 
            </plugin>  
         </plugins>
     </build>
     
</project>

Step-7). Now lets start the WebLogic server and then deploy the EJB project. In order to deploy the EJB Project, Open a terminal and then run the following commands. The Maven plugin will build the EAR containing the EJB as well as it will deploy it to WebLogic server.

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 /WebLogic_EJB_Demo_Using_Maven/EJB_Project
$ mvn clean install wls:deploy 

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:\WebLogic_EJB_Demo_Using_Maven\EJB_Project
$ mvn clean install wls:deploy 

In case if you face any error / exception while running the WebLogic 12c Maven plugin “wls-maven-plugin” then please refer to the article:
http://middlewaremagic.com/weblogic/?p=8217

RAW Output of EJB Project Build and Deployment

$ mvn clean install wls:deploy
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for middleware.magic:TestApp:ear:1.0
[WARNING] The expression ${artifactId} is deprecated. Please use ${project.artifactId} instead.
[WARNING] The expression ${artifactId} is deprecated. Please use ${project.artifactId} instead.
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] WebLogic12_EJB_Demo_With_Maven
[INFO] TestEJB
[INFO] TestApp
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building WebLogic12_EJB_Demo_With_Maven 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ WebLogic12_EJB_Demo_With_Maven ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ WebLogic12_EJB_Demo_With_Maven ---
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/pom.xml to /Users/jsensharma/.m2/repository/middleware/magic/WebLogic12_EJB_Demo_With_Maven/1.0/WebLogic12_EJB_Demo_With_Maven-1.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building TestEJB 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestEJB ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestEJB ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ TestEJB ---
[INFO] Compiling 2 source files to /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ TestEJB ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ TestEJB ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ TestEJB ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-ejb-plugin:2.3:ejb (default-ejb) @ TestEJB ---
[INFO] Building EJB TestEJB-1.0 with EJB version 3.0
[INFO] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/target/TestEJB-1.0.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ TestEJB ---
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/target/TestEJB-1.0.jar to /Users/jsensharma/.m2/repository/middleware/magic/TestEJB/1.0/TestEJB-1.0.jar
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestEJB/pom.xml to /Users/jsensharma/.m2/repository/middleware/magic/TestEJB/1.0/TestEJB-1.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building TestApp 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ TestApp ---
[INFO] Deleting /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target
[INFO] 
[INFO] --- maven-ear-plugin:2.9:generate-application-xml (default-generate-application-xml) @ TestApp ---
[INFO] Generating application.xml
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ TestApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/src/main/resources
[INFO] 
[INFO] --- maven-ear-plugin:2.9:ear (default-ear) @ TestApp ---
[INFO] Copying artifact [ejb:middleware.magic:TestEJB:1.0] to [TestEJB.jar]
[INFO] Building zip: /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target/TestApp-1.0/TestEJB.jar
[INFO] Copying artifact [jar:org.slf4j:slf4j-api:1.7.0] to [lib/slf4j-api.jar]
[INFO] Copy ear sources to /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target/TestApp-1.0
[INFO] Could not find manifest file: /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target/TestApp-1.0/META-INF/MANIFEST.MF - Generating one
[INFO] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target/TestApp.ear
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ TestApp ---
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target/TestApp.ear to /Users/jsensharma/.m2/repository/middleware/magic/TestApp/1.0/TestApp-1.0.ear
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/pom.xml to /Users/jsensharma/.m2/repository/middleware/magic/TestApp/1.0/TestApp-1.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building WebLogic12_EJB_Demo_With_Maven 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- wls-maven-plugin:12.1.3.0:deploy (default-cli) @ WebLogic12_EJB_Demo_With_Maven ---
[INFO] ++====================================================================++
[INFO] ++  wls-maven-plugin: deploy                                          ++
[INFO] ++====================================================================++
weblogic.Deployer invoked with options:  -noexit -adminurl t3://localhost:7001 -user weblogic -deploy -name TestApp -source TestApp/target/TestApp.ear
<Oct 29, 2015 11:02:23 PM IST> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating deploy operation for application, TestApp [archive: /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Project/TestApp/target/TestApp.ear], to configured targets.> 
Task 0 initiated: [Deployer:149026]deploy application TestApp on myserver.
Task 0 completed: [Deployer:149026]deploy application TestApp on myserver.
Target state: deploy completed on Server myserver

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] WebLogic12_EJB_Demo_With_Maven ..................... SUCCESS [  2.781 s]
[INFO] TestEJB ............................................ SUCCESS [  0.824 s]
[INFO] TestApp ............................................ SUCCESS [  0.327 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.298 s
[INFO] Finished at: 2015-10-29T23:02:24+05:30
[INFO] Final Memory: 19M/310M
[INFO] ------------------------------------------------------------------------

Writing WebLogic EJB Client Code

Step-8). Now lets start writing the client project. So first of all lets make the following directories:

$ mkdir -p /WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/src/main/java/ejb3
$ mkdir -p /WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/src/main/java/client
$ mkdir -p /WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/src/main/resources

Step-9). Now lets first place the above created “HelloRemote.java” inside the “/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/src/main/java/ejb3” directory so that the client can use it.

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

Step-10). Now the main step of writing the EJB Client. So lets create a file “WebLogicEJBClient.java” inside the “/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/src/main/java/client” as following:

package client;
import javax.naming.*;
import java.util.*;
import ejb3.HelloRemote;

public class WebLogicEJBClient {
  public static void main(String ar[]) throws Exception {
    Context context = null;
	try {
          Properties props = new Properties();
          props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          props.put(Context.PROVIDER_URL, "t3://localhost:7001"); 
          //props.put(Context.SECURITY_PRINCIPAL, "ejbUserOne");
          //props.put(Context.SECURITY_CREDENTIALS, "ejbPasswordOne@123");
          context = new InitialContext(props);	
          
          System.out.println("\n\n****************************************");
	      System.out.println("\n\t[WebLogicEJBClient] Got initial Context: "+context);	

          String EJB_JNDI_NAME="java:global/TestApp/TestEJB/HelloBean!ejb3.HelloRemote";
          
          HelloRemote remote = (HelloRemote) context.lookup(EJB_JNDI_NAME);
          String result = remote.sayHello("MiddlewareMagic!!!");
          System.out.println("\n\t[WebLogicEJBClient] Got response from EJB : " + result);
          System.out.println("\n****************************************\n\n");
          
     } catch(Exception e) {
			e.printStackTrace();
     }     
   }
}

Step-11). Now lets run the EJB 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 /WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project

$ mvn clean install exec:exec

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:\WebLogic_EJB_Demo_Using_Maven\EJB_Client_Project

$ mvn clean install exec:exec

RAW Output of Client Code Execution:


$ mvn clean install exec:exec



[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building WebLogic12_EJB_Client 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ WebLogic12_EJB_Client ---
[INFO] Deleting /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ WebLogic12_EJB_Client ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ WebLogic12_EJB_Client ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ WebLogic12_EJB_Client ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ WebLogic12_EJB_Client ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ WebLogic12_EJB_Client ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ WebLogic12_EJB_Client ---
[INFO] Building jar: /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/target/WebLogic12_EJB_Client-1.0.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ WebLogic12_EJB_Client ---
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/target/WebLogic12_EJB_Client-1.0.jar to /Users/jsensharma/.m2/repository/middleware/magic/WebLogic12_EJB_Client/1.0/WebLogic12_EJB_Client-1.0.jar
[INFO] Installing /Users/jsensharma/NotBackedUp/MM_Tests/WLS/FINAL_EJB_CAMEL/WebLogic_EJB_Demo_Using_Maven/EJB_Client_Project/pom.xml to /Users/jsensharma/.m2/repository/middleware/magic/WebLogic12_EJB_Client/1.0/WebLogic12_EJB_Client-1.0.pom
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:exec (default-cli) @ WebLogic12_EJB_Client ---


****************************************

	[WebLogicEJBClient] Got initial Context: javax.naming.InitialContext@1889f191

	[WebLogicEJBClient] Got response from EJB : Hello, MiddlewareMagic!!!, Date: Thu Oct 29 23:02:44 IST 2015

****************************************


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.231 s
[INFO] Finished at: 2015-10-29T23:02:45+05:30
[INFO] Final Memory: 14M/245M
[INFO] ------------------------------------------------------------------------

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/EJB/WebLogic_EJB_Demo_Using_Maven
.
.
Regards
Jay SenSharma


Calling EJB3 to EJB3 Locally using @EJB Annotation

Hi,

Jay SenSharma

Jay SenSharma

Here is a simple demo of calling an EJB3 Stateless bean through another EJB3 bean which is reside inside the same EAR file. To refer to a Local EJB using @EJB annotation we must use the  Tag <ejb-local-ref> inside our “ejb-jar.xml” as described in the following post.

Many times we get following kind of error at the time of Deploying the application while using @EJB annotation…

Caused By: javax.naming.NameNotFoundException: While trying to look up ejb-info

Which usually happens because our deployment descriptors  are not configured properly to lookup a Local Bean and when the WebLogic Container tries to inject the Local EJB in to another Bean/WebApplication it fails due. Example:

http://forums.oracle.com/forums/thread.jspa?threadID=2174802&tstart=0

Step1). Create a directory somewhere in your file system like “C:demosrc”

Step2). Now place the following files inside “C:demosrc” directory:

“CalculatorLocal.java”

//CalculatorRemote.java
package calculator;
import javax.ejb.*;
@Local
public interface CalculatorLocal
{
  public int add(int x,int y);
}

“CalculatorBean.java”

package calculator;
import javax.ejb.*;

@Stateless(name="CalculatorBean")
public class CalculatorBean implements CalculatorLocal
 {
   public int add(int x,int y)
    {
	  System.out.println("nt[CalculatorBean]  add() invoked.");
      return (x+y);
    }
 }
 

“CallerRemote.java”

//CalculatorRemote.java
package caller;
import javax.ejb.*;
@Remote
public interface CallerRemote
{
     public String testMethod();
   	 public String callEJBOne(int a, int b);
}
 

“CallerBean.java”

//CalculatorBean.java
package caller;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;

@Stateless(name = "CallerName", mappedName = "CallerMappedName")
public class CallerBean implements CallerRemote
 {
	 @EJB calculator.CalculatorLocal local;
     public String testMethod()
	    {
		   System.out.println("nnt Bean testMethod() called....");
		   return "DONE----returned";
	    }

	public String callEJBOne(int a, int b)
	    {
           int result=0;
		   try{
		   System.out.println("nnt Bean callEJBOne(a,b) called....");
           result=local.add(a,b);
		   }
		   catch(Exception e){ e.printStackTrace(); }
		   return "DONE----result = "+result;
	    }
 }
 

“ejb-jar.xml”

<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">

<enterprise-beans>
	<session>
		<ejb-name>CallerBean</ejb-name>
		<business-remote>caller.CallerRemote</business-remote>
		<ejb-class>caller.CallerBean</ejb-class>
		<session-type>Stateless</session-type>
		<ejb-local-ref>
		        <ejb-ref-name>ejb/CalculatorLocal</ejb-ref-name>
				<local>calculator.CalculatorLocal</local>
		</ejb-local-ref>
	</session>
</enterprise-beans>
</ejb-jar>

“application.xml”

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4">
   <display-name>EJB3_to_EJB3_Local_Injection</display-name>

   <module>
	<ejb>ejbOne.jar</ejb>
    </module>

   <module>
	<ejb>ejbTwo.jar</ejb>
    </module>

</application>

“TestClientEjb3.java”

import caller.CallerRemote;
import javax.naming.*;
import java.util.*;
public class TestClientEjb3
{
	public static void main(String[] args)  throws Exception
	{
		  String  result="";
		System.out.println("nnt Hello ...");
		   try{
                 Properties pr=new Properties();
                 pr.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
	             pr.put(Context.PROVIDER_URL,"t3://localhost:7001");

		   InitialContext ic=new InitialContext(pr);
           CallerRemote remote=(CallerRemote)ic.lookup("CallerMappedName#caller.CallerRemote");
           result=remote.callEJBOne(1000,2000);
		   }
		   catch(Exception e){ e.printStackTrace(); }
		  System.out.println("ONE----result = "+result);
	    }
}

Step3). Now place the following “build.xml” file inside “C:demo” directory:

<project name="webservices-complex" default="deploy">

<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic" />
<property name="wls.hostname" value="localhost" />
<property name="wls.port" value="7001" />
<property name="wls.server.name" value="AdminServer" />
<property name="wl.home" value="C:/bea1033/wlserver_10.3" />

<property name="src.dir" value="src" />
<property name="deploy.stuff" value="deploy" />
<property name="build.dir" value="build" />
<property name="ear.name" value="EJB3LocalLookupEAR" />
<property name="ear.dir" value="${deploy.stuff}/${ear.name}" />
<property name="ejb.jar.name.1" value="ejbOne.jar" />
<property name="ejb.jar.name.2" value="ejbTwo.jar" />
<property name="client.jar.name" value="testClient.jar" />

<path id="wl.classpath">
        <fileset dir="${wl.home}/server/lib">
              <include name="*.jar"/>
        </fileset>
        <fileset dir="${ear.dir}">
              <include name="${ejb.jar.name.1}"/>
              <include name="${ejb.jar.name.2}"/>
        </fileset>
</path>

<path id="client.classpath">
        <fileset dir="${wl.home}/server/lib">
              <include name="weblogic.jar"/>
			  <exclude name="jrmp.jar"/>
        </fileset>
        <fileset dir="${build.dir}">
              <include name="${client.jar.name}"/>
        </fileset>
</path>

<target name="init">
	<mkdir dir="${build.dir}"/>
	<mkdir dir="${ear.dir}"/>
	<mkdir dir="${ear.dir}/META-INF"/>
	<mkdir dir="${deploy.stuff}" />
</target>

<target name="compile1" depends="init">
	<javac srcdir="${src.dir}" destdir="${build.dir}"  includes="CalculatorBean.java,CalculatorLocal.java"/>
	<jar destfile="${ear.dir}/${ejb.jar.name.1}" basedir="${build.dir}" />
    <delete dir="${build.dir}" />
</target>

<target name="compile2" depends="compile1">
	<mkdir dir="${build.dir}"/>
	<mkdir dir="${build.dir}/META-INF" />
	<javac srcdir="${src.dir}" destdir="${build.dir}"  includes="CallerBean.java,CallerRemote.java" classpathref="wl.classpath"/>
	<copy file="${src.dir}/ejb-jar.xml" tofile="${build.dir}/META-INF/ejb-jar.xml"/>
	<jar destfile="${ear.dir}/${ejb.jar.name.2}" basedir="${build.dir}" />
    <copy file="${src.dir}/application.xml" tofile=	"${ear.dir}/META-INF/application.xml" />
	<delete dir="${build.dir}"/>
</target>

<target name="deploy" depends="compile2">
<wldeploy action="deploy" name="${ear.name}" source="${ear.dir}" user="${wls.username}"
password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}"/>
</target>

<target name="undeploy">
<wldeploy action="undeploy" failonerror="false" name="EJB3LocalLookupEAR" user="${wls.username}"
     password="${wls.password}" verbose="true" adminurl="t3://${wls.hostname}:${wls.port}" targets="${wls.server.name}"/>
</target>

<target name="compileClient">
	<delete dir="${build.dir}"/>
	<mkdir dir="${build.dir}"/>
	<javac srcdir="${src.dir}" destdir="${build.dir}"  includes="CallerRemote.java" classpathref="wl.classpath"/>
	<javac srcdir="${src.dir}" destdir="${build.dir}"  includes="TestClientEjb3.java" classpathref="wl.classpath"/>
	<jar destfile="${build.dir}/${client.jar.name}" basedir="${build.dir}" />
</target>

<target name="run" depends="compileClient">
    <java classname="TestClientEjb3" classpathref="client.classpath"/>
</target>

</project>

Step4). Now run the “. ./setWLSEnv.sh” to set the CLASSPATH and PATH then run the ANT script like following:

building_And_Deploying_Ejb3Local_App

building_And_Deploying_Ejb3Local_App

Step5). Now invoke the Client like following:

Running_The_Client

Running_The_Client

NOTE: Above instruction is for EJB to EJB lookup.

But if you are trying to inject an EJB  Bean inside your Servlet (WebApplication) then you must make sure that you are using the following link of “webxml”   which uses Servlet 2.5  XSD specification:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

</webapp>

.

.

Thanks

Jay SenSharma


Suspending & Resuming MDBs Using WLST

Hi,

Jay SenSharma

Jay SenSharma

Many times we want to suspend or resume the MDBs due to some production maintenance. In this case rather than using the AdminConsole we can use WLSTScript to automate the Production maintenance. The best thing to suspend the Message Processing (Message Consumption) is to untarget the MDB Application. Suppose if the MDB is targeted to WebLogic Server “TestServerOne” then using WLST script we need to just untarget the MDB Application. So in this case the Incoming Messages will be still stored in the JMS Queue but they will not be processed by the MDB because the MDBs are not targeted to any Server.

As soon as the Maintenance completes  we can again Target the MDB to the WebLogic Server so that the MDB will again start processing the JMS Messages present inside the JMS Queue. Here is a Simple Demonstration of it:

Step1). Create an MDB application as described in the following Link:   http://middlewaremagic.com/weblogic/?p=1987

Step2). Now Create a Jar file of your MDB then place it inside an EAR file with the following kind of  “MDB_Apps_Ear/META-INF/application.xml” file.

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.4">
<description>Suspending_Resuming MDB Example</description>
<display-name>Suspending_Resuming MDB Example</display-name>

<module>
<ejb>mdb30.jar</ejb>
</module>

</application>

Step3). Now Deploy the EAR application to some Server like AdminServer ..

Step4). Start Sending some Messages using the Program “QueueSend.java” in Step6)  as mentioned in the Above Link:  http://middlewaremagic.com/weblogic/?p=1987

Step5). Now write the following WLST Script somewhere in your FileSystem to Untarget your Deployed EAR application so that it will stop Consuming JMS Messages which are coming to the JMS Queue.

earAppName="MDB_Apps_Ear"
currentTargetOfEAR="AdminServer"

connect('weblogic','weblogic','t3://localhost:7001')
edit()
startEdit()
ls()
cd('/AppDeployments/' + earAppName)
ls()
cmo.removeTarget(getMBean('/Servers/' + currentTargetOfEAR))

save()
activate()

Step6). Now you can write the Following WLST Script to make your MDBs again ready to start Consuming JMS Messages from the JMS Queue by Targeting them back to the WebLogic Server back.

earAppName="MDB_Apps_Ear"
currentTargetOfEAR="AdminServer"

connect('weblogic','weblogic','t3://localhost:7001')
edit()
startEdit()
ls()
cd('/AppDeployments/' + earAppName)
ls()
cmo.addTarget(getMBean('/Servers/' + currentTargetOfEAR))

save()
activate()

NOTE:Suspending One individual MDB from an EAR file is not possible so if your EAR application Contains multiple MDBs then you will have to suspend all the MDBs present in that EAR like above.

Or Whatever MDB you think is requires regular Suspension or Resuming process make them Separate from the Original EAR.

.

.

Thanks

Jay SenSharma


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