在从Servlet调用的会话Bean中,SessionContext始终为null



我的工作是使用Servlet编写一种Web服务适配器,然后Servlet调用适配器EJB(SOAP Web服务),EJB又调用现有的服务方法(也是EJB)。现有的体系结构是基于EJB 2.0的,我现在不能更改它。外部客户应该访问Servlet,而不是直接访问webservice类。原因是需要使用HTTPRequest中的信息进行一些预处理(从证书获取和映射用户id,对应于HTTP头)。因此,Servlet对doPost()方法做出反应,执行预处理,如解析和编组SOAP XML数据,然后调用适配器EJB(无状态会话Bean),更确切地说,Web服务方法将触发不同EJB中的现有服务方法。到目前为止,这种方法运行良好,直到EJB需要一个现有的会话响应。SessionContext,类似于以下处理事务回滚的情况:

protected void preventTransactionRolledBackException() {
    if (this.getSessionContext().getRollbackOnly()) {
        this.getSessionContext().setRollbackOnly();
    }
}

在我的设置中,SessionContext始终为null。由于整个应用程序已经相当复杂,我尝试只使用Servlet和一个EJB来发布一个简化的设置,希望我不会排除一些相关的东西。系统:WebSphere Application Server 8.5

Servlet:

public class NewZekEclsServiceServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = null;
        response.setContentType("text/xml");
        try { 
            PrintWriter out = response.getWriter(); 
            // Get Header data
            // Get Attribute data
            // Get Body data:
            InputStream body = request.getInputStream();
            String xml = IOUtils.toString(body, "UTF-8");
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setNamespaceAware(true); // required as several namespaces might be used
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
            operation = getOperation(doc); // method which scans the xml to retrieve the correct operation to use.
            if (operation.equalsIgnoreCase("Ping")) {
                // Calling the Adapter Bean:
                EclsTestBeanBean eclsTestBean = new EclsTestBeanBean();
                eclsTestBean.ejbCreate();
                PingIn pingIn = new PingIn();
                PingOut pingOut = eclsTestBean.ping(pingIn);
                String xmlString = convertEclsObjectToSOAPString(pingOut);
                out.print(xmlString);
            }
        }
    }
}

EJB(由Rational Application Developer 9.0生成):

/**
 * Bean implementation class for Session Bean: EclsTestBean
 *
 * @ejb.bean
 *  name="EclsTestBean"
 *  type="Stateless"
 *  jndi-name="ejb/ch/zek/ecls/EclsTestBeanHome"
 *  view-type="remote"
 *  transaction-type="Bean"
 *
 * @ejb.home
 *  remote-class="ch.zek.ecls.EclsTestBeanHome"
 *
 * @ejb.interface
 *  remote-class="ch.zek.ecls.EclsTestBean"
 *
 */
