环境:
- Eclipse Juno(4.2)
- Java 1.7.0_25
- JBoss AS 5
有关如何创建项目和配置JBOSS环境参见:http://www.micmiu.com/j2ee/ejb/eclipse-jboss-ejb-demo ,注意在创建项目时,ejb module version 选择 2.1 即可。
[一]、服务端实现
1. 编码业务接口
HelloWorldRemote.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.micmiu.ejb2x; import java.rmi.RemoteException; import javax.ejb.EJBObject; /** * 服务接口 * 所有的业务方法都要在此接口中声明 * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-30 上午11:04:03 * @version 1.0 */ public interface HelloWorldRemote extends EJBObject { String sayHello(String name) throws RemoteException; } |
2.编码Home接口
HelloWorldHome.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.micmiu.ejb2x; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; /** * Home接口必须扩展EJBHome或者EJBLocalHome接口 * Home接口暴露给客户端请求调用 * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-30 上午11:04:17 * @version 1.0 */ public interface HelloWorldHome extends EJBHome { HelloWorldRemote create() throws RemoteException, CreateException; } |
3.编码EJB业务实现的bean
HelloWorldBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.micmiu.ejb2x; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; /** * Bean类 所有具体的业务逻辑都在此类里面 * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-30 上午11:04:46 * @version 1.0 */ public class HelloWorldBean implements SessionBean { private static final long serialVersionUID = 6359187995832650345L; /** * 业务方法 * * @param name * @return * @throws RemoteException */ public String sayHello(String name) throws RemoteException { return "Hi," + name + " Welecome to EJB 2.x. "; } /** * 必须有这个方法,这是EJB的一条原则,这个方法不是来自SessionBean接口的 */ public void ejbCreate() { System.out.println("call HelloWorldBean ejbCreate()"); } @Override public void ejbActivate() throws EJBException, RemoteException { System.out.println(">>>> call HelloWorldBean ejbActivate()"); } @Override public void ejbPassivate() throws EJBException, RemoteException { System.out.println(">>>> call ejbPassivate()"); } @Override public void ejbRemove() throws EJBException, RemoteException { System.out.println(">>>> call ejbRemove()"); } @Override public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException { System.out.println(">>>> call setSessionContext()"); } } |
ps:
- 要实现所有的业务方法
- 要创建方法:public void ejbCreate()
4. 修改src META-INF/ejb-jar.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"> <display-name>EJB-demo2.x</display-name> <enterprise-beans> <session> <display-name>helloWorldEJB</display-name> <ejb-name>helloWorldEJB</ejb-name> <home>com.micmiu.ejb2x.HelloWorldHome</home> <remote>com.micmiu.ejb2x.HelloWorldRemote</remote> <ejb-class>com.micmiu.ejb2x.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar> |
5.打包发布
编译 export EJB jar包,部署到Jboss5 发布即可。
[二]、客户端开发
准备环境:
- 服务端相关的接口(不需要实现类) 打成jar包,添加到客户端环境中
- 客户端配置好 JBOSS client的相关lib包
客户端调用编码:
EJB2xClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.micmiu.ejb.demo; import java.util.Properties; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import com.micmiu.ejb2x.HelloWorldHome; import com.micmiu.ejb2x.HelloWorldRemote; /** * EJB2.x客户端调用测试 * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-30 下午3:23:55 * @version 1.0 */ public class EJB2xClientTest { /** * @param args */ public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); props.setProperty("java.naming.provider.url", "localhost:1099"); props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); // 初始化JNDI上下文环境 InitialContext ctx = new InitialContext(props); // loolup ejb 服务 Object objref = ctx.lookup("helloWorldEJB"); // 强制转换为所需类型的对象 HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objref, HelloWorldHome.class); // 通过home对象创建一个组件接口对象 HelloWorldRemote hello = home.create(); // 通过组件接口对象调用业务方法 String msg = hello.sayHello("micmiu.com"); System.out.println(msg); } } |
运行结果:
Hi,micmiu.com Welecome to EJB 2.x.
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
0 条评论。