Making a remote method call

When you write a client to access a bean, the client is either local or remote. A local client means the client is running in the same JVM as the bean. In other words, both the bean and the client live in the same heap. We’ll talk about that much more in the Client View chapter, but for now, remember that local means same heap/JVM. Chances are, you’ll use local clients only with entity beans, and only under very special circumstances.

You’ll use a remote client when you want a bean to be used by the outside world. Most enterprise applications have a remote client, even if some of the beans used in the application talk to one another as local clients. (We’ll explore every gory detail about this before the book is over.)

So how does an object in one heap/JVM directly call a method on a reference to an object running in another heap/JVM? Technically, it’s not possible! Java references hold bits that don’t mean anything outside the currently running JVM. If you’re an object and you have a reference to another object, that object must be in the same heap with you.

Java RMI (Remote Method Invocation) solves this problem by giving the client a proxy (called a stub) object that acts as the go-between for the client and Remote object. The client calls a method on the stub, and the stub takes care of the low-level communication (sockets and streams) with the Remote object.

With RMI, your client object gets to act like it’s making a remote method call. But what it’s ...

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.