A case study for Java RMI: Reservator Assister

INTRODUCTION

Remote method invocation allows applications to call object methods located remotely, sharing resources and processing load across systems. Unlike other systems for remote execution which require that only simple data types or defined structures be passed to and from methods, RMI allows any Java object type to be used - even if the client or server has never encountered it before. RMI allows both client and server to dynamically load new object types as required.

THREE STEPS

If we want to build a distributed system based on Java RMI we have to follow three steps:1.Defining the remote interfaces: A remote interface specifies the methods that can be invoked remotely by a client. Clients program to remote interfaces, not to the implementation classes of those interfaces. The design of such interfaces includes the determination of the types of objects that will be used as the parameters and return values for these methods. If any of these interfaces or classes do not yet exist, you need to define them as well.2.Implementing the remote objects: Remote objects must implement one or more remote interfaces. The remote object class may include implementations of other interfaces and methods that are available only locally. If any local classes are to be used for parameters or return values of any of these methods, they must be implemented as well.3.Implementing the clients: Clients that use remote objects can be implemented at any time after the remote interfaces are defined, including after the remote objects have been deployed.

1 - REMOTE INTERFACE

An object becomes remote by implementing a remote interface so, before anything else, we have to implement a remote interface like this: RemoteReservation.javaThis Java interface is a remote interface because:
  • it extends the interface java.rmi.Remote;
  • each method of the interface declares RemoteException in its throws clause.

2 - REMOTE OBJECTS

Server implementation: RemoteReservationImpl.javaThis object is the server implementation and it contains remote methods implementations and a "particulary" main. In the main server take locale RMI registry reference, that offers "yellow pages service", and bind on it our new service ("ReservationService").

3 - IMPLEMENTING THE CLIENT

Client implementation: ReservationClient.javaClient have to set the Java SecurityManager and connect to the server using Naming.lookup(...) method. N.B. Client MUST know: address server, server RMI registry port and service name (in our case it is "ReservationService")

THE LAST STEP

Now we have ALMOST all the components required to offer our new service. Finally we need stub! It acts as a gateway for client side objects and all outgoing requests to server side objects that are routed through it. The stub wraps client object functionality and by adding the network logic ensures the reliable communication channel between client and server.Open a shell in the server package root folder and type " rmic -d unibo/sisdist1011/rmi/reservation/ unibo.sisdist1011.rmi.reservation.RemoteReservationImpl ". This command create stub starting from server class. Obviously it considers the remote interface.N.B. Create a security file in server and client folder in which we describe Java security policy for this program. Because this is a simple case study we allow anything and set "AllPermission" variable.

LET'S RUN!

And now let's start! Open a shell in the server package root folder and start the Java RMI Registry typing " rmiregistry ". Now the registry is listening on port 1099 (be careful on setting up your system firewall!!!).Then type " java -classpath :/unibo/sisdist1011/rmi/reservation -Djava.rmi.server.codebase=file:///unibo/sisdist1011/rmi/reservation -Djava.security.policy=security.policy unibo.sisdist1011.rmi.reservation.RemoteReservationImpl".Pay attention to the options: -classpath : add a directory to classpath variable. -Djava.rmi.server.codebase : tells the client where the implementation of stub class can be found. -Djava.security.policy : configure Java security policy.Now server is running. Move to client package root folder and type " java -Djava.security.policy=security.policy unibo.sisdist1011.rmi.reservation.ReservationClient".Default client connect to server using local network, on default port 1099, at service named " ReservationService ". You can launch client defining "reservator address" "reservator port" "service name".For simplify the program launch I have edit 2 scripts named startup.sh. To testing the program try to type in the client shell command "help" and have fun!

BIBLIOGRAPHY

Introduction to Java RMI (http://www.javacoffeebreak.com/articles/javarmi/javarmi.html)An Overview of RMI Applications (http://download.oracle.com/javase/tutorial/rmi/overview.html)Wikipedia (http://en.wikipedia.org/wiki/Class_stub)