各个容器开发EJB不太一样,这里使用的是Weblogic8.1和EJB2.0进行演示的。
1 下载java1.4的EE包
2 设置环境变量
3 设置Weblogic的配置
4 添加weblogic的javaee.jar和weblogic.jar到CLASSPATH 在bea/weblogic81/server/lib中
下面的演示使用手工,不使用Eclipse
1 编写Bean
<textarea cols="50" rows="15" name="code" class="c-sharp">/** * */ package com; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloWorldBean implements SessionBean { public void setSessionContext(SessionContext ctx) { System.out.println("set session context"); } public void ejbCreate() { System.out.println("ejb create"); } public void ejbRemove() { System.out.println("ejb remove"); } public void ejbActivate() { System.out.println("ejb activate"); } public void ejbPassivate() { System.out.println("ejb passivate"); } public String sayHello(String name) throws RemoteException { return "Hello :" + name; } } </textarea>
2 实现接口
<textarea cols="50" rows="15" name="code" class="c-sharp">/** * */ package com; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface HelloWorldHome extends EJBHome { HelloWorldRemote create() throws RemoteException, CreateException; } </textarea>
<textarea cols="50" rows="15" name="code" class="c-sharp">/** * */ package com; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloWorldBean implements SessionBean { public void setSessionContext(SessionContext ctx) { System.out.println("set session context"); } public void ejbCreate() { System.out.println("ejb create"); } public void ejbRemove() { System.out.println("ejb remove"); } public void ejbActivate() { System.out.println("ejb activate"); } public void ejbPassivate() { System.out.println("ejb passivate"); } public String sayHello(String name) throws RemoteException { return "Hello :" + name; } } </textarea>
3 创建部署描述符ejb-jar.xml 在META-INF文件夹下
<textarea cols="50" rows="15" name="code" class="c-sharp"><?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> <display-name>TestEjb</display-name> <enterprise-beans> <session> <display-name>helloEJB</display-name> <ejb-name>helloEJB</ejb-name> <home>com.HelloWorldHome</home> <remote>com.HelloWorldRemote</remote> <ejb-class>com.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar> </textarea>
4 创建weblogic的部署weblogic-ejb-jar.xml
<textarea cols="50" rows="15" name="code" class="c-sharp"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN" "http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd"> <weblogic-ejb-jar> <description></description> <weblogic-enterprise-bean> <ejb-name>helloEJB</ejb-name> <jndi-name>ejb/hello</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> </textarea>
5 编译上面的文件
javac
HelloWorldBean.class
HelloWorldRemote.class
HelloWorldHome.class
6 使用jar进行打包
jar -cvf TestEjb.jar *.*
7 使用weblogic的命令
java weblogic.appc TestEjb.jar
客户端访问可以去除HelloWorldBean.class
8 部署
8.1 启动weblogic服务,使用console进行部署。
8.2 或直接复制到应用程序目录,比如C:/bea/user_projects/domains/mydomain/applications也可以。
9 编写客户端
<textarea cols="50" rows="15" name="code" class="c-sharp">import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import com.HelloWorldHome; import com.HelloWorldRemote; /* * Created on 2010-11-12 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ /** * @author Administrator * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class TestEjb { public static void main(String[] args) { try { System.out.println("begin ctx"); //得到与远程通信的连接 Hashtable cs = new Hashtable(); cs.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); cs.put(Context.PROVIDER_URL, "t3://rollin:7001"); Context ctx = new InitialContext(cs); System.out.println("----------------------"); HelloWorldHome objRef = (HelloWorldHome) ctx.lookup("ejb/hello"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objRef, HelloWorldHome.class); HelloWorldRemote remote = home.create(); System.out.println(remote.sayHello("mrl")); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.toString()); } } } </textarea>
10 javac Test.java
11 java Test
<textarea cols="50" rows="15" name="code" class="c-sharp">C:/Documents and Settings/Administrator/workspace/TestEjb/ejbModule>java TestEjb begin ctx ---------------------- Hello :mrl </textarea>
<textarea cols="50" rows="15" name="code" class="c-sharp"><2010-11-12 下午14时54分11秒 CST> <Notice> <WebLogicServer> <BEA-000355> <Thread "ListenThread.Default" listening on port 7001, ip address *.*> set session context ejb create</textarea>
12 在容器内访问:该web的lib有EJBClient.jar(TestEjb.jar)文件即可。
<textarea cols="50" rows="15" name="code" class="c-sharp"><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <%@ page import="java.sql.*"%> <%@ page import="java.util.*"%> <%@ page import="javax.naming.*"%> <%@ page import="javax.rmi.PortableRemoteObject"%> <%@ page import="com.HelloWorldHome"%> <%@ page import="com.HelloWorldRemote"%> <% String result = ""; try { System.out.println("begin context"); //得到与远程通信的连接 Hashtable cs = new Hashtable(); cs.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); cs.put(Context.PROVIDER_URL, "t3://rollin:7001"); Context ctx = new InitialContext(cs); System.out.println("----------------------"); HelloWorldHome objRef = (HelloWorldHome) ctx.lookup("ejb/hello"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objRef, HelloWorldHome.class); HelloWorldRemote remote = home.create(); result = remote.sayHello("mrl2"); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.toString()); } finally { out.println("end"); } %> <%=result%> </body> </html></textarea>
访问该页面2次的情况
end Hello :mrl2
end Hello :mrl2
<textarea cols="50" rows="15" name="code" class="c-sharp">begin context ---------------------- begin context ----------------------</textarea>
容器内其实容器可以发现EJB对象
<textarea cols="50" rows="15" name="code" class="c-sharp"><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <%@ page import="java.sql.*"%> <%@ page import="java.util.*"%> <%@ page import="javax.naming.*"%> <%@ page import="javax.rmi.PortableRemoteObject"%> <%@ page import="com.HelloWorldHome"%> <%@ page import="com.HelloWorldRemote"%> <% String result = ""; try { System.out.println("begin context ag"); //得到与远程通信的连接 Context ctx = new InitialContext(); System.out.println("----------------------"); HelloWorldHome objRef = (HelloWorldHome) ctx.lookup("ejb/hello"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objRef, HelloWorldHome.class); HelloWorldRemote remote = home.create(); result = remote.sayHello("mrl3"); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.toString()); } finally { out.println("end"); } %> <%=result%> </body> </html></textarea>
后台
<textarea cols="50" rows="15" name="code" class="c-sharp">begin context ---------------------- set session context ejb create begin context ag ----------------------</textarea>