RMI Notes

Rmiremoteexception public long sublong a long b

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Calculator extends java.rmi.Remote { public long add(long a, long b) throws java.rmi.RemoteException; public long sub(long a, long b) throws java.rmi.RemoteException; public long mul(long a, long b) throws java.rmi.RemoteException; public long div(long a, long b) throws java.rmi.RemoteException; } Notice this interface extends Remote, and each method signature declares that it may throw a RemoteException object. Copy this file to your directory and compile it with the Java compiler: >javac Calculator.java 2. Implementation Next, you write the implementation for the remote service. This is the CalculatorImpl class: public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator { // Implementations must have an //explicit constructor // in order to declare the //RemoteException exception public CalculatorImpl() throws java.rmi.RemoteException { super(); } public long add(long a, long b) throws java.rmi.RemoteException { return a + b; } public long sub(long a, long b) throws java.rmi.RemoteException { return a - b; } public long mul(long a, long b) throws java.rmi.RemoteException { return a * b; } public long div(long a, long b) throws java.rmi.RemoteException { return a / b; } } Again, copy this code into your directory and compile it. The implementation class uses UnicastRemoteObject to link into the RMI system. In the example the implementation class directly extends UnicastRemoteObject. This is not a requirement. A class that does not extend UnicastRemoteObject may use its exportObject() method to be linked into RMI. When a class extends UnicastRemoteObject, it must provide a constructor that declares that it may throw a RemoteException object. When this constructor calls super(), it 9 Manisha Akolkar INTERNATIONAL INSTITUTE OF INFORMATION TECHNOLOGY P-14, Pune Infotech Park, Hinjaw adi, Pune – 411 027 activates code in UnicastRemoteObject that performs the RMI linking and remote object initialization. 3. Stubs and Skeletons You next use the RMI compiler, rmic, to generate the stub and skeleton files. The compiler runs on the remote service implementation class file. >rmic CalculatorImpl Try this in your directory. After you run rmic you should find the file Calculator_Stub.class and, if you are running the Java 2 SDK, Calculator_Skel.class. Options for the JDK 1.1 version of the RMI compiler, rmic, are: Usage: rmic <options> <class names> where <options> includes: -keep Do not delete intermediate generated source files -keepgenerated (same as "-keep") -g Generate debugging info -depend Recompile out-of-date files recursively -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -classpath <path> Specify where to find input source and class files -d <directory> Specify where to place generated class files -J<runtime flag> Pass argument to the java interpreter The Java 2 platform version of rmic add three new options: -v1.1 Create stubs/skeletons for JDK 1...
View Full Document

Ask a homework question - tutors are online