A simple RMI callback example

Can anyone give a simple RMI callback Hello World example? I tried to research it, but I can not find the one that I understand. I don't understand what the callback is doing.

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 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(); } } } 
+6
source share
1 answer

I am not an expert in RMI, but I can say that you can search for the book "Java Network Programming and Distributed Computing" by "David and Michael Reilley". You can find a great RMI CALLBACK implementation example that starts on page 278!

The author defines a good way to understand this, so I'd rather copy / paste than try to make my own, here it is:

  • "The easiest way to understand the callback is to think about a phone call. Suppose you want to know if the stock price has reached a certain level, and ask your broker to call back when this happens. When the broker (event source) notices that the stock price reflects your parameters , he or she calls you to notify you of a new price. This is a callback.

In the default implementation, RMI allows only communication between the CLIENT and SERVER, requesting the actions of remote services (remote objects) on the server host. You can use the callback method than to have your server access your client!

Thoroughly! Imagine if you have ONE server that you want to check if it is on the network (or if it did not fall / turn off), called the client! You will need to request continuous use of the remote object, which should return some logical value (for example) that actually speaks on the Internet.

That would be awful! As you lose some network bandwidth, ask the server again, and again, and again ... causing some connection pools on it!

This should be useful, in these cases use CALLBACK; -)

I hope you with my answer will understand a little what callback is.

Respectfully,

+11
source

Source: https://habr.com/ru/post/914835/


All Articles