Friday, February 3, 2012

EJB 2.x with MyEclipse and Weblogic 9.x

EJB 2.x with MyEclipse and Weblogic 9.x

Dear reader,
Here is the basic example and how to run the EJB using Client, tested on the above mentioned platform:

1) Create Weblogic domain using "Configuration Wizard" under "Tool" in Bea/Weblogic.
2) Create a "File-->New-->EJB Project" in Myeclipse. 
3) We need two projects: "EJB Project" for Server side and "Java Project" for client side.
4) Folder structure is as below, excluding other Supported Runtime libraries and Classpaths:
--------------EJB Project for Server side-------------


--------------Java Project for Client side-------------

5) Below are the code for above mentioned files for Server Side:
package bean;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface TestBeanRemote extends EJBObject {
    public void doSomething() throws RemoteException;
    public String calculateSum(int num1, int num2, int num3) throws RemoteException;
package bean;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface TestBeanHome extends EJBHome{
    public TestBeanRemote create() throws CreateException, RemoteException;    
package bean;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class TestBean implements SessionBean {
    private SessionContext ctx;
    public void setSessionContext(SessionContext c) {
        ctx = c;
        System.out.println(" \n In setSessionContext() \n");
    public void ejbCreate() {
        System.out.println("In ejbCreate().");
    public void ejbRemove() {
        System.out.println("In ejbRemove().");
    public void ejbActivate() {
        System.out.println("In ejbActivate().");
    public void ejbPassivate() {
        System.out.println("In ejbPassivate().");
    public void doSomething(){
        System.out.println("Hello deepak from TestBean");
    public String calculateSum(int num1, int num2, int num3){
        int sum=num1+num2+num3;
        System.out.println("In calculateSum(): Sum="+sum);
        return sum+"";
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' ''>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "" >
//MANIFEST.MF (No change is required).
Manifest-Version: 1.0

6) Below is the code for Client side only:
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import bean.TestBeanHome;
import bean.TestBeanRemote;

public class TestEJBClient {
    public static void main(String[] args) throws Exception{
        try {
            Hashtable<String, String> ht_env = new Hashtable<String, String>();
            ht_env.put(Context.PROVIDER_URL, "t3://localhost:7001");
            InitialContext ctx = new InitialContext(ht_env);
            Object o = ctx.lookup("TestJNDI");
            TestBeanHome home = (TestBeanHome) PortableRemoteObject.narrow(o,TestBeanHome.class);
            TestBeanRemote bean = home.create();
            bean.calculateSum(100, 160, 190);
        } catch (NamingException e) {
7) Once the coding is done, we have to deploy the EJB in Server and test it via Client.
   For deploying, Left click on "" in your "EJB Project" in Myeclipse and click on 
   "Deploy MyEclipse J2EE Project to Server". This is a "Clickable Button" given on LEFT side of Myeclipse UI, before "Start Server" option.
   There you can "Remove/Redeploy/Edit" as well. Once it is deployed, done.

8) Now you have to execute the Java Client code to get the output from EJB, Simply run the
   This is a working example, so it should work, However if you are getting any "NameNotFound" kind of JNDI exception,
   Better check the Weblogic Console "Server-> AdminConsole->JNDI Tree". Your JNDI should appear.
   Hope you got this well and first step in EJB is completed well.

No comments:

Post a Comment