Simple Singleton EJB示例

时间:2013-12-19 09:41:44

标签: java ejb

我正在学习EJB,并且已经使用GlassFish 4编写了一个示例程序,但由于某种原因它无法正常工作。

的index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="javax.ejb.EJB" %>
<%@ page import="ejbtest.utility.Utility" %>
<%@ page import="ejbtest.action.*" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>EJB Test</title>
    </head>
    <body>
        <%
            try {
                Utility utility = new Utility();
                out.println("count: " + utility.getCount() + "<br/><br/>");
                Trigger1 trigger1 = new Trigger1();
                out.println("trigger1 count: " + trigger1.getTriggerCount());
            } catch (NullPointerException npe) {
                out.println("Nullpointer caught");
            }
        %>
    </body>
</html>

Utility.java

package ejbtest.utility;

import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class Utility {

    int count;

    public Utility() throws NullPointerException {
        count = 0;
        count++;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

Trigger1.java

package ejbtest.action;

import ejbtest.utility.Utility;
import javax.faces.bean.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class Trigger1 {

    @Inject
    Utility utility;

    public Integer getTriggerCount() {
        return utility.getCount();
    }

}

不受欢迎的输出

  

数:1

     

Nullpointer抓住了

2 个答案:

答案 0 :(得分:5)

首先,您在标准JVM中启动EJB。但EJB只能存在于EJB Container中。当您使用main方法启动应用程序时,所有EJB注释都会被忽略。您需要像glassfish或JBoss这样的应用程序服务器来测试您的应用程序。
这里您的改进示例:

Trigger1.java

package ejbtest.action;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ejbtest.utility.Utility;

@Stateless
public class Trigger1 {

    @EJB
    private Utility utility;

    public Integer getTriggerCount() {
        return utility.getCount();
    }
}

<强> Utility.java

package ejbtest.utility;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;

@Singleton
public class Utility {
    int count = 0;

    @PostConstruct
    public void init() {
        count++;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

<强>的index.jsp

<%@page import="ejbtest.action.Trigger1"%>
<%@page import="ejbtest.utility.Utility"%>
<%@page import="javax.naming.InitialContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>EJB Test</title>
    </head>
    <body>
    <%
        InitialContext ic = new InitialContext();
        Utility utility = (Utility) ic.lookup("java:module/Utility");
        out.println("count: " + utility.getCount() + "<br/><br/>");
        Trigger1 trigger1 = (Trigger1) ic.lookup("java:module/Trigger1");
        out.println("trigger1 count: " + trigger1.getTriggerCount());
    %>
    </body>
</html>

答案 1 :(得分:0)

我认为您只能在其他EJBSession Beans中注入Message-Driven Beans。在您的代码中,您尝试将它们注入到简单的java类中。您可以使用JNDI lookup

如果您的班级Trigger1Trigger2与您的Java EE Bean不在同一个(Utility)应用程序中,那么您需要Remote访问权限,因此界面视图。然后,您可以按如下方式访问Singleton Bean:https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project

如果您的Trigger1Trigger2类与Utility Bean位于同一个应用程序中,那么您已经提供的Local no interface视图就足够了。您可以像Singleton那样通过Injection访问Session Bean,但是您必须创建类JNDI lookup Bean(最好是无状态)。然后,您应该再次通过{{1}}从主方法访问它们。