java 简单的 RMI 回调示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10437408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple RMI Callback Example
提问by BinaryShrub
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.
有人可以给出一个简单的 Hello World 的 RMI 回调示例吗?我一直在尝试研究它,但似乎找不到我理解的。我不明白回调是/做什么。
This is my current Hello World RMI if it helps...
如果有帮助,这是我当前的 Hello World RMI...
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();
}
}
}
回答by Filipe Wolve
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!
我不是 RMI 方面的专家,但我可以告诉您的是,您可以从“David and Michael Reilley”中搜索“Java 网络编程和分布式计算”一书。您将能够找到从第 278 页开始的 RMI CALLBACK 实现的一个很好的例子!
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!
在默认实现中,RMI 只允许 CLIENT 与 SERVER 之间的通信,请求服务器主机中远程服务(远程对象)的操作。您可以使用回调方法而不是让您的服务器与您的客户端通话!
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 ;-)
这应该很有用,在这些情况下使用 CALLBACK ;-)
I hope you can understand with my answer a little bit about what callback is/does.
我希望你能通过我的回答稍微了解回调是/做什么。
Best regards,
最好的祝福,