本文介绍了从另一个 Glassfish 实例访问无状态 EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题困扰着我,我在互联网上找不到任何统一的方法来解决它.我正在使用 Java EE、Glassfish 和 Netbeans 开发企业应用程序.

there is an issue that troubles me and I cannot find any uniform way to solve it around the internet. I am developing an enterprise application using Java EE, Glassfish and Netbeans.

我设置了两个 Glassfish 实例,我正在构建一个拆分为两者的企业应用程序.我有一个运行在一个 Glassfish 实例上的网页(带有几个 JSP 和几个 HttpServlet).另一方面,我想实现应用程序的业务逻辑.也就是说,我在其中有一些 Java 实体 Bean,还有一个带有使用这些实体 Bean 的远程接口的 EJB.

I have two instances of Glassfish set up, and I am building an enterprise application split among the two. I have a web page (with a couple of JSPs and a couple of HttpServlets) running on one instance of Glassfish. On the other instance, I want to implement the business logic of the app. That is, I have some Java Entity Beans in it, and also an EJB with a remote interface that uses these entity beans.

我的目标是能够从 glassfish 的第一个实例上的 servlet 远程访问 EJB.我已按照 this 中的说明进行操作发布但未成功.我正在提供代码片段以获得更具体的...

My goal is to be able to access the EJB remotely from the servlets on the first instance of glassfish. I have followed the instructions found in this post but unsuccessfully. I am providing code snippets to get more specific...

在 Glassfish 的一个实例上,EJB 的远程接口:

On one instance of Glassfish, EJB's remote Interface:

@Remote
public interface MyEjbRemote {
     //...
}

和无状态bean:

@Stateless(name="ejb/MyEjb")
public class MyEjb implements MyEjbRemote {
     //...
}

在另一个实例上:

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        MyEjbRemote = lookupEjb();
        //...
    }

    private MyEjbRemote lookupEjb() {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial",
            "com.sun.enterprise.naming.SerialInitContextFactory");
        props.setProperty("java.naming.factory.url.pkgs",
            "com.sun.enterprise.naming");
        props.setProperty("java.naming.factory.state",
            "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

        //default: localhost
        props.setProperty("org.omg.CORBA.ORBInitialHost",
            "localhost");
        //default: 3700
        props.setProperty("org.omg.CORBA.ORBInitialPort", "2037");

        InitialContext ic = new InitialContext(props);
        return (MyEjbRemote) ic.lookup("ejb/MyEjb");
    }
}

不幸的是,当我启动我的网站并尝试查找远程 ejb 时,出现以下异常:

Unfortunately when I launch my website and try to lookup the remote ejb I get the following exception:

WARNING: Internal error flushing the buffer in release()
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.io.IOException: Stream closed
    at org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:277)
    at org.apache.jasper.runtime.JspWriterImpl.clearBuffer(JspWriterImpl.java:222)
    at org.apache.jsp.home_jsp._jspService(home_jsp.java from :76)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:406)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:662)

我的实现中缺少什么?有什么建议/想法吗?请询问我的问题描述中是否缺少某些内容,这是我第一次在此站点上发帖.感谢您的时间!

What is missing from my implementation?Any suggestions/ideas? Please ask if something is missing from the description of my problem, it's my first time posting in this site. I appreciate your time!

伊利亚斯

推荐答案

完整的 Glassfish 3.1 + Eclipse Helios + EJB 3.1 示例

简单的 Main.java(无需部署的简单客户端项目)

Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

// optional.  Defaults to localhost.  Only needed if web server is running
// on a different host than the appserver
props.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
// optional.  Defaults to 3700.  Only needed if target orb port is not 3700.
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

InitialContext ic = new InitialContext(props);
GlassfishSessionBeanRemote gRemote = (GlassfishSessionBeanRemote)ic.lookup("java:global/EJBGFish/GlassfishSessionBean!com.example.GlassfishSessionBeanRemote");
gRemote.sayHello();

EJB 模块 - 您必须将以下三个模块部署为 JAR 文件(只需在 Eclipse 中作为服务器运行)- 已部署

@Stateless
public class GlassfishSessionBean implements GlassfishSessionBeanRemote, GlassfishSessionBeanLocal {

    @Override
    public void sayHello() {
        System.out.println("First Glassfish App!");
    }

远程和本地接口如下.

@Remote
public interface GlassfishSessionBeanRemote {

    public void sayHello();
}

@Local
public interface GlassfishSessionBeanLocal {

    public void sayHello();
}

最重要的事情要做:

  1. 必须将 gf-client.jar 从 GLASSFISH 3.1 LIB 文件夹添加到 Main.java的类路径.(第一个)
  2. 将 EJB 项目添加到 Main.java 的类路径.

这篇关于从另一个 Glassfish 实例访问无状态 EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 21:59