public class EclsTestBeanBean implements javax.ejb.SessionBean {
    private Log log = LogFactory.getLog(EcodeAbfragenAction.class);
    private SessionContext mySessionCtx;
    public SessionContext getSessionContext() {
        return mySessionCtx;
    }
    public void setSessionContext(SessionContext ctx) {
        mySessionCtx = ctx;
    }
    public void ejbCreate() throws CreateException {}
    public void ejbActivate() {}
    public void ejbPassivate() {}
    public void ejbRemove() {}
    /**
     * Basic ping service for ECLS
     * @param parameters
     * @return
     * @throws PingEntityNotFoundException
     * @throws PingPermissionException
     * @throws PingSystemException
     */
    public ch.zek.ecls.PingOut ping(ch.zek.ecls.PingIn parameters) throws java.rmi.RemoteException, ch.zek.ecls.PingPermissionException, ch.zek.ecls.PingEntityNotFoundException, ch.zek.ecls.PingSystemException {
        PingOut pingOut = new PingOut();
        String pingAnswer = "Ping_ECLS_v1";
        String adapter = "";
        String operation = "";
        Parameter[] msgParams = new Parameter[1];
        String eclsEnvironment = "TEST";
        pingAnswer += "_" + eclsEnvironment;
        logAvailableEjbs();
        try {
            if (mySessionCtx != null) { // Why can it be null at all?
                log.debug("mySessionCtx: " + mySessionCtx.getContextData());
            } else {
                log.debug("mySessionCtx was null");
                InitialContext ic = new InitialContext();
                mySessionCtx = (SessionContext) ic.lookup("java:comp/env/sessionContext"); // gives error:  javax.naming.NameNotFoundException: Name "comp/env/sessionContext" not found in context "java:".
                System.out.println("mySessionCtx: " + mySessionCtx);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // creating the SOAP data... not relevant for the problem.
        msgParams[0] = new Parameter();
        msgParams[0].setValue(pingAnswer);
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setCode("OK");
        systemMessage.setMessage("Ping");
        systemMessage.setParameter(msgParams);
        pingOut.setSystemMessage(systemMessage);
        return pingOut;
    }
    /**
     * ONLY USED DURING DEVELOPMENT:
     * Helper method to print "accessible" EJBs.
     */
    protected void logAvailableEjbs() {
        try {
            //Get the Initial Context for the JNDI lookup for a local EJB
            InitialContext ic = new InitialContext();
            NamingEnumeration<NameClassPair> list;
            String level = "";
            String name = "";
            list = ic.list(level);
            while (list.hasMore()) {
                name = list.next().getName();
                System.out.println(level + "/" + name);
            }
            level = "ejb";
            list = ic.list(level);
            while (list.hasMore()) {
                name = list.next().getName();
                System.out.println(level + "/" + name);
            }   
            level = "java:comp";
            list = ic.list(level);
            while (list.hasMore()) {
                name = list.next().getName();
                System.out.println(level + "/" + name);
            }
            level = "java:comp/env";
            list = ic.list(level);
            while (list.hasMore()) {
                name = list.next().getName();
                System.out.println(level + "/" + name);
            }
            /*
            level = "java:comp/env/ejb"; // Throws Error!
            list = ic.list(level);
            while (list.hasMore()) {
                name = list.next().getName();
                System.out.println(level + "/" + name);
            }
            */
            level = "java:global";
            list = ic.list(level);
            while (list.hasMore()) {
                name = list.next().getName();
                System.out.println(level + "/" + name);
            }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我们正在使用SoapUI工具测试设置(http://www.soapui.org)。同样,我们为一个简单的Ping()服务获得了正确的结果,但一旦需要与会话相关的东西,它就会失败。

当然,我做了一些研究,特别有趣的是这个链接:如何在JBOSS中获取SessionContext其还指:http://javahowto.blogspot.co.uk/2006/06/4-ways-to-get-ejbcontext-in-ejb-3.html

关于EJB生命周期:docs.oracle.com/cd/E13224_01/wlw/docs100/guide/EJB/session/CossionBeanLifeCycle.htmlJ2EE教程:docs.oracle.com/javaee/6/Tutorial/doc/gipss.html#gipsx(抱歉,不能发布超过2个链接…)

但正如代码中所评论的,我无法访问java:comp/env/ejb/sessionContext。好甚至连java:comp/env/ejb都没有。我编写了一个脏助手方法来查看EJB实际可访问的内容,并获得以下输出:

SystemOut     O /jta
SystemOut     O /eis
SystemOut     O /cell
SystemOut     O /thisNode
SystemOut     O /DefaultDatasource
SystemOut     O /services
SystemOut     O /jdbc
SystemOut     O /servername
SystemOut     O /com.ibm.websphere.ejbcontainer
SystemOut     O /com
SystemOut     O /zek
SystemOut     O /wm
SystemOut     O /ejb
SystemOut     O /Increment
SystemOut     O /tm
SystemOut     O ejb/ivtEJBObject
SystemOut     O ejb/ch
SystemOut     O ejb/mgmt
SystemOut     O java:comp/ValidatorFactory
SystemOut     O java:comp/TransactionSynchronizationRegistry
SystemOut     O java:comp/ORB
SystemOut     O java:comp/Validator
SystemOut     O java:comp/UserTransaction
SystemOut     O java:comp/env
SystemOut     O java:comp/BeanManager
SystemOut     O java:comp/websphere
SystemOut     O java:comp/HandleDelegate
SystemOut     O java:global/com.ibm.ws.AppNameSpaces
SystemOut     O java:global/NewZekEar
SystemOut     O java:global/SchedulerCalendars
SystemOut     O java:global/DefaultApplication
SystemOut     O java:global/cell
SystemOut     O java:global/query
SystemOut     O java:global/ManagementEJB
SystemOut     O java:global/ivtApp

我还尝试使用注释:

@Resource
private SessionContext mySessionCtx;

或者以不同的方式设置交易类型:

transaction-type="Container"

没有任何帮助。我理解SessionContext应该自动创建。但如果没有,那么必须创建一个新的选项?进一步的问题:servlet还"创建"了一个我可以访问的会话(request.getSession()),但这是不同的(HTTPSession)。我想我不能共享或"转换"这个会话对象到会话Bean?

您已经自己实例化了EclsTestBeanBean

EclsTestBeanBean eclsTestBean = new EclsTestBeanBean();

EJB依赖于将对象包装在代理中,以允许容器添加额外的服务,包括依赖注入会话上下文,这意味着只有容器才能创建并提供这些服务。您还会发现这个对象不会执行任何EJB服务,它只是一个POJO。

要从容器中请求EJB,您需要通过依赖注入来请求它,或者从JNDI中查找它,如下所示:

JPA实体管理器未正确注入-Weblogic

接下来的问题是,您的bean在JNDI中似乎不可用-我认为这是因为您没有为它定义本地或远程接口。这在EJB3中很好,在EJB3使用"无接口"视图,但在EJB2中不可用:http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jst.ejb.doc.user%2Ftopics%2Fcearch.html

因此,我建议您自己编写一个接口,(希望)然后EclsTestBeanBean在JNDI中可用,并从那里查找它,而不是实例化它

(使用@Resource注释,请注意,所有基于注释的工作也都添加到了EJB3中。)

相关内容

  • 没有找到相关文章

最新更新