Previous lesson 2/6 |home| Next lesson 4/6

Remote Method Invocation (Level I)

Design a Remote Object

What is a remote object?

According to the RMI specification, a remote object is one whose methods can be invoked from another Java virtual machine, potentially on a different host(JVM).

You have learnt from the previous section how to design a remote interface. Do you remember that you have not implemented the remote method calculatePayment(). The implementation class is a remote object. So we need to design a class which implements the remote interface Payment and defines the remote method -- calculatePayment().

Betty made the implementation class name PaymentImpl, because it is easily to be recognized when you deal with multiple classes. Since Payment is an interface, "Impl" means implementation, PaymentImpl is an implementation class of Payment interface. We will adopt such name convention in the following sections.

 
import java.rmi.RemoteException;

public class PaymentImpl implements Payment {

    public double calculatePayment(double principal, double annRate, int years)
                                  throws RemoteException {
        double monthlyInt = annRate / 12;
        double monthlyPayment = (principal * monthlyInt)
                    / (1 - Math.pow(1/ (1 + monthlyInt), years * 12));
        return format(monthlyPayment, 2);
    }
    public double format(double amount, int places) {
        double temp = amount; 
        temp = temp * Math.pow(10, places);
        temp = Math.round(temp);
        temp = temp/Math.pow(10, places);
        return temp;
    }
}

Right now, the PaymentImpl class is a remote object class. If you have multiple remote methods, you can implement them in this way.

Note that:

Ask yourself: what is a remote object?

Next step, we should create a server which can be used to hold the remote object.

Check your skill

Design a remote object class for sayHello() method.

A possible solution: HelloImpl.java

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