编程语言
首页 > 编程语言> > Java-JGroups:发送(空,空,消息)与发送(地址,空,消息)

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

谢谢.

解决方法:

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

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

标签:jgroups,java
来源: https://codeday.me/bug/20191207/2087246.html