EJB 不“可见"EJB 经理.不能使用 CDI 或 JNDI 来引用它 [英] EJB not "visible" to EJB manager. Cannot use CDI or JNDI to reference it

查看:32
本文介绍了EJB 不“可见"EJB 经理.不能使用 CDI 或 JNDI 来引用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试创建一个新的 EJB 并将其注入另一个 EJB 以便我可以调用它的资源时,突然出现了一个奇怪的问题.我使用的是 Glassfish 3.1 和 Java EE 6.

I have a weird problem that suddenly sprung up when I was trying to create a new EJB and inject it into another EJB so I could call its resources. I'm using Glassfish 3.1, and Java EE 6.

我之前在同一个项目中做过几次都没有问题,但由于某种原因,这个 EJB 会导致部署错误.只要我添加注释

I've done this a couple of times before without problems in the same project, but for some reason this EJB causes deployment errors. As soon as I add the annotation

@EJB EJBname ejbname;

@EJB EJBname ejbname;

对于我想在其中引用并保存的 bean,出现部署服务器错误.

To the bean I want to reference it in and save I get a deployment server error.

服务器日志显示:

引起:javax.naming.NameNotFoundException: com.bob.thrift.ThriftClient#com.bob.thrift.ThriftClient 未找到

Caused by: javax.naming.NameNotFoundException: com.bob.thrift.ThriftClient#com.bob.thrift.ThriftClient not found

