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 ...

Get Java Enterprise in a Nutshell, Third Edition 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.