远程接口无法强制转换为EJB容器 [英] Remote interface cannot be cast to EJB container

查看:115
本文介绍了远程接口无法强制转换为EJB容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为BookEJB的EJB,它实现了一个远程接口BookEJBRemote

I have a EJB named "BookEJB", which implements an remote interface "BookEJBRemote"

BookEJB声明如下:

BookEJB is declared like this:

@Stateless    
@Remote(BookEJBRemote.class)   
public class BookEJB implements BookEJBRemote{}

BookEJBRemote声明如下:

BookEJBRemote is declared like this:

@Remote
public interface BookEJBRemote
{}

我有一个文件进行单元测试,当我运行测试时出现错误。
错误消息如下:

I have a file that does unit testing, and I got an error when I run the test. The error message is the following:

java.lang.ClassCastException: _BookEJBRemote_Wrapper cannot be cast to BookEJB

并且出错的行是:

BookEJB mediaEJB = (BookEJB) ctx.lookup("java:global/classes/BookEJB");

我不知道这里发生了什么......

I don't what's going on here...

推荐答案

远程接口(以及通常的接口)的原理是,对实现给定接口的对象的引用的客户端不需要知道具体类型对象。如果他知道界面,他会调用界面中定义的方法。

The principle of remote interfaces (and interfaces in general) is that a client which has a reference to an object implementing a given interface doesn't need to know the concrete type of the object. If he knows the interface, he calls the methods defined in the interface.

在界面中定义方法,否则没有任何意义。

Define methods in your interface, else it doesn't have any sense.

并在此界面上调用方法:

And call methods on this interface:

BookEJBRemote book = (BookEJBRemote) ctx.lookup("java:global/classes/BookEJB");
int pages = book.getNumberOfPages();

EJB容器用一个实现其接口的对象包装每个EJB,并委托给实际的EJB bean实例。但是在调用EJB bean实例上的方法之前,它确保事务已启动,调用者有权调用方法等。

The EJB container wraps every EJB with an object which implements its interface, and delegates to the actual EJB bean instance. But before calling the method on the EJB bean instance, it makes sure the transaction is started, the caller has the rights to call the methods, etc.

如果是远程接口,客户端可以存在于单独的JVM中,甚至无法访问BookEJB类。它只接收实现远程接口的存根类的实例。存根,在调用方法时,序列化参数,向服务器发送网络请求,从服务器获取响应,反序列化它,并将结果返回给调用者。 BookEJB实例仅存在于服务器JVM中。

In the case of remote interfaces, the client could live in a separate JVM, and not even have access to the BookEJB class. It just receives an instance of a stub class which implements the remote interface. The stub, when a method is called, serializes the arguments, sends a network request to the server, gets the response from the server, deserializes it, and returns the result to the caller. The BookEJB instance only lives inside the server JVM.

这篇关于远程接口无法强制转换为EJB容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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