EJB不是“可见的".到EJB管理器.无法使用CDI或JNDI引用它 [英] EJB not "visible" to EJB manager. Cannot use CDI or JNDI to reference it

查看:103
本文介绍了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-ref名称= com.bob.logic.RSSbean/tclient,远程3.x接口= 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查找,映射空类型的异常.它拒绝以这种方式被发现.

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实例 客户端容器在运行时注入资源引用.
  • Java EE application clients refer to enterprise bean instances by annotating static fields with the @EJB annotation. The annotated static field represents the enterprise bean’s business interface, which will resolve to the session bean instance when the application client container injects the resource references at runtime.

关于What is EJB?When to use EJB?Benefits of 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.

此外,在此

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

  • 该bean不公开任何其他客户端视图(本地,远程,2.x远程主页,2.x本地主页,Web服务),并且它的Implements子句为空.
  • 该bean至少公开了一个其他客户端视图. Bean指定在Bean类或部署描述符中使用 @LocalBean 批注公开无接口视图.
  • 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天全站免登陆