Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple RMI Callback Example

Tags:

java

callback

rmi

Can someone give a simple RMI Callback Example of Hello World? I have been trying to research it but I cant seem to find one that I understand. I don't understand what a callback is/does.

This is my current Hello World RMI if it helps...

Interface

package example.hello;

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

public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}

Client

package example.hello;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

    private Client(){}

    public static void main(String[] args){

        String host = (args.length < 1) ? null : args[0];

        try{
            Registry registry = LocateRegistry.getRegistry(host);
            Hello stub = (Hello) registry.lookup("Hello");
            String response = stub.sayHello();
            System.out.println("response: " + response);
        } catch (Exception e) {
            System.err.println("Client exception: " + e.toString());
            e.printStackTrace();
        }
    }

}

Server

package example.hello;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {

    public Server(){}

    @Override
    public String sayHello() {
        System.out.println("responded!");
        return "Hello, world!";
    }

    public static void main(String[] args) {

        try{
            Server obj = new Server();
            Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

            // Bind the remote object's stub in the registry
            Registry registry = LocateRegistry.getRegistry();
            registry.bind("Hello", stub);

            System.err.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }
}
like image 241
BinaryShrub Avatar asked May 03 '12 18:05

BinaryShrub


People also ask

What is RMI callback?

An RMI callback occurs when the client of one service passes an object that is the proxy for another service. The recipient can then call methods in the object it received, and be calling back (hence the name) to where it came from.

What is RMI give example?

RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM. RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.

Is RMI outdated?

Conclusion. RMI and CORBA(Common Object Request Broker Architecture) attempt to solve a problem that people thought was important in the early 1990s. However, today we can't consider RMI as modern technology and nowadays we have REST, SOAP as the de-facto standards for communicating with remote services.


1 Answers

I'm no expert in RMI but i can tell you is that you can search for the book "Java Network Programming and Distributed Computing" from "David and Michael Reilley". You will be able to find a great example of RMI CALLBACK implementation that starts in page 278!

The author defines a good way to understand it, so i tought it would be better to copy/paste than try to make my own, here it goes:

  • "The simplest way of understanding a callback is to think of a phone call. Suppose you want to know if a stock price hits a certain level, and you ask your broker to call back when it does. When the broker (the source of the event) notices that the stock price reflects your parameters, he or she calls you back, to notify you of the new price. That's a callback."

In default implementation, RMI just allows communication between CLIENT to the SERVER, requesting actions of remote services (remote objects) in the server host. You can use the callback method than to make your Server talk to your Client!

Thast's great! Imagine if you have ONE server that you wanna to check if it's online (or if didnt drop/shutdown) trought the client! You would have to request continuous use of a remote object that should return you some boolean value (for example) telling that's in fact online.

That would be horrible! Because you would loose some network bandwidth, requestin the server again, and again, and again... causing some connection pooling on it!

That's wy should be usefull, in these cases to use CALLBACK ;-)

I hope you can understand with my answer a little bit about what callback is/does.

Best regards,

like image 55
Filipe Wolve Avatar answered Oct 02 '22 01:10

Filipe Wolve