3

我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序

import org.jgroups.*;
import org.jgroups.conf.ConfiguratorFactory;
import org.jgroups.conf.ProtocolConfiguration;
import org.jgroups.conf.ProtocolStackConfigurator;

import java.util.List;

/**
 * @author Sergii.Zagriichuk
 */
public class Test {
    public static void main(String[] args) throws Exception {
        JChannel ch = new JChannel();
        ch.setReceiver(new ReceiverAdapter() {
            public void receive(Message msg) {
                System.out.println("received message " + msg.getObject());
            }
        });
        ch.connect("one");
    }
}

还有这个

package com.datacradle.example;

import org.jgroups.Global;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.conf.ConfiguratorFactory;
import org.jgroups.conf.ProtocolConfiguration;
import org.jgroups.conf.ProtocolStackConfigurator;
import org.jgroups.stack.IpAddress;
import org.jgroups.util.SingletonAddress;
import org.jgroups.util.Util;

import java.util.List;

/**
 * @author Sergii.Zagriichuk
 */
public class Test {

    void start(String props) throws Exception {

        JChannel chanel = new JChannel();
        String line = "Test message";
        chanel.connect("one");
//        Message msg = new Message(null, null, new TestData(line, 1111, line + " Test suffix"));
                Message msg = new Message(new IpAddress("fe33:0:0:0:1986:ba23:d939:f226%12",55435) , null, new TestData(line,1111,line+" sdfasdfasdfasdfasdfa"));
        chanel.send(msg);
    }

    public static void main(final String[] args) throws Exception {
        new Test().start(null);
    }
}

所以,如果我使用这种风格来创建消息

 Message msg = new Message(null, null, new TestData(line, 1111, line + " Test suffix"));

我只会收到一条消息(这是针对当前组中的所有订阅者的),但是如果我使用这种样式

Message msg = new Message(new IpAddress("fe33:0:0:0:1986:ba23:d939:f226%12",55435) , null, new TestData(line,1111,line+" sdfasdfasdfasdfasdfa"));

我会在一个循环中收到很多消息(这是针对一个 dist 地址)有什么问题或者我应该添加一些额外的参数?

PS,JGroups 3.0.0 RC1

谢谢。

4

1 回答 1

3

您不应使用IpAddress 类创建成员地址,因为这是不透明的。我建议从视图中获取目标地址,例如

List<Address> members=channel.getView().getMembers();
Address target=members.get(0);
Message msg=new Message(target, null, "hello");
channel.send(msg);
于 2011-09-29T06:21:46.507 回答