在JNDI查找中使用什么上下文名称?

时间:2014-05-20 22:52:12

标签: java-ee jndi

我有一个SFSB声明:

@Stateful(name="StatefulBean")
public class StatefulBean implements MyListener
{
    private String id;

    public StatefulBean()
    {
    }

    public void setId(String id)
    {
        this.id = id;
    }
}

我试图在servlet的doGet方法中实例化它,如下所示:

protected void doGet(final HttpServletRequest request,
    final HttpServletResponse response) throws ServletException, IOException
{
    Date now = new Date();
    String id = new Long(now.getTime()).toString();

    try
    {
        Context context = new InitialContext();
        StatefulBean statefulBean = (StatefulBean) context.lookup("name");
        statefulBean.setId(id);
        response.getWriter().write("Created StatefulBean with id " + id);
    }
    catch(Exception ex)
    {
        response.getWriter().write("Exception creating bean:" + ex.getMessage());
    }
}

servlet和bean都属于my.package,并且在同一个名为BeanTest.war的war文件中部署到Glassfish3服务器。

我在确定name应该是什么时遇到问题。 Glassfish server.log声明:

[#|2014-05-20T16:31:51.720-0600|INFO|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=988;_ThreadName=Thread-14;|EJB5181:Portable JNDI names for EJB StatefulBean: [java:global/BeanTest/StatefulBean!my.package.MyListener, java:global/BeanTest/StatefulBean]|#]

我尝试在namejava:global/BeanTest/StatefulBean!ca.shaw.tno.oss.MyListener的查询调用中设置java:global/BeanTest/StatefulBean。当使用其中任何一个调用doGet时,我收到此错误:

Exception processing batch:com.sun.proxy.$Proxy353 cannot be cast to my.package.StatefulBean

由于这些是Glassfish服务器提供的唯一两个命名选项,我应该使用什么?

1 个答案:

答案 0 :(得分:0)

问题是我没有使用@LocalBean注释我的StatefulBean。一旦我添加了它,JNDI查找就成功了。

@Stateful(name="StatefulBean")
@LocalBean
public class StatefulBean implements MyListener
{
    private String id;

    public StatefulBean()
    {
    }

    public void setId(String id)
    {
        this.id = id;
    }
}