在RMI(客户端代码)中,如何使用服务器端代码中定义的事件?
例如,以下服务器端代码定义了PropertyChangeSupport
事件。
如何在客户端实现它?
package rmiservice.services.calculator;
import java.beans.PropertyChangeSupport;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;
import java.util.Queue;
public class CalculatorService extends UnicastRemoteObject implements ICalculator {
private Queue<Integer> numbers = new LinkedList<Integer>();
private Integer result;
***private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);***
public CalculatorService() throws RemoteException {
super();
}
public void start() throws Exception {
java.rmi.registry.LocateRegistry.createRegistry(1099);
Naming.bind("CalculatorService", this);
System.out.println("Calculator Service is Run . . .");
}
public void stop() throws Exception {
Naming.unbind("CalculatorService");
UnicastRemoteObject.unexportObject(this, true);
System.out.println("Calculator Service is Stop . . .");
}
//-------------------------------------------------------------------
//------------------------------ Implements ICalculator -------------
public void addNumber(Integer number) throws Exception {
numbers.add(number);
}
public Integer getResult() throws Exception {
return this.result;
}
public void setResult(Integer result) throws Exception {
Integer oldResult = this.getResult();
this.result = result;
***propertyChangeSupport.firePropertyChange("result", oldResult, result);***
}
public void calculate(Operation operation) throws Exception {
Integer _result = 0;
if (numbers.size() < 2)
return;
switch (operation) {
case Add: {
_result = 0;
while (numbers.size() > 0) {
_result += numbers.poll();
}
break;
}
case Substract: {
_result = numbers.poll();
while (numbers.size() > 0) {
_result -= numbers.poll();
}
break;
}
}
this.setResult(_result);
}
//-------------------------------------------------------------------
}
最佳答案
RMI不支持通知。但是,您可以选择具有事件支持的JMX Bean,该支持可在RMI上使用。
为此,您的MBean接口必须扩展NotificationEmitter。
关于java - 如何在RMI(客户端代码)中使用服务器端代码中定义的事件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14826542/