Show TOC

Procedure documentationImplementing the Remote Interface Locate this document in the navigation structure

 

After you have defined the remote interface, you need to provide its implementation.

Procedure

  1. Define the class that implements the remote interface and provide the implementation of the methods of this interface.

    Note Note

    You can implement more than one remote interface in your implementation class.

    End of the note.

    Caution Caution

    The RMI-P4 runtime makes no guarantees with respect to mapping remote object invocations to threads. The next invocation of the same remote method may or may not happen in the same thread so remote object implementations should not use and rely on the state of thread-local variables. Since remote method invocation on the same remote object may execute concurrently, a remote object implementation needs to make sure its implementation is thread-safe.

    End of the caution.
  2. You can also implement additional methods that are not defined by any of the implemented remote interfaces. However, such methods cannot be called remotely.

Example

The AccountImpl class is the implementation class of the Account remote interface. The full code of the class is listed below:

Syntax Syntax

  1. package examples.rmi_p4;
    
    import java.rmi.RemoteException;
    
    public class AccountImpl implements Account {
      private int balance = 0;
    
      public void deposit(int amount) throws RemoteException {
        if (amount < 0) {
          throw new RemoteException("Incorrect value:" + amount);
        }
        balance += amount;
      }
    
      public void draw(int amount) throws OverdrawException {
        if (balance < amount) {
          throw new OverdrawException(amount - balance );
        }
        balance -= amount;
      }
    
      public int getBalance() {
        return balance;
      }
    
End of the code.