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:
Steps:
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-------------
TestEJB3
--src
--bean
--TestBean.java
--TestBeanHome.java
--TestBeanRemote.java
--META-INF
--ejb-jar.xml
--MANIFEST.MF
--weblogic-ejb-jar.xml
--------------Java Project for Client side-------------
TestEJB3Client
--src
--TestEJBClient.java
5) Below are the code for above mentioned files for Server Side:
//TestBeanRemote.java
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;
}
----------------------------------------------------
//TestBeanHome.java
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;
}
----------------------------------------------------
//TestBean.java
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+"";
}
}
----------------------------------------------------
//ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>TestEJB3</ejb-name>
<home>bean.TestBeanHome</home>
<remote>bean.TestBeanRemote</remote>
<ejb-class>bean.TestBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>TestEJB3</ejb-name>
<method-intf>Remote</method-intf>
<method-name>doSomething</method-name>
</method>
<method>
<ejb-name>TestEJB3</ejb-name>
<method-intf>Remote</method-intf>
<method-name>calculateSum</method-name>
</method>
<trans-attribute>NotSupported</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
----------------------------------------------------
//weblogic-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd" >
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>TestEJB3</ejb-name>
<stateless-session-descriptor>
<pool>
<max-beans-in-free-pool>200</max-beans-in-free-pool>
<initial-beans-in-free-pool>20</initial-beans-in-free-pool>
</pool>
<stateless-clustering>
<home-load-algorithm>RoundRobin</home-load-algorithm>
<stateless-bean-is-clusterable>true</stateless-bean-is-clusterable>
</stateless-clustering>
</stateless-session-descriptor>
<transaction-descriptor>
<trans-timeout-seconds>120</trans-timeout-seconds>
</transaction-descriptor>
<enable-call-by-reference>true</enable-call-by-reference>
<jndi-name>TestJNDI</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
----------------------------------------------------
//MANIFEST.MF (No change is required).
Manifest-Version: 1.0
Class-Path:
----------------------------------------------------
6) Below is the code for Client side only:
//TestEJBClient.java
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.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
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.doSomething();
bean.calculateSum(100, 160, 190);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
----------------------------------------------------
7) Once the coding is done, we have to deploy the EJB in Server and test it via Client.
For deploying, Left click on "TestBean.java" 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 EJBClient.java.
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.
Friday, February 3, 2012
EJB 2.x with MyEclipse and Weblogic 9.x
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment