O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

RMI and Native Method Calls

RMI is a Java-only remote object scheme, so it doesn’t provide a direct connection between objects implemented in different languages, as CORBA does. But, using Java’s Native Interface API , it is possible to wrap existing C or C++ code with a Java interface and then export this interface remotely through RMI.

To demonstrate, let’s suppose we have some (legacy) native code that implements the withdrawal, deposit, and transfer operations for a particular banking system. We can create an implementation of our Account interface that uses this native code to implement the deposit(), withdraw(), and transfer() methods on our remote interface. The implementation for a NativeAccountImpl is shown in Example 13-4. The only significant difference between this implementation and our original AccountImpl is that the deposit(), withdraw(), and transfer() methods are declared native, so the method bodies are not provided here.

Example 13-4. Remote object using a native method implementation

import java.rmi.server.UnicastRemoteObject; import java.rmi.RemoteException; import java.util.List; import java.util.ListIterator; public class NativeAccountImpl extends UnicastRemoteObject implements Account { // Our current balance private float mBalance = 0; // Name on account private String mName = ""; // Create a new account with the given name public NativeAccountImpl(String name) throws RemoteException { mName = name; } public String getName() throws RemoteException ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required