Você está na página 1de 3

National University of Singapore School of Computing IS2103 Enterprise Systems Development Concepts

Workshop 1: Remote Method Invocation (RMI)


Notes: You should have a copy of the book Learn To Program Enterprise JavaBeans 3.0 3rd Edition with you to complete the exercises in this workshop.

Developing Applications using RMI 1. Implement the AddServer RMI example in Chapter 3 of the book. Instructions on how to run the application are available in the chapter. The instructor will be running the AddServer on his machine. You will be informed of the IP address of AddServer. You will need the Stub class from the instructor: addRmi.AddServerImpl_Stub.class Copy the Stub class into your machine where your Client application resides. Execute the client application on your machine using the arguments as suggested in the chapter. You should get the same output as recorded in Chapter 3. The default port for RMI is 1099. 2. You are required to run the AddServer on your machine. Instructions on how to execute your AddServer are provided in Chapter 3. Once you are ready, inform the instructor of your machines IP address. Use the default port 1099 for your RMI server bound as AddServer. The instructor will execute his client application to connect to your machine.

3. The LuckyDrawServer interface below specifies the methods you as a client can invoke.
package luckyDraw; import java.rmi.Remote; import java.rmi.RemoteException; public interface LuckyDrawServer extends Remote { // input : client's ipAddress and name // output: prize id public String register(String ipAddress, String name) throws RemoteException; // input : prize id // output: prize public String getPrize(String id) throws RemoteException; // input : client's ipAddress and name // output: server's response public String thankYou(String ipAddress, String name) throws RemoteException; }

You are required to write a client application to: Connect to the server via the RMI protocol. Register yourself with the server by calling the register() method with the appropriate arguments. The ipAddress and name are used as a key combination to uniquely identify you. In return, you will get the prize identification code. Claim your prize by calling the getPrize() method with the prize identification code. Send an acknowledgement to the server by calling thankYou() on the server. You will receive a message from the server. Display the message. The instructor running the server has to pass you a Stub class file (luckyDraw.LuckyDrawServerImpl_Stub.class) so that you can execute your client application. Client output should look as follow (replace 123.123.123.123 with your client machines IP address; replace 127.0.0.1 with the servers IP address); 1099 is the port number RMI is listening at; Danny is the clients name:

4. Implement the LuckyDrawServer interface and run the LuckyDrawServerApp on your machine. Your instructor will be executing his client application to connect to your server. Your server should have the following classes: LuckyDrawServer.class LuckyDrawServerApp.class LuckyDrawServerImpl.class LuckyDrawServerImpl_Stub.class Create your own list of lucky draw prizes and prize ids. Bind the server as LuckyDrawServer. You should pass to your instructor (who now behaves as a client) a Stub class file (luckyDraw.LuckyDrawServerImpl_Stub.class) so that he can execute his client application on his machine. Execute the server application on your machine and inform the instructor when you are ready to receive requests from clients. Do not forget to start the rmiregistry (start rmiregistry) at the appropriate directory. A sample output of the server is given below:

Você também pode gostar