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

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