Tuesday, August 31, 2010

Remote method Invocation (RMI)

Remote method invocation is used to invoke methods across the networks...... The objects have to be registered in the rmi-registry..... Its like the yellow pages where all the entries are registered. From the Rmi- Registry we can access the objects but the service name has to be known to access the objects.


Remote Interface :


import java.rmi.Remote;
import java.rmi.RemoteException;

public interface remote extends Remote {
    int getSum(int a,int b) throws RemoteException;
}

Server :




import java.rmi.*;
import java.rmi.server.*;

public class server extends UnicastRemoteObject
                           implements remote
{
    public server() throws RemoteException {
        super();
    }

    public int getSum(int a,int b) {
return a+b;
    }

    public static void main(String[] args) {
      
        try {
            server engine = new server();
            Naming.rebind("samplermi", engine);
            System.out.println("Server bound");
        } catch (Exception e) {
            System.err.println("server exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Naming.rebind() method puts the engine object in the name "samplermi".

Client :

// Java Document

import java.rmi.*;

public class client {
    public static void main(String args[]) {
try {
            remote obj = (remote) Naming.lookup("samplermi");
            System.out.println(obj.getSum(10,20));
        } catch (Exception e) {
            System.err.println("client exception: " + e.getMessage());
            e.printStackTrace();
        }
    }  
}

The client access the remote object using the "samplermi" service name.

1 comment:

  1. I'm 15 years old. I was born with HIV my mother passed away because of the HIV infection And I regret why i never met Dr Itua he could have cured my mum for me because as a single mother it was very hard for my mother I came across Dr itua healing words online about how he cure different disease in different races diseases like HIV/Aids Herpes Copd Diabetes Hepatitis even Cancer I was so excited but frighten at same time because I haven't come across such thing article online then I contacted Dr Itua on Mail drituaherbalcenter@gmail.com I also chat with him on what's app +2348149277967 he tells me how it works then I tell him I want to proceed I paid him so swiftly Colorado post office I receive my herbal medicine within 4/5 working days he gave me guild lines to follow and here am I living healthy again can imagine how god use men to manifest his works am I writing in all articles online to spread the god work of Dr Itua Herbal Medicine Yes can ask me anything about this on my twitter @ericamilli or text 7205992850.He's a Great Man.

    ReplyDelete