首页 > 代码库 > EJB远程接口调用
EJB远程接口调用
一.EJB简介
EJB是sun的JavaEE服务器端组件模型,设计目标与核心应用是部署分布式应用程序。简单来说就是把已经编写好的程序(即:类)打包放在服务器上执行。凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台。EJB (Enterprise JavaBean)是J2EE(javaEE)的一部分,定义了一个用于开发基于组件的企业多重应用程序的标准。其特点包括网络服务支持和核心开发工具(SDK)。 在J2EE里,Enterprise Java Beans(EJB)称为Java 企业Bean,是Java的核心代码,分别是会话Bean(Session Bean),实体Bean(Entity Bean)和消息驱动Bean(MessageDriven Bean)
二.EJB与DUBBO的区别
Dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架。(必须依赖于Spring框架,必须启动Zookeeper服务,用于注册接口),与EJB一样,都可以实现接口与实现类的远程类调用,实现接口,可以将核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应。
三.EJB在同项目中的应用(只有一个实现类)
1、定义接口(加@Local注解,代表本地接口)
@Local public interface Iejb { String sayHelloWord(String userName); }
2、编写实现类(加注解@Stateless,无状态bean)
@Stateless @Local public class EjbBean implements Iejb { @Override public String sayHelloWord(String userName) { return userName+":HelloWord!"; } }
3.客户端调用
@Stateless public class HelloWorldLocalController extends HttpServlet { @EJB private Iejb iejb; @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); String s = JSON.toJSONString(helloWorld.sayHello("wangping")); pw.write(s); pw.close(); } }
四.EJB在同项目中的应用(一个接口对应多个实现类)
1、定义接口(加@Local注解,代表本地接口)
@Local public interface Iejb2 { String sayHelloWord(String userName); }
2、编写实现类
1(加注解@Alternative,代表取消自动注入)
@Stateless @Alternative public class IejbBean implements Iejb2 { @Override public String sayHello(String userName) { return userName+":Hello"; } }
2(加注解@Alternative,代表取消自动注入)
@Stateless @Alternative public class NewIejbBean implements Iejb2 { @Override public String sayHello(String userName) { return userName+":HelloWorld"; } }
3 编写bean.xml指定接口的实现类(指定实现类为NewIejbBean)
<?xml version="1.0" encoding="UTF-8"?> <beans 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"> <alternatives> <class>com.phhc.ejb_eg2.NewIejbBean</class> </alternatives> </beans>
3、编写客户端(加注解@Inject表示注入)
public class DemoServlet2 extends HttpServlet { @Inject private Iejb2 iejb; @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) { try { response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); String s = JSON.toJSONString(iejb.sayHello("wangping")); pw.write(s); pw.close(); }catch (Exception e){ e.printStackTrace(); } } }
五.EJB在同类容器不同项目中的远程调用
1.编写接口(加@Remote,代表远程调用接口)
@Remote public interface HelloWordRemote extends Serializable { Model sayHello(String world); }
2、编写实现类(加@Stateless表示无状态的bean)
@Stateless(name = "HelloWorldBeanEJB") @Remote(HelloWordRemote.class) @Local(HelloWordLocal.class) public class HelloWorldBean implements HelloWordRemote,HelloWordLocal { @Override public String sayHello(String world) { return "HelloWorld:"+world; } }
3、在项目根目录下创建META-INF目录,下面添加ejb-jar.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <ejb-jar 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_1.xsd" version="3.1"> <enterprise-beans> <session> <ejb-name>HelloWorldBeanEJB</ejb-name> <remote>com.phhc.ejb.servcices.HelloWordRemote</remote> <local>com.phhc.ejb.servcices.HelloWordLocal</local> <ejb-class>com.phhc.ejb.servcices.impl.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>
4、创建客户端程序(加EJB注解,表示EJB远程调用)
public class HelloWordController extends HttpServlet { @EJB(name = "ejb/HelloWorldBeanEJB") private HelloWordRemote helloWord;
5、部署EJB程序和客户端程序,访问客户端程序
六.不同Glassfish之间的EJB远程调用
1、编写接口项目
@Remote public interface HelloWordRemote extends Serializable { Model sayHello(String world); }
2、编写实现类
@Stateless(name = "HelloWorldBeanEJB") public class HelloWorldBean implements HelloWordRemote { public HelloWorldBean() { } @Override public Model sayHello(String world) { Model model = new Model(); model.setName(world); model.setSex("男"); model.setHobby("女"); return model; } }
3、编写配置
<?xml version="1.0" encoding="UTF-8" ?> <ejb-jar 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_1.xsd" version="3.1"> <enterprise-beans> <session> <ejb-name>HelloWorldBeanEJB</ejb-name> <remote>com.phhc.ejb.servcices.HelloWordRemote</remote> <ejb-class>com.phhc.ejb.servcices.impl.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> </ejb-jar>
4、编写客户端
public class HelloWordController extends HttpServlet { @EJB(name = "ejb/HelloWorldBeanEJB") private HelloWordRemote helloWord; @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-type", "text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); String s = JSON.toJSONString(helloWord.sayHello("wanping")); pw.write(s); pw.close(); } }
5、客户端配置(jndi)
?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-web-app PUBLIC ‘-//Sun Microsystems, Inc.//DTD Sun ONE Application Server 7.0 Servlet 2.3//EN‘ ‘http://www.sun.com/software/sunone/appserver/dtds/sun-web-app_2_3-0.dtd‘> <sun-web-app> <ejb-ref> <ejb-ref-name>com.phhc.controller.HelloWordController/helloWorld</ejb-ref-name> </ejb-ref> <jsp-config/> </sun-web-app>
6、结构目录
EJB远程接口调用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。