Documente Academic
Documente Profesional
Documente Cultură
1/27
Developing Calculator application with RMI Edition
Contents
1. Introduction..........................................................................................................3
2. Objective ...............................................................................................................5
3. Hardware & Software Environment..................................................................5
3.1 Hardware ..................................................................................................5
3.2 Software ....................................................................................................5
4. Approach (Design) ...............................................................................................5
5. Implementation ....................................................................................................6
5.1 The Server Side ........................................................................................7
5.1.1 Creating the Interface.........................................................................7
5.1.2 Implementation of the remote service...............................................8
5.1.3 Implementation of the remote service.............................................10
5.2 The Client Side .......................................................................................11
6. Results (Execution) ............................................................................................13
6.1 Compiling the Stubs...............................................................................13
6.2 Starting the Server.................................................................................13
6.3 Running the Client.................................................................................13
7. Alternatives and Discussion ..............................................................................15
7.1 Example services ....................................................................................17
7.2 Client-side stub and client coding.........................................................17
7.3 Result.......................................................................................................18
7.3.1 Costs associated directly with communications protocols ............18
7.3.2 Costs of document transfer ..............................................................20
7.3.3 Common implementation for Java RMI, SOAP, and CORBA ....20
8. Conclusion ..........................................................................................................26
9. References...........................................................................................................27
2/27
Developing Calculator application with RMI Edition
1. Introduction
This paper is divided into three sections. Part I starts with an introduction to the
core infrastructure and the basic features of RMI . Part II builds on the first by drilling
down on the underlying technologies ,building a RMI application and discusses the
necessary steps when building a simple RMI application.. Part III compares three
distributed object systems that they consist of SOAP,CORBA and RMI.
RMI is designed to make communication between two Java programs, running in
separate JVMs, as much like making a method call inside a single process as possible.
RMI relies on two similar types of objects that are automatically generated by the
RMI Compiler from an implementation of the server: stubs and skeletons. A stub is a
client-side object that represents a single server object inside the client's JVM. It
implements the same methods as the server object, maintains a socket connection to
the server object's JVM automatically and is responsible for marshalling and
demarshalling data on the client side. A skeleton is a server-side object responsible for
maintaining network connections and marshalling and demarshalling data on the
server side.
The basic procedure a client uses to communicate with a server is as follows:
1. The client obtains an instance of the stub class. The stub class is automatically
pregenerated from the target server class and implements all the methods that the
server class implements.
2. The client calls a method on the stub. The method call is actually the same
method call the client would make on the server object if both objects resided in the
same JVM.
3. Internally, the stub either creates a socket connection to the skeleton on the
server or reuses a pre-existing connection. It marshalls all the information associated
to the method call, including the name of the method and the arguments, and sends
this information over the socket connection to the skeleton.
4. The skeleton demarshalls the data and makes the method call on the actual server
object. It gets a return value back from the actual server object, marshalls the return
value, and sends it over the wire to the stub.
5. The stub demarshalls the return value and returns it to the client code.
3/27
Developing Calculator application with RMI Edition
By the way , there is also a lot of RMI information available on the Internet.
Three of the best general-purpose RMI resources are:
1. Javasoft's RMI home page
This is the place to obtain the most recent information about RMI. It also
contains links to other pages containing RMI information from Javasoft. The URL is
http://java.sun.com/products/jdk/rmi/.
2. The RMI trail from the Java Tutorial
The Java Tutorial is a very good way to get your feet wet on almost any Java
topic. The RMI sections are based at
http://java.sun.com/docs/books/tutorial/rmi/index.html.
3. The RMI Users mailing list
The RMI users mailing list is a small mailing list hosted by Javasoft. All levels,
4/27
Developing Calculator application with RMI Edition
from beginner to advanced, are discussed here, and many of the world's best RMI
programmers will contribute to the discussion if you ask an interesting enough
question.The archives of the mailing list are stored at
http://archives.java.sun.com/archives/rmi-users.html.
2. Objective
Understand the structure of RMI in depth and use RMI to develop an example
application to describe the development process for creating distributed, object-based
applications for Java. Learn the process of middleware development, and understand
the RMI infrastructure and how it works, for example, suppose an object on
140.113.73.48 has a query method that looks up information in a local database. The
query method would be exported in a remote interface. The client on localhost would
look up the object in 140.113.73.48 registry, then call the query method, just as it
would call a query method in an object on localhost. The object that queries the
database runs on the server, but it accepts arguments from and returns results to the
client on localhost. This is simpler than designing and implementing a new
socket-based protocol for communication between the database server and its client.
The details of making the connections between the hosts and transferring the data are
hidden in the RMI classes.
3.2 Software
Java 2, using JDK1.5.0, Java RMI 1.2 and later, the skeleton layer is omitted.
Eclipse for designing java programs.
4. Approach (Design)
The project is to create a simple calculator system that performs the functionality
of a remote calculator service. There is a single client and a single server. The server
provides a set of arithmetic methods include add, subtract, multiply, divide,log and
exp. These can be remotely invoked by the client. Therefore, the server receives a
5/27
Developing Calculator application with RMI Edition
request from the client, performs the arithmetic operation and then returns the result
back to the client. In addition , we provider a formula service to Calculate Quadratic
equation and Simultaneous Linear Equations in 2 unknown. When the client needs to
Calculate Quadratic equation and Simultaneous Linear Equations in 2 unknown , the
client calls the formula service , and when the formula service must arithmetic
operation , it will call arithmetic service.
5. Implementation
Most of the methods you need for working with remote objects are in three
packages: java.rmi , java.rmi.server , and java.rmi.registry. The java.rmi package
defines the classes, interfaces, and exceptions that will be seen on the client side. You
need these when you're writing programs that access remote objects but are not
themselves remote objects. The java.rmi.server package defines the classes, interfaces,
and exceptions that will be visible on the server side. You use these classes when you
are writing a remote object that will be called by clients. The java.rmi.registry
package defines the classes, interfaces, and exceptions that are used to locate and
name remote objects.
The RMI system must be composed of the following parts:
1. The Server Side
2. The Client Side
3. Compiling the Stubs
4. Starting the Server
5. Running the Client
6/27
Developing Calculator application with RMI Edition
Program 1: iCalculator
public interface iCalculator extends java.rmi.Remote {
public double add(double a, double b)throws java.rmi.RemoteException;
public double sub(double a, double b)throws java.rmi.RemoteException;
public double mul(double a, double b)throws java.rmi.RemoteException;
public double div(double a, double b)throws java.rmi.RemoteException;
public double exp(double a)throws java.rmi.RemoteException;
public double log(double a)throws java.rmi.RemoteException;
public double sqrt(double a)throws java.rmi.RemoteException;
}
Program2:iFunction
public interface iFunction extends java.rmi.Remote {
//給定abc, 令ax^2+bx+c=0, 求x之值
public double[] quadratic(double first, double second, double third)
throws java.rmi.RemoteException;
//給定abc, 令ax+by+c=0, 求x,y之值
public double[] linear(double first1, double mid1, double last1,
double first2, double mid2, double last2)throws java.rmi.RemoteException;
}
7/27
Developing Calculator application with RMI Edition
Program 3: CalculatorImpl
import java.rmi.Naming;
public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements iCalculator {
// Implementations must have an
//explicit constructor
// in order to declare the
//RemoteException exception
public CalculatorImpl()throws java.rmi.RemoteException {
super();
}
public double add(double a, double b)throws java.rmi.RemoteException {
return a + b;
}
public double sub(double a, double b)throws java.rmi.RemoteException {
return a - b;
}
public double mul(double a, double b)throws java.rmi.RemoteException {
return a * b;
}
public double div(double a, double b)throws java.rmi.RemoteException {
return a / b;
}
public double exp(double a)throws java.rmi.RemoteException{
return Math.exp(a);
}
public double log(double a)throws java.rmi.RemoteException{
return Math.log(a);
}
public double sqrt(double a)throws java.rmi.RemoteException{
return Math.sqrt(a);
}
}
8/27
Developing Calculator application with RMI Edition
Program4: FunctionImpl
import java.rmi.Naming;
public class FunctionImpl extends java.rmi.server.UnicastRemoteObject implements iFunction {
private iCalculator calc;
double[] ans; //求出的解
private double a;//宣告public final,可被外部讀取,但不能更動。
private double b;
private double c;
private double b2;
private double ac4;
public FunctionImpl()throws java.rmi.RemoteException {
super();
try {
calc = new CalculatorImpl();
Naming.rebind("rmi://localhost/CalculatorService", calc); //呼叫Calculator的服務
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public double[] quadratic(double first, double second, double third)throws java.rmi.RemoteException
{
ans = new double[2];
//以下加、減、乘、除、開根號等計算,皆是呼叫Calculator的服務來計算
a = first;
b = second;
c = third;
ans[0] = calc.div(calc.add(-b, calc.sqrt(calc.sub(calc.mul(b, b), calc.mul(4,calc.mul(a,c))))) ,
calc.mul(2, a));
ans[1] = calc.div(calc.sub(-b, calc.sqrt(calc.sub(calc.mul(b, b), calc.mul(4,calc.mul(a,c))))) ,
calc.mul(2, a));
eturn ans;
}
public double[] linear(double first1, double mid1, double last1,
double first2, double mid2, double last2)throws
java.rmi.RemoteException{
ans = new double[2];
a = first1;
b = mid1;
9/27
Developing Calculator application with RMI Edition
c = last1;
double d = first2;
double e = mid2;
double f = last2;
ans[0] = (c*e-f*b)/(a*e-d*b);
ans[1] = (d*c-a*f)/(d*b-a*e);
return ans;
}
}
Program 5: FunctionServer
import java.rmi.Naming;
public class FunctionServer {
public FunctionServer() {
try {
iFunction f = new FunctionImpl();
Naming.rebind("rmi://localhost/FunctionService", f);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new FunctionServer();
}
}
10/27
Developing Calculator application with RMI Edition
Program 6: CalculatorServer
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
iCalculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost/CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer();
}
}
Program 7: RMIGUI
...
public class RMIGUI extends JFrame implements ActionListener{
private iFunction function;
private iCalculator cal;
...
//RMIGUI's Constructor
public RMIGUI() {
super("RMI Application");
...
//===============call the service=================
try {
function = (iFunction)Naming.lookup("rmi://localhost/FunctionService");
11/27
Developing Calculator application with RMI Edition
cal = (iCalculator)Naming.lookup("rmi://localhost/CalculatorService");
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
...
public void actionPerformed(ActionEvent evt){
//use function and cal the call the service from
CalculatorServer and FunctionServer, respectively
}
public static void main(String[] args) {
RMIGUI frame = new RMIGUI();
frame.setSize(340, 350);
frame.setResizable(false);
frame.setTitle("RMI DEMO PROGRAM");
frame.setVisible(true);
}
}
12/27
Developing Calculator application with RMI Edition
6. Results (Execution)
6.1 Compiling the Stubs
Before your server can begin accepting invocations, you must generate the stubs
and skeletons that the program requires. We have already discussed what the
stubs and skeletons do: the stub contains the information in the Remote interface
, and a skeleton is similar but on the server side. Fortunately, we don't have to
write them ourselves: they can be generated automatically from the remote
object's Java source code, using a utility called rmic included with the JDK. To
generate the stubs and skeletons for the FunctionImpl and CalculatorImpl remote
object, run rmic on the remote object's class.
%javac iCalculator.java
% javac iFunction.java
%javac –classpath . CalculatorImpl.java
%javac –classpath . FunctionImpl.java
%rmic –classpath . CalculatorImpl
%rmic –classpath . FunctionImpl
13/27
Developing Calculator application with RMI Edition
14/27
Developing Calculator application with RMI Edition
That's it; you have created a working RMI system. Even though you ran the three
consoles on the same computer, RMI uses your network stack and TCP/IP to
communicate between the three separate JVMs. This is a full-fledged RMI system.
15/27
Developing Calculator application with RMI Edition
Figure 7.1. Generation of client and server components from interface for
Java-RMI, SOAP, and CORBA
16/27
Developing Calculator application with RMI Edition
17/27
Developing Calculator application with RMI Edition
be able to download the WSDL from the registry. The Tomcat server
configuration for a JAXRPC implementation can act as an alternative source of
the WSDL. Both .Net and JAXRPC development systems include helper
applications that can generate a client-side stub class, and other helper classes,
from a downloaded WSDL definition.
A CORBA developer has to obtain a copy of the IDL interface definition (this
too could come from a UDDI registry for these are not restricted to handling only
WSDL defined services). The developer then generates a client stub in the
required implementation language via an IDL compiler.
Each test client had code that established a connection and then in a loop
repeatedly invoked service operations. Such tightly coded loops result in client
behavior that is much more demanding than atypical real-world application.
Client application code is essentially identical for all technologies. All
implementations will work with an object reference of the interface type; most of
the code will simply invoke operations via this reference.
A Java-RMI implementation will require a principal server-side object whose
identity is published via the rmiregistry naming service. This could be the
singleton object in a singleton stateless server, or a factory object for a stateful
service. The client obtains a reference via a lookup operation on the naming
service. The underlying Java-RMI runtime arranges to download the stub class
code from an HTTP server as part of the lookup process.
The SOAP uses JAXRPC code to utilize an instance of an auto generated
helper-class to create a stub object (an instance of an application-specific
subclass of a generic Stub class). The end-point URL should be encoded in the
generated stub, but can be overwritten.
A CORBA client might obtain a reference to the service for its stub from a
CosNaming name service, or from a trader, or from a stringified IOR in a file in
a shared file space.
7.3 Result
7.3.1 Costs associated directly with communications
protocols
The typical illustrative Web Service application has a client connect to a
service, submit a single request for data, and terminate. Such applications
are unlikely to put any great demands on either a server host's CPU, or a
communications network; consequently, performance issues are not that
important. In any case, for such applications, Web Services technologies
18/27
Developing Calculator application with RMI Edition
perform well in comparison with the distributed object systems. The data
shown in Table 7.2, Figure 7.2, and Figure 7.3 show relative performances
for three implementations of the “calculator” application with a request for
a single data structure retrieved according to an argument key.
Table 7.2. Costs of single shot request using various technologies
Data Transferred in Number of Connect
Technology Time (Seconds)
Bytes Packets
RMI (Java SE 1.5.0) 0.1829 2885.4 32.1
SOA (AXIS2) 0.6906 2744.9 2
CORBA (VisiBroker) 1.0905 5002 9
6000 35
32.1
5002 30
5000
25
4000
20
2885.4
3000 2744.9
15
2000
10
9
1000
5
2
0 0
CORBA (VisiBroker) RMI (Java SE 1.5.0) SOA (AXIS2)
Data Transferred in Bytes
Middleware Technology
Number of Connect Packet
1.2
1.0905
0.8
0.6906
Time (Seconds)
0.6
0.4
0.1829
0.2
0
CORBA (VisiBroker) RMI (Java SE 1.5.0) SOA (AXIS2)
Middleware Technology
19/27
Developing Calculator application with RMI Edition
Table 7.3. Traffic analysis for illustrative applications with different technologies
Data Transferred in Number of Connect
Technology Time
Bytes Packet
RMI (Java SE 1.5.0) 0.817 272128.2 4799.6
SOA (AXIS2) 10.9765 1572026.2 14877.7
CORBA (VisiBroker) 0.7579 276299.5 4013.1
1800000 16000
14877.7
1572026.2
1600000 14000
1400000
12000
1200000
10000
1000000
8000
800000
6000
600000
4799.6
4013.1 4000
400000
276299.5 272128.2
200000 2000
0 0
CORBA (VisiBroker) RMI (Java SE 1.5.0) SOA (AXIS2)
Data Transferred in Bytes
Middleware Technology
Number of Connect Packet
20/27
Developing Calculator application with RMI Edition
12
10.9765
10
8
Time (Seconds)
2
0.7579 0.817
0
CORBA (VisiBroker) RMI (Java SE 1.5.0) SOA (AXIS2)
Middleware Technology
21/27
Developing Calculator application with RMI Edition
22/27
Developing Calculator application with RMI Edition
23/27
Developing Calculator application with RMI Edition
for(int i=0;i<1000;i++){
AddResponse response = stub.add(request);
System.out.println("Calculator: " + response.get_return());
}
System.out.println("Time: "+(System.currentTimeMillis()-start));
result=Execute();
result=result.substring(result.indexOf("Bytes")+5).trim();
Receiving=Double.parseDouble(result.substring(0,result.indexOf(" ")));
result=result.substring(result.indexOf(" ")).trim();
Sending=Double.parseDouble(result.substring(0,result.indexOf("\n")-1));
System.out.println("Networking: "+(Receiving-Received)+"\\"+(Sending-Sent));
result=result.substring(result.indexOf("Unicast packets")+15).trim();
Receiving2=Double.parseDouble(result.substring(0,result.indexOf(" ")));
result=result.substring(result.indexOf(" ")).trim();
Sending2=Double.parseDouble(result.substring(0,result.indexOf("\n")-1));
System.out.println("Packets: "+(Receiving2-Received2)+"\\"+(Sending2-Sent2));
}
}
24/27
Developing Calculator application with RMI Edition
25/27
Developing Calculator application with RMI Edition
System.out.println("Packets: "+(Receiving2-Received2)+"\\"+(Sending2-Sent2));
}
}
8. Conclusion
RMI lets Java objects on different hosts communicate with each other in a way
that's similar to how objects running in the same virtual machine communicate with
each other: by calling methods in objects. A remote object lives on a server. Each
remote object implements a remote interface that specifies which of its methods can
be invoked by clients. Clients invoke the methods of the remote object almost exactly
as they invoke local methods. For example, an object running on a local client can
pass a database query as a String argument to a method in a database object running
on a remote server to ask it to sum up a series of records. The server can return the
result to the client as a double.
In this paper, web design and implement the “calculator” example using RMI
which shows in Session 5 and Session 6. We know how to use RMI to build services
automatically. However, in recently, there are many kind of middleware which
include RMI, SOAP, CORBA, COM, DCOM, and so on, and those difference
26/27
Developing Calculator application with RMI Edition
9. References
[1] David Reilly, Michael Reilly, “Java™ Network Programming and Distributed
Computing”, Addison Wesley, 2002.
[2] Elliotte Rusty Harold, “Java Network Programming, 3rd Edition”, O'Reilly, 2004.
[3] František Plášil, Michael Stal, “An Architectural View of Distributed Objects
and Components in CORBA, Java RMI, and COM/DCOM”, Asubmission to
Software Concepts & Tools, 1998.
[4] Jan Graba, “An Introduction to Network Programming with Java”, Springer,
2007.
27/27