Tag: EJB_Local_Lookup

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


WebLogic 10.3 EJB3 Local Lookup Sample

Hi,

Jay SenSharma

Jay SenSharma

Here is a very simple Step by Step instructions to develope a EJB3 Stateful Session Bean in WebLogic Server which allows us to perform a Local JNDI lookup.

Developing Local Lookup EJB3 in WebLogic 10.3 . . . .

This Sample demonstrates that how we can develop an EJB 3 Stateless Session Bean in WebLogic 10.3 by providing a custom Local JNDI Name.

Step 1). First of all we need to develop a EJB Local business interface “ReplicableSFSLocalInt.java” like following.

package sfsb3;
public interface ReplicableSFSLocalIntf
{
public void removeWithRetain() throws Exception;
public void removeWithoutRetain() throws Exception;
public String printBean ();
public int testIncrement() throws Exception;
public int testDecrement() throws Exception;
public int getCount() throws Exception;
}

Step 2). Now we are going to develop the EJB3 Stateless Session Bean class using @Stateful annotation.

package sfsb3;
import javax.ejb.*;
@Stateful(mappedName = "ejb/ReplicableSFSBean")
@Local(ReplicableSFSLocalIntf.class)
public class ReplicableSFSBean mplements ReplicableSFSLocalIntf
{
int count = 0;
public ReplicableSFSBean()
{
System.out.println("nt ReplicableSFSBean-->  ReplicableSFSBean() invoked");
}

@Init("create")
public void initMethod() throws CreateException
{
System.out.println("nt ReplicableSFSBean-->  public void initMethod() invoked");
}

@Remove(retainIfException=true)
public void removeWithRetain() throws Exception
{
System.out.println("nt ReplicableSFSBean-->  removeWithRetain() invoked");
}

@Remove
public void removeWithoutRetain() throws Exception
{
System.out.println("nt ReplicableSFSBean-->  removeWithoutRetain() invoked");
}

public String printRemoteIntf ()
{
System.out.println("nt ReplicableSFSBean --->  public String printRemoteIntf ()  invoked");
return "ReplicableSFSRemoteObjectIntf";
}

public String printLocalIntf ()
{
System.out.println("nt ReplicableSFSBean --->  public String printLocalIntf ()  invoked");
return "ReplicableSFSLocalObjectIntf";
}

public String printBean ()
{
System.out.println("nt ReplicableSFSBean --->  public String printBean ()  invoked");
return "ReplicableSFSBean";
}

public int testIncrement() throws Exception
{
System.out.println("nt ReplicableSFSBean ---> public int testIncrement()  invoked");
count=count+5;
return count;
}

public int testDecrement() throws Exception
{
System.out.println("nt ReplicableSFSBean --->  public int testDecrement()  invoked");
count=count-2;
return count;
}

public int getCount() throws Exception
{
System.out.println("nt ReplicableSFSBean --->  public int getCount()  invoked");
return count;
}
}

Step3).  We need to compile above two classes using

javac  –d  .  ReplicableSFSLocalIntf.java
javac  -d . ReplicableSFSBean.java

Step 4). We Need to Create a Jar file out of these two classes like below

jar  -cvf  Test.jar  sfsb3  *.java

Step 5). Now we need to develop a Web Application to invoke this EJB using its Local JNDI Name.

Developing “index.jsp” like following…

<html>
<head> <title>Test Local Lookup in WebLogic 10.3</title> </head>
<body>
<centre> <h2> Sample For EJB3 Local Lookup in WebLogic 10.3<br>
<h3><a href=first.jsp>Lookup for Local EJB3 Click Here</a>
</center>
</body>
</html>

Step 6). developing the “first.jsp” to write the Local Lookup code.

<%@ page import="java.util.*,javax.naming.*" %>
<%
Context env = (Context) new InitialContext();
try{
Object obj=env.lookup("java:comp/env/ejb/ReplicableSFSBean");
out.println("<BR><BR>obj=>"+obj.getClass()+"t"+obj);

sfsb3.ReplicableSFSLocalIntf  local=(sfsb3.ReplicableSFSLocalIntf )obj;
out.println("<BR><BR>local=>"+local.getClass()+"t"+local);

out.println("<BR><BR>local testIncrement() =>"+local.testIncrement());
}
catch(Exception e)
{
out.println("<BR><BR>Exception => "+e);
e.printStackTrace();
}
%>

EJB3 Stateless Session Bean invoked Successfully in WebLogic 10.3 Using Local Lookup.

Step7). Provide the “web.xml” like following:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<error-page>
<error-code>500</error-code>
<location>/error.html</location>
</error-page>

<ejb-local-ref>
<ejb-ref-name>ejb/ReplicableSFSBean</ejb-ref-name>
<local>sfsb3.ReplicableSFSLocalIntf</local>
</ejb-local-ref>

</web-app>

———————————-

Step 8). Creating an EAR file with above Web Application and Test.jar EJB, using META-INF/application.xml  file like following

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>
<application>
<display-name></display-name>
<module>
<ejb>Test.jar</ejb>
</module>
<module>
<web>
<web-uri>WebApp1</web-uri>
<context-root>WebApp1</context-root>
</web>
</module>
</application>

Step 9). Now deploy the EAR File on WebLogic Server and access the WebApp1 index.jsp page.

.

==============================================================
But if you are trying to inject an EJB  Bean inside your Servlet (WebApplication) using @EJB annotation 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>

.

Regards
Jay SenSharma


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