JGroups:send(null,null,Message)vs send(Address,null,Message)

时间:2011-09-28 15:52:29

标签: java jgroups

我已经编写了使用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地址) 有什么问题或者我应该添加一些额外的参数?

P.S,JGroups 3.0.0 RC1

感谢。

1 个答案:

答案 0 :(得分:3)

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

List<Address> members=channel.getView().getMembers();
Address target=members.get(0);
Message msg=new Message(target, null, "hello");
channel.send(msg);