我编写了以下类来创建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() {
}
}


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

最佳答案

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

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


JGroups不会自动寻找要插入的接收器。

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

10-11 20:33