Previous lesson 1/6 |home| Next lesson 3/6

Remote Method Invocation (Level I)

Design a Remote Interface

Since the calculatePayment() method is a key method in Mortgage class, Betty decided to make this method remotely available (invoked). If a user provides principal, annual interest rate and loan term from a command-line, the monthly payment information will be printed on the client side.

How to make such method available via network? According to the RMI technology, you should design a remote interface first and then put the method signature inside the remote interface. Betty checked the new RMI specification, such design step since jdk 1.1 has not been changed.

What is a remote interface?

According to RMI specification, a remote interface is an interface that extends java.rmi.Remote interface and declares a set of methods that may be invoked from a remote Java Virtual Machine(JVM). Note that the java.rmi.Remote interface serves as a mark to tell the system that methods declared within this interface may be invoked from a non-local virtual machine.

What is a remote method?

A remote method is a method that is declared inside a remote interface.

Betty followed the step and designed a remote interface named Payment. She declared the method calculatePayment() signature and made it throw RemoteException which is required.

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Payment extends Remote {
    public double calculatePayment(double principal, double annualRate, int terms)
                    throws RemoteException;

Recall that only method signatures or abstract methods are allowed within an interface, so does the remote interface. The implementation class will take care of the remote methods.

Right now, we can say that Payment is a remote interface and calculatePayment() is a remote method.

You may be wondering why the method must throw RemoteException. Because remote method invocation may fail in many ways, like server down, resources unavailable, the method itself may throw exception, etc. If the failure happens, the client should be able to know it via the RemoteException.

Please note that:

  1. The Payment interface must extends java.rmi.Remote interface. It is required.
  2. The method signature in the remote interface must throw java.rmi.RemoteException. It is required too.

If you want multiple methods available remotely, just put these method signatures inside one or more remote interfaces and let them throw java.rmi.RemoteException.

This is the first step you should do when you use RMI technology. Ask yourself if you know how to make a method remotely available, what is the first step?

The next step is to design a remote object.

Check your skill

Design a method that can be called to say Hello to the parameter. Let's make it have such signature -- String sayHello(String s).

A possible solution:

page 1  page 2  page 3  page 4  page 5  page 6  Previous lesson 1/6 |home| Next lesson 3/6