首页 > 代码库 > EJB远程接口调用

EJB远程接口调用

一.EJB简介

 

EJBsunJavaEE服务器端组件模型,设计目标与核心应用是部署分布式应用程序。简单来说就是把已经编写好的程序(即:类)打包放在服务器上执行。凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台。EJB (Enterprise JavaBean)J2EE(javaEE)的一部分,定义了一个用于开发基于组件的企业多重应用程序的标准。其特点包括网络服务支持和核心开发工具(SDK)。 在J2EE里,Enterprise Java Beans(EJB)称为Java 企业Bean,是Java的核心代码,分别是会话BeanSession Bean),实体BeanEntity Bean)和消息驱动BeanMessageDriven Bean

 

二.EJBDUBBO的区别

 

Dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架。(必须依赖于Spring框架,必须启动Zookeeper服务,用于注册接口),EJB一样,都可以实现接口与实现类的远程类调用,实现接口,可以将核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应。

 

三.EJB在同项目中的应用(只有一个实现类)

 

1、定义接口(@Local注解,代表本地接口)

 

技术分享
@Local
public interface Iejb {
    String sayHelloWord(String userName);
}
View Code

 

2、编写实现类(加注解@Stateless,无状态bean

 

技术分享
@Stateless
@Local
public class EjbBean implements Iejb {

    @Override
    public String sayHelloWord(String userName) {
        return userName+":HelloWord!";
    }
}
View Code

 

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();
    }

}
View Code

 

四.EJB在同项目中的应用(一个接口对应多个实现类)

 

1、定义接口(@Local注解,代表本地接口)

 

技术分享
@Local
public interface Iejb2 {
    String sayHelloWord(String userName);
}
View Code

 

2、编写实现类

  1加注解@Alternative,代表取消自动注入

技术分享
@Stateless
@Alternative
public class IejbBean implements Iejb2 {
    @Override
    public String sayHello(String userName) {
        return userName+":Hello";
    }
}
View Code

  2加注解@Alternative,代表取消自动注入

 

 

技术分享
@Stateless
@Alternative
public class NewIejbBean implements Iejb2 {

    @Override
    public String sayHello(String userName) {
        return userName+":HelloWorld";
    }
}
View Code

 

  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>
View Code

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();
        }
    }
}
View Code

五.EJB在同类容器不同项目中的远程调用

 

1.编写接口(@Remote,代表远程调用接口)

技术分享
@Remote
public interface HelloWordRemote extends Serializable {
    Model sayHello(String world);
}
View Code

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;
    }
}
View Code

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>
View Code

 

4、创建客户端程序(EJB注解,表示EJB远程调用)

 

技术分享
public class HelloWordController extends HttpServlet {
    @EJB(name = "ejb/HelloWorldBeanEJB")
    private HelloWordRemote helloWord;
View Code

 

5、部署EJB程序和客户端程序,访问客户端程序

六.不同Glassfish之间的EJB远程调用

 

1、编写接口项目

技术分享
@Remote
public interface HelloWordRemote extends Serializable {
    Model sayHello(String world);
}
View Code

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;
    }
}
View Code

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>
View Code

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();
    }
}
View Code

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>
View Code

 

6、结构目录

技术分享

 

技术分享

技术分享

技术分享

 

EJB远程接口调用