gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:20:23 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com