Simple RMI Callback Example
callback, java, rmi
Solution
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,
Problem
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(); } } } ```