Remote Method Invocation (Level I)
Compilation and Execution
Till now, you have created 4 classes:
Make sure that you put these four files in one subdirectory. In this case, we put in C:\myrmi\ subdirectory.
C:\myrmi\Payment.java PaymentImpl.java Server.java Client.java
Compile them using the following commands:
C:\myrmi\javac -d . Payment.java PaymentImpl.java Server.java Client.java
Note that the switch "-d ." means "put the compiled files into the current directory". If you want to put the compiled files to some specific directory for example, c:\myrmi\mortgage, you will use command instead:
javac -d mortgage Payment.java .... etc.
Now, you can try the client\server system on your own computer. You may be wondering how it could be to use one computer to try a client\server system. Remember that, you can start different command-line windows to try it. Because every command-line window will get one distinguished Java Virtual Machine(JVM) instance, and the communication between two JVMs can be established in one computer. We will use localhost as a host for the server.
Betty checked the RMI specification, found that a new feature for running an RMI server is available.
If you use jdk version below 1.5.0, you cannot run the examples listed here. You must use rmic utility to generate a stub class. If so, use the following command to generate a stub class:
set classpath= rmic -v1.2 Server
A file with name Server_stub.class is generated for JVM communication. Now you can go to the next step to start rmiregistry and server.
Let's start rmiregistry. First make sure the classpath is empty, then use command as follows:
C:\myrmi\set classpath= C:\myrmi\start rmiregistry
Note that an empty command-line window will pop-up. Don't close it, minimize it if you want. That window is for rmiregistry.exe program.
Then, you can start a server window in the following command:
C:\myrmi\start java Server
The pop-up window will have the following printout which indicates the server is started successfully and ready to accept client connection.
Mortgage server is ready to listen...
Note that the RMI server we designed is not for one client connection. It can accept many-client connections simultaneously.
When the server starts, you are ready to start a client. Please issue the following command:
When the client starts, the LocateRegistry.getRegistry("localhost") sends information to the RMI registry which is held by the localhost(the Server command-line window) and asks to look for a remote object named "Mortgage". Since the server has such remote object, so the server sends an instance of stub class back, or we can say the client downloads the stub class from the localhost server. When the client gets the reference to the remote object, it can use such reference to call remote method calculatePayment() just like to call a local method. This is the most beautiful thing we mentioned earlier for RMI technology.
The following shows all commands and printout when you start the RMI server and client system.
C:\myrmi>javac -d . Payment.java PaymentImpl.java Server.java Client.java C:\myrmi>set classpath= C:\myrmi>start rmiregistry C:\myrmi>start java Server C:\myrmi>java Client Usage: java Client principal annualInterest years For example: java Client 80000 .065 15 You will get the output like the following: The principal is $80000 The annual interest rate is 6.5% The term is 15 years Your monthly payment is $696.89 C:\myrmi>java Client 150000 .060 15 The principal is $150000 The annual interest rate is 6.0% The term is 15 years Your monthly payment is $1265.79 C:\myrmi>
Congratulations! You have successfully designed a simple RMI server\client system.
It is time for you to recap the process of designing an RMI server and client system:
Betty felt so happy. She said to her colleague.
"It's great to see something actually work. I am going to use RMI server to publish the mortgage calculator."
Betty still has some doubts in her mind, like how to get the remote object run at a specific TCP port, why the server is running all the time if no client to connect it, it seems wasting resource? All these issues will be discussed in RMI level 2 tutorial.
Note: if you follow the examples exactly, you will get same result. If you use different subdirectory, or create your own package, the commands used to run server and client may be different. You are recommended to use exact code and commands to try the sample code if you are brand new to the RMI technology. If you did so, but failed to run, please send your information to javacamp.org. Javacamp.org will try its best to help you run the sample programs successfully.
Any comments for the RMI (level I) tutorial? please feel free to fill this form
Compile and run the client/server system you created in the previous sections.
A possible solution:
C:\myrmi>javac -d . Hello.java HelloImpl.java HelloServer.java HelloClient.java C:\myrmi>set classpath= C:\myrmi>start rmiregistry C:\myrmi>start java HelloServer C:\myrmi>java HelloClient Got info from server: Hello, Betty C:\myrmi>
Note that: you will have two more command-line windows: One is for rmiregistry which has nothing to print out. The other window is for HelloServer. The message from the server printed out like the following.
Hello Server is ready to listen...
Note: you have to start HelloServer first before you start the HelloClient.
If you use Java 5 and got exception, please make a batch file to run all commands at one time. Copy the following code to a notepad and save it as run.bat.
javac -d . Hello.java HelloImpl.java HelloServer.java HelloClient.java set classpath= start rmiregistry start java HelloServer java HelloClient
At command line, type c:\run
page 1 page 2 page 3 page 4 page 5 page 6 |home|