Advanced programming, Lecture notes of Information Integration

Advanced programming prepared for the next

Typology: Lecture notes

2023/2024

Uploaded on 11/25/2024

tolerate-man
tolerate-man 🇪🇹

5 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5
RMI (Remote Method Invocation)
The RMI (Remote Method Invocation) is an API that provides a mechanism to
create distributed application in java. The RMI allows an object to invoke
methods on an object running in another JVM.
The RMI provides remote communication between the applications using two
objects stub and skeleton.
Understanding stub and skeleton
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM.
Let's understand the stub and skeleton objects:
stub
The stub is an object, acts as a gateway for the client side. All the outgoing
requests are routed through it. It resides at the client side and represents the
remote object. When the caller invokes method on the stub object, it does the
following tasks:
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the
incoming requests are routed through it. When the skeleton receives the
incoming request, it does the following tasks:
1. It reads the parameter for the remote method
2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.
Understanding requirements for the distributed applications
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Advanced programming and more Lecture notes Information Integration in PDF only on Docsity!

Chapter 5 RMI (Remote Method Invocation) The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM. The RMI provides remote communication between the applications using two objects stub and skeleton. Understanding stub and skeleton RMI uses stub and skeleton object for communication with the remote object. A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects: stub The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks:

  1. It initiates a connection with remote Virtual Machine (JVM),
  2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
  3. It waits for the result
  4. It reads (unmarshals) the return value or exception, and
  5. It finally, returns the value to the caller. skeleton The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:
  6. It reads the parameter for the remote method
  7. It invokes the method on the actual remote object, and
  8. It writes and transmits (marshals) the result to the caller. Understanding requirements for the distributed applications

If any application performs these tasks, it can be distributed application. .

  1. The application need to locate the remote method
  2. It need to provide the communication with the remote objects, and
  3. The application need to load the class definitions for the objects. The RMI application have all these features, so it is called the distributed application. Java RMI Example The is given the 6 steps to write the RMI program.
  4. Create the remote interface
  5. Provide the implementation of the remote interface
  6. Compile the implementation class and create the stub and skeleton objects using the rmic tool
  7. Start the registry service by rmiregistry tool
  8. Create and start the remote application
  9. Create and start the client application RMI Example In this example, we have followed all the 6 steps to create and run the rmi application. The client application need only two files, remote interface and client application. In the rmi application, both client and server interacts with the remote interface. The client application invokes methods on the proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy object and then to the client application.
  1. super();
  2. }
  3. public int add(int x,int y){return x+y;}
  4. }
  1. create the stub and skeleton objects using the rmic tool. Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects.
  1. rmic AdderRemote
  1. Start the registry service by the rmiregistry tool Now start the registry service by using the rmiregistry tool. If you don't specify the port number, it uses a default port number. In this example, we are using the port number 5000.
  1. rmiregistry 5000
  1. Create and run the server application Now rmi services need to be hosted in a server process. The Naming class provides methods to get and store the remote object. The Naming class provides 5 methods. public static java.rmi.Remote lookup(java.lang.String) throws java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException; It returns the reference of the remote object. public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException; It binds the remote object with the given name. public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException; It destroys the remote object which is bound with the given name. public static void rebind(java.lang.String, It binds the remote

java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException; object to the new name. public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException; It returns an array of the names of the remote objects bound in the registry. In this example, we are binding the remote object by the name sonoo.

  1. import java.rmi.*;
  2. import java.rmi.registry.*;
  3. public class MyServer{
  4. public static void main(String args[]){
  5. try{
  6. Adder stub=new AdderRemote();
  7. Naming.rebind("rmi://localhost:5000/sonoo",stub);
  8. }catch(Exception e){System.out.println(e);}
  9. }
  10. }
  1. Create and run the client application At the client we are getting the stub object by the lookup() method of the Naming class and invoking the method on this object. In this example, we are running the server and client applications, in the same machine so we are using localhost. If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located.
  1. import java.rmi.*;
  2. public class MyClient{
  3. public static void main(String args[]){
  4. try{

Meaningful example of RMI application with database Consider a scenario, there are two applications running in different machines. Let's say MachineA and MachineB, machineA is located in United States and MachineB in India. MachineB want to get list of all the customers of MachineA application. Let's develop the RMI application by following the steps. 1) Create the table First of all, we need to create the table in the database. Here, we are using Oracle10 database.

  1. Connection con=DriverManager.getConnection("jdbc:oracle:thin:@l ocalhost:1521:xe","system","oracle");
  2. PreparedStatement ps=con.prepareStatement("select * from custom er400");
  3. ResultSet rs=ps.executeQuery();
  4. while(rs.next()){
  5. Customer c=new Customer();
  6. c.setAcc_no(rs.getInt( 1 ));
  7. c.setFirstname(rs.getString( 2 ));
  8. c.setLastname(rs.getString( 3 ));
  9. c.setEmail(rs.getString( 4 ));
  10. c.setAmount(rs.getFloat( 5 ));
  11. list.add(c);
  12. }
  13. con.close();
  14. }catch(Exception e){System.out.println(e);}
  15. return list;
  16. }//end of getCustomers()
  17. }

_4) Compile the class rmic tool and start the registry service by rmiregistry tool

  1. Create and run the Server_ File: MyServer.java
  1. package com.javatpoint;
  2. import java.rmi.*;
  3. public class MyServer{
  4. public static void main(String args[])throws Exception{
  5. Remote r=new BankImpl();
  6. Naming.rebind("rmi://localhost:6666/javatpoint",r);
  7. }}

In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.