java - 如何在 RMI(客户端代码)中使用在服务器端代码中定义的事件?

标签 java events rmi

在 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 接口(interface)必须扩展 NotificationEmitter为了做到这一点。

关于java - 如何在 RMI(客户端代码)中使用在服务器端代码中定义的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826542/

相关文章:

java - 找不到 RMI 注册表

java - spring-data-solr 数据导入处理器调用

java - 捕获错误请求异常 (400)

javascript - 鼠标进入/离开 Chrome/Firefox 的 Polyfill

vb.net - datagridview vb.net 中特定单元格的单击事件

java - 如何避免 Java RMI 的 MarshalException?

java - 是否可以实现 RMI 自动化?

java - javaFX 中的 observablelist 更新问题

java - 添加到 stringbuffer 逗号

c# - Windows Phone 8 检测键盘何时关闭