javax.naming.NamingException: 异常解析 Ejb for 'Remote ejb-ref name=com.bob.logic.RSSbean/tclient,Remote 3.x interface =com.bob.thrift.ThriftClient,ejb-link=null,lookup=,mappedName=,jndi-name=com.bob.thrift.ThriftClient,refType=Session' .实际(可能是内部的)远程 JNDI用于查找的名称是 'com.bob.thrift.ThriftClient#com.bob.thrift.ThriftClient' [根异常是 javax.naming.NamingException:查找失败...

javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=com.bob.logic.RSSbean/tclient,Remote 3.x interface =com.bob.thrift.ThriftClient,ejb-link=null,lookup=,mappedName=,jndi-name=com.bob.thrift.ThriftClient,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'com.bob.thrift.ThriftClient#com.bob.thrift.ThriftClient' [Root exception is javax.naming.NamingException: Lookup failed for...

我不知道哈希符号 # 是什么意思,或者我是否可以确认这是正确的语法.但是,看起来这是我的班级所在的正确包.

I don't know what that hash symbol # means or if I can confirm that that is the correct syntax. It looks like that is the correct package where my class exists, however.

我所做的正是我为其他 EJB 所做的,它们都是简单的 @stateless 会话 bean.这似乎类似于未在构建路径中列出的引用库文件.好像它有名字,但它找不到实际位置.在 EJB 注入的情况下,我不确定如何解决这个问题.

I'm doing exactly what I did for the other EJBs they're all simple @stateless session beans. This seems to be analogous to a referenced library file not being listed in the buildpath. As if it has the name but it can't find the actual location. I'm not sure how to resolve this in the case of EJB injection.

带有我需要的东西的 EJB:包 com.bob.thrift;

The EJB with the stuff I need: package com.bob.thrift;

import com.bob.thrift.sendEventMessage2;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;

import javax.annotation.ManagedBean;
import javax.ejb.Remote;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Stateless;

@Stateless
@LocalBean

public class ThriftClient {

public ThriftClient(){} 

public String sendToServer(String say){
    System.out.println("Entering ThriftClient's main method starting server connection...");
    String msg;
    //**Make Socket**
    TSocket socket = new TSocket("137.222.23.23",1111);

    //**Make Buffer**
    //TSocket bufferedSocket = (socket); skipping this step because the jvm already handles
    //the buffering on this end. 

    //**put in protocol**
    TBinaryProtocol protocol = new TBinaryProtocol(socket);
    //**create client to use protocol encoder**
    sendEventMessage2.Client client = new sendEventMessage2.Client(protocol);

带有导致部署错误的注入的 EJB:包com.bob.logic;

The EJB with the injection that causes deployment errors: package com.bob.logic;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.bob.eao.XRSSeao;
import com.bob.thrift.ThriftClient;


 @Stateless
 @LocalBean
 //@WebService
public class RSSbean {

private String inputString;
private List Statuses;

@EJB private ThriftClient tclient;

一旦我添加了上面的行@EJB ThriftClient tclient",它就不会部署,并且我得到 NameException、JNDI 查找、映射 null 类型的异常.它拒绝以这种方式被发现.

As soon as I add the above line "@EJB ThriftClient tclient" it will not deploy and I get NameException, JNDI lookup, mapping null type of exceptions. It refuses to be found this way.

推荐答案

我不确定您是否正确使用 ejbName.假设您有以下 bean:

I'm not sure if you did it correctly with your ejbName. Suppose you have the following bean:

@Stateless
public class MrBean implements MrBeanInterface {}

然后你需要用 @EJB 注释注入 bean,如下所示:

Then you need to inject the bean with @EJB annotation as following:

@EJB
private MrBeanInterface mrBean;

注意这个类是MrBeanInterface,而不是直接MrBean.或者,如果你使用 CDI 并且你有 2 个相同接口的实现,你也可以像这样注入一个 bean:

Notice that the class is MrBeanInterface, not MrBean directly. Alternatively, if you use CDI and you have 2 implementation for the same interface, you can also inject a bean like this:

@Stateless
public class MrBean  implements BeanInterface {}

@Stateless
public class MrsBean implements BeanInterface {}

@Inject
@Exact(MrBean.class)
private BeanInterface mrBean;

更新 1:

这是来自 Oracle 的教程:

  • Java EE 应用程序客户端通过以下方式引用企业 bean 实例使用@EJB 批注来批注静态字段.注释的静态字段代表企业 bean 的业务接口,这将在应用程序时解析为会话 bean 实例客户端容器在运行时注入资源引用.

关于什么是EJB?什么时候使用EJB?EJB的好处?,可以参考这个文章.

Regarding What is EJB?, When to use EJB? and Benefits of EJB?, you can refer to this article.

更新 2:

根据您的更新,您的 @LocalBean 没有任何接口.这可能违反了 EJB 3.0 的规范.在这个 Oracle 的文档中,他们提到:

According to your update, your @LocalBean does not have any interfaces. This might have violated EJB 3.0's specs. In this Oracle's documentation, they mentioned:

  • 当使用 EJB 3.0 编程模型对 bean 进行编程时,您是需要指定业务接口.
  • When using the EJB 3.0 programming model to program a bean, you are required to specify a business interface.

此外,在这个 教程来自 IBM,他们定义了一个无接口的本地 SessionBean 如下:

Besides, in this tutorial from IBM, they defined a No-Interface Local SessionBean as following:

  • 该 bean 不公开任何其他客户端视图(Local、Remote、2.x Remote Home、2.x Local Home、Web Service),并且它的 implements 子句为空.
  • 该 bean 公开至少另一个客户端视图.bean 指定它使用 @LocalBean 注释在 bean 类或部署描述符中公开一个无接口视图.
  • The bean does not expose any other client views (Local, Remote, 2.x Remote Home, 2.x Local Home, Web Service) and its implements clause is empty.
  • The bean exposes at least one other client view. The bean designates that it exposes a no-interface view with the @LocalBean annotation on the bean class or in the deployment descriptor.

简而言之,即使您不需要使用它,我认为您仍然应该为您的 ThriefClient 指定一个接口.

In brief, I think you should still specify an interface for your ThriefClient even if you don't need to use it.

@Stateless
@LocalBean
public class ThriefClient implements ThriefInterface {
   // Your  functions
}

@Local
public interface ThriefInterface {
   // Empty interface
}

或者,在 EJB 3.1 中,您可以试试这个:

Alternatively, in EJB 3.1, you can try this:

@Stateless
public class ThriefClient {
   // Your  functions
}

@Stateless
public class RSSbean {
    @EJB
    private ThriefClient thriefClient;
}

这篇关于EJB 不“可见"EJB 经理.不能使用 CDI 或 JNDI 来引用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