Hi All,

Jay SenSharma

Jay SenSharma

I have observed that many Oracle WebLogic Application Server Developers and Administrators face an issue when they deploy their EJB3.0 based applications in WebLogic container, The WebLogic Generates a JNDI name in following format “mappedName#packageName/remoteInterfaceName”. Many Users wants to use their own Custom JNDI (User Defined JNDI)name rather than above kind of Complex JNDI name.
So here is a Simple EJB3.0 Example which describes us how to define our own Custom JNDI name for an EJB component in WebLogic Server.

Step1). First of all Create a Simple Business Interface “ReplicableSFSRemoteIntf.java”


package sfsb3;
import javax.ejb.*;

@Remote

public interface ReplicableSFSRemoteIntf {
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;
}

Step2). Now we need to Create the Bean class “ReplicableSFSBean.java” like following


package sfsb3;
import javax.ejb.*;
@Stateful
public class ReplicableSFSBean implements ReplicableSFSRemoteIntf
{
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). Now compile above classes using simple “javac”

Step 4). Now we need to provide our own “ejb-jar.xml” file like following (We need to provide our own Deployment Descriptors to customize the JNDI name because there is no J2EE specific or WebLogic Specific annotation available at this point to define it with customization).


<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>ReplicableSFSB</ejb-name>
<business-local>sfsb3.ReplicableSFSLocalIntf</business-local>
<business-remote>sfsb3.ReplicableSFSRemoteIntf</business-remote>
<ejb-class>sfsb3.ReplicableSFSBean</ejb-class>
<session-type>Stateful</session-type>
</session>
</enterprise-beans>
</ejb-jar>

Step5). Now we need to provide the “weblogic-ejb-jar.xml” file here we are going to define our own User Defined Custom JNDI name.


<weblogic-ejb-jar xmlns="http://www.bea.com/ns/weblogic/90" xmlns:j2ee=" http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/90  http://www.bea.com/ns/weblogic/90/weblogic-ejb-jar.xsd">

<weblogic-enterprise-bean>
<ejb-name>ReplicableSFSB</ejb-name>
<stateful-session-descriptor>
     <business-interface-jndi-name-map>
            <business-remote>sfsb3.ReplicableSFSRemoteIntf </business-remote>
            <jndi-name>ReplicableSFSB</jndi-name>
      </business-interface-jndi-name-map>
</em> </stateful-session-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>

Step6). Now place above Deployment descriptors in META-INF directory and create a Jar of it and then Deploy it on WebLogic Server.

Step 7). To test the Custom JNDI name in Oracle WebLogic Server, we need to write a Simple Client Application so that we can perform a JNDI lookup in WebLogic Server JNDI tree for the Business component.


package sfsb3;
import javax.naming.*;
import java.util.*;
public class Ejb3Client
{
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
private static String serverUrl ="t3://localhost:7001";
public static void main(String[] ar) throws Exception
{
InitialContext ic=null;
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, serverUrl);
ic = new InitialContext(env);
}
catch(Exception e){
System.out.println("nt Didnot get InitialContext: "+e);
}
try{
Object obj=ic.lookup("ReplicableSFSB");
sfsb3.ReplicableSFSRemoteIntf home=(sfsb3.ReplicableSFSRemoteIntf)obj;
System.out.println("nntRemote=>"+home.getClass());
System.out.println("nntRemote testIncrement() =>"+home.testIncrement());
}
catch(Exception e)
{
System.out.println("nnt jack Exception => "+e);
e.printStackTrace();
}
}
}

Regards
Jay SenSharma