java - 如果我没有显式调用 ReceiverAdapter 类中的重写方法(例如 jgroups 中的 viewAccepted() 方法),它们是如何被调用的?

标签 java inheritance jboss jgroups

我编写了以下类来创建 JGroup 集群:

public class TestClient extends ReceiverAdapter {
JChannel channel;

private void start() throws Exception {
    channel=new JChannel().setReceiver(this);
    channel.connect("ChatCluster");
    eventLoop();
    channel.close();
}

private void eventLoop() {
    while(true) {
    }
}

public void viewAccepted(View new_view) {
    System.out.println("** view: " + new_view);
    System.out.println("Get Coord"+new_view.getCoord());
    System.out.println(new_view.getMembers());
}

public void receive(Message msg) {
    System.out.println(msg.getSrc() + ": " + msg.getObject());

}

public void getState(OutputStream output) throws Exception {

}

public void setState(InputStream input) throws Exception {

}
}

ReceiverAdapter 是 Jgroups 定义的类:

public class ReceiverAdapter implements Receiver {
public ReceiverAdapter() {
}

public void receive(Message msg) {
}

public void receive(MessageBatch batch) {
    Iterator var2 = batch.iterator();

    while(var2.hasNext()) {
        Message msg = (Message)var2.next();

        try {
            this.receive(msg);
        } catch (Throwable var5) {
            ;
        }
    }

}

public void getState(OutputStream output) throws Exception {
}

public void setState(InputStream input) throws Exception {
}

public void viewAccepted(View view) {
}

public void suspect(Address mbr) {
}

public void block() {
}

public void unblock() {
}
}

我的问题是如何在 View 更改或发送/接收消息时调用 ReceiverAdapter 类中的这些方法,因为我不需要显式调用这些方法。 JGroups 是否实现了某种事件监听器?

最佳答案

如果我正确理解你的问题,我认为你已经解决了你的问题。您需要将 Receiver 实现显式传递到您希望使用它的 JChannel:

Receiver receiver = new MyReceiverImplementation();
JChannel jChannel = new JChannel().connect("clusterName").receiver(receiver);

JGroups 不会自动寻找要注入(inject)的接收器。

关于java - 如果我没有显式调用 ReceiverAdapter 类中的重写方法(例如 jgroups 中的 viewAccepted() 方法),它们是如何被调用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51859474/

相关文章:

java - 通过反射获取int[]的值

java - 我如何从另一个类(class)获得类(class)的值(value)

jquery - CSS继承问题。请帮助CSS。

c# - ScrollViewer:当 ComputedHorizo​​ntalScrollBarVisibility 改变时通知

java - 这是 JAVA_OPT JVM 标志吗?

java - Android 版 openCV - 集成问题

java - 为什么所有 opengl 枚举都是十六进制的?

c++ - 我可以让静态函数继承每个类的特定值吗?

xml - Web.xml:url-pattern 标签是相互关联的吗?

java - 为什么没有服务器端套接字?