Reputation: 21
I have a EJB named "BookEJB", which implements an remote interface "BookEJBRemote"
BookEJB is declared like this:
@Stateless
@Remote(BookEJBRemote.class)
public class BookEJB implements 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
and the line that has the error is:
BookEJB mediaEJB = (BookEJB) ctx.lookup("java:global/classes/BookEJB");
I don't what's going on here...
Upvotes: 2
Views: 3522
Reputation: 691625
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();
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.
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.
Upvotes: 5