Distributed Object System: Java - Remote Method Invocation (RMI) Remote Method Invocation (RMI)
Distributed Object System: Java - Remote Method Invocation (RMI) Remote Method Invocation (RMI)
Tutorials
Core Java
Advanced Java
Programms
Projects
Downloads
Contact
Java
Programmin
g
Share26
Java - Remote Method Invocation (RMI)
JAVA Provides us a great Feature to Call the Methods those are Made in JAVA and those are
Stored on the Remote or other Computer in the Network. And With the Help of JAVA Program
We can Call them. So the Main Aim of RMI or Remote Method Invocation is to Call the Method
which is Stored on any Remote Computer.
There are number of methods for developing the network application such as COM/CDCOM
CORBA and RMI with their own advantages and disadvantages. These all application just works
on the concept of distributed object system technology.
Locating and loading of remote classes are needed by ordinary java applets. They contain
references of classes those are download by the browser from several hosts. Those classes
can provide the functionality to system developers for improving their system
performance by virtually using the resources of these classes. For Calling Methods in the
Network we have to Pass Some Arguments. The Passed Arguments must be Transferred
to Server Arguments and the Parameters those are Passed are Stored in the Proxy Object
which is called as stub . Stubs are always Created in Client Machine . So the Process of
Storing the Parameters in Stub Object is also Called as Parameter Marshalling. The Main
Purpose Marshalling to Convert the Arguments in a Forma So that they can be
Transferred to any Java Virtual Machine. And Stub always Contains Parameters those are
Marshaled. When a Client Pass those Agreements by Creating Stubs then the Parameters
are Stored on the Skelton Object . Skelton Objects are always Created in the Server Side.
When a Server Receive Request from a Client then he Performs :-
For locating and providing the reference of remote objects there are must be some sort of
catalog or either the database objects and a serer that will provide the access to those
catalogs. Whenever a program need a service then it first ask from the catalog server that
whether the service is provide to it by using the server object or not . we can't sort a
reference to a object simply on a network, for this garbage collection is used which tells
us whether remote reference to an object exist or not. An object must not send references
whenever a remote computer even the location is out using this uses it. So that before
Locating any Remote Object that Remote Object must be Checked whether it is
Available or not So that a information Must be Contained in both the Client and Server.
Means a Client Must Knows that Method is Exists on the Server and Server also
Maintains that all the Required information is Send by Client is Successfully Reached
and a Server Contains that Requested Method. Whenever we wants to Make a Method as
a Remote then The Server Class or a Method which we wants to being Available to all
the other Client Must Extends the Class Remote. And also throws Exception known as
Remote Exception Which is Occurred because Possibility of Network Failure. And also
Contains an Object of UnicastRemoteObject Class which will act as the server interface
or which Provides the Methods to the Clients. And also use the java.rmi Package which
contains all the Required Classes.
This method requires a mechanism for obtaining a reference to a target method and then
passing the argument and return values from or across the network.
In the distributed if there is an occurrence of any problem then the application is need not
shutdown. But it simply advice the user that your request is not available. But in the
standalone application this facility is less commonly helpful in the application.
RMI Architecture
The RMI architecture of RMI consists of four layers: -
1. Application.
2. Proxy Layer or Stub-Skeleton.
3. Remote Reference.
4. Transport Layer.
Any application that wants to make some of its methods available in the remote client
then those are first declared in an instance that extends java.rmi.remote and all these
methods are declared as public in which these are implemented. But remember at the
time of creating interface it doesn't define the methods in the interface. The entire remote
object must extend the UnicastRemoteObject class, which provides functionality that is,
need to make objects available from remote machine. These classes belong to the
package java.rmi.Server. Then the server normally registers itself with a name server or
registry. The client first users this registry or name servers to make first contact with the
server and obtain a reference to its remote objects.
2. Proxy Layer
RMIC stubs , RMI compiler for client-side and server-side representation of remote
object generates the stub and skeleton classes. A program that is running on the client
machine uses stub classes for marshalling the information from client to remote object
means it is used for transport the parameter exceptions and errors and it is also
responsible for returning the result from the remote object. It first marshals object
information to byte stream and then it send it over the network and then it is de-
marshalled on the server-side proxy of remote object. The stub always receives a
reference from its class for a remote object from the remote reference layer. It uses the
RRL for communicating with a skeleton on the server-side of the skeleton class.
Stub Class
The stub is a client-side proxy. Stubs define all of the interfaces that the remote object
implementation supports. A program running on the client machine references the stub
the same way that it references any other local object.
The stub is responsible for making a call to a remote object as well as for marshaling
information to and from a remote object. A stub receives a reference to a remote object
from the Remote Reference Layer. A stub uses a marshal stream for communication with
a skeleton on the server side. A marshal stream is also acquired through the Remote
Reference Layer.
Skeleton Class
The skeleton is the server-side proxy that interfaces with the server RRL. It received the
request of calling the remote method from the client and then it de-marshalled the
arguments that are sending to a remote method. This class stays on the server-side
connection and directly deals with the implemented classes of Remote methods. It is
responsible for sending the parameters to the method and then sending the results or any
error back to the client. The skeleton first de-marshal information send by stub and then it
marshal information, which is being sent back to the s/w.
In this layer it receives the information from the stub class and then send to the skeleton
class. It is used for setting the connection from the transport layer. The actual transport
layer may take place by using a non-connection based but they will represent their
communication as a connection-based.
4. Transport Layer
1. For using the Registry Service first is to look-up the name of server on the well-
defined port-number.
2. The second is running its own registry services but by specifying the port number
we can registers the remote objects in the registry table. Then we can call hem by using
the rmi registry. Registry interface or from rmi naming class.
RMI uses the first method to maintain a registry. The registry keeps track of the addresses of all
the remote objects that are being exported by their applications. All objects are assigned unique
names that are used to identify them. Applications can add, remove, and access remote objects in
the registry's table of objects by calling certain methods from the rmi.registry.Registry interface,
or from the rmi.Naming class.
To create an application that uses RMI, one needs to use the classes and interfaces defined by
the java.rmi package.
5. Create and Compile the Client Program to Access the Remote Objects
To implement the RMI bases client/server application, an interface is created that contains all the
methods that the remote object must support. All interfaces for remote objects must extend the
Remote interface defined in java.rmi package. All the methods in those interfaces must include a
throws RemoteException statement. The reason is that remote method calls are inherently less
reliable that local calls. It is always possible that the remote calls fails. The server or the network
connection may be temporarily unavailable. The client code must be prepared to deal with this.
For these reasons, Java forces the programmer to catch the RemoteException with every remote
method call and to specify the appropriate action to be taken when the call doen not succeed.
Example:
The following contains a simple interface that can be used with a remote object
The above example creates an interface which defines getSqrt() method which will be invoked
by the client.
Example:
A security manager on a server prevents random clients from connection and making
unauthorized calls. This is done by configuring the security manager to allow or disallow various
operations. The java class library includes a class called RMISecurityManager, which can be
used for this purpose.
Any Server application, which wishes to make remote objects available to client applications,
must first register those objects with a registry service. For an application to bind or register an
object with the registry, both the registry service as well as the application must be running on
the same machine.
An application can bind any object to the registry, provided that object either extends the
UnicastRemoteObject class, or extends a Remote interface.
A client application invokes a remote object by using the name with which that object is
registered with the registry service. As a result no two objects can be registered with the same
name.
The java.rmi.Naming class has provided two methods for binding objects to a registry service.
These two methods are - bind() and rebind()
The bind() Method()
Syntax: java.rmi.Naming.bind(URL,Ref)
The bind() method will throw an exception if it tries to bind a name that already exists in the
registy service.
Example:
The following block of code attempts to bind an object of "RMIServer" class with a registry
service running on a computer whose URL is "www.yourservername.com" under the name
"RefsctEmp".
Syntax: java.rmi.Naming.rebind(URL,Ref)
This method takes the same two parameters as the bind() method.
» URL specifies the location of the registry along with the name that objects will
be registered as.
» Ref is a reference to the object that is being registered.
The rebind() method differs from the bind() method in that it will not throw an
AlreadyBoundException exception. The rebind() method will simple replace any previously
registered objects possessing the same name, with the current object.
Example:
The following example contains a server application that implements the MethodImpl interface
and binds the remote methods with the registry.
C:\>javac MethodImpl.java
The server application returns the square root of number to the remote client.
Compile the RMIServer application using the javac compiler.
rmic RMIServer
Create and Compile the Client Program to Access the Remote Objects
The client is implemented to access the remote objects created to use the remote interface and
calls the methods from that interface.
A stub as seen earlier is a Java object that resides on the client machine. It's function is to present
the same interface as the remote server. Remote method calls are actually directed to the stub.
The stub workds with the other parts of the RMI system to formulate a request that is sent to the
remote machine.
A remote machine may accept the arguments that are simple types or objects. In the latter case,
the object may have references to other objects also. All of this information must be send to the
remote machine.
A skeleton as seen earlier in Java object that resides on the server machine. It works with other
parts of the RMI system to receive requests and invoke the appropriate code on the server.
Syntax: java.rmi.Naming.lookup(URL)
This method takes a URL as its parameter. The URL points to the registy and the object that the
client application requires. If the lookup() method finds the object, it will return a reference to
that object else it may throw a NotBoundException.
Example:
The following example creates a client application that makes a reference to the remote object
using lookup() method.
The client application receives the square root of the number passed.
The lookup() methods returns an object of type java.rmi.Remote and hence must always be cast
to the type of object the client application (in this case, MethodImpl).
Incase the Server Application and Client Application is run on separate machines then:
1. Copy RMIClient.class, MethodImp.class, RMIServer_stub.class to a directory
on the client machine.
2. Copy RMIServer_stub.class, RMIServer_skel.class, MethodImpl.class to a
directory on the server_machine.
Sun Microsystems has provided an RMI registry application (rmiregistry) as part of the JDK.
The RMI registry application is normally started as a background application that will by default
run on port 1099. On a Windows machine, by issuing the following command at the DOS
prompt: the RMI registry application can be started.
The <port number> argument is optional. By default the rmiregisty will run on port 1099. This
command will load and execute rmiregistry as a TSR application in Windows. To run the RMI
registry in a separate DOS shell, the registry can be started with the DOS "start" command. For
example
C:>start rmiregistry
The above statement start the registry service at the default port i.e. 1099. and will show new
window as shown below.
Creating A Model For An Air-line Reservation Using RMI
Related Lyrics
Sockets of UDP
1 comment:
1.
venkatesh@CSJune 13, 2019 at 9:51 PM
It is very good blog and useful for students and developers.
Java Training in OMR
Reply
Share26
--Advertisement Here--
Translate
Select Language ▼
Advance Java ProgrammingCore Java Programming SwingClass Program AppletJFrame Java DatabaseJPanel Event
Listener Java Servlets Java LoopsBranching ProgrammingFile
Handling GUI Programming UserInput Program Exception HandlingGraphics
ProgrammingMultiThreadingConstructor/FinalizeInheritance Java Images ArrayInterface Java BeansM.S.Access Socket
Programming String FrameJApplet JOptionPane Jumping in Loops Package RMI SQLVectors AWT Buttons AWT CheckBoxes AWT Choice Controls AWT Labels AWT Radio
Buttons AWT ScrollBarsAWT Scrolling Lists AWT TextAreas AWT
TextFields DSNInputDialogBoxes JButtonsJCheckBox JComboBoxJDesktopPane JEditorPaneJFileChooser JLabel JListJProgressBar JRadioButtonsJScrollBar JScrollPane JSliderJSplitPane JT
able JTextAreaJTextField & JPasswordFieldJTextPane JToolBar JTree Java Programming Java Simple Program Login Method Overloading Tables Update Records
Useful Links
NusratSahib.Com
C Programming
C++ Programming
C# Programming
ASP Programming
Visitors
464172
--Advertisement Here--
Java Tutorials
Section 1
Home
Introduction To Java
Java Evolution
Overview of Java
Branching Statements
Looping Statements
Section 2
Inheritance
Java Packages
Java AWT
Java MultiThreading
Java Swing-I
Java Swing-II
Section 2
Java RMI
Java Servlets
Java Beans
Advertisement
Follow by Email
Submit
Google+ Followers
Follow Advanced Java Popular Programs Follow Advanced Java
Programming on Programming on Twitter
Facebook
Java Graphics Programming
Home » Java » Java
Graphics Programming
Graphics is one of the most
important features of Java. Java
app...
Java Applet Programming
Home » Java » Java
Applet Programming Applets
are small applications that are
accessed from an internet ser...
Getting Input from the User in
Java Applet
Applets work in graphical
environment. Therefore applets
treat input as text strings. We
must first create an area of the
screen in which ...
Java AWT (Abstract Window
ToolKit)
Home » Java » Advance
Java » Java AWT (Abstract
Window ToolKit) In Today's
World the Most im...
Display Numerical Values in
Applet
In applets, we can display
numerical values by first
converting them into strings
and then using the drawstring()
method of Graphics class...
Powered by Java Programming © 2018