zbus发送topic 接收不到,大神帮看看

木易大虾 发布于 2016/06/14 12:48
阅读 357
收藏 0

@少帮主 你好,想跟你请教个问题:

消费者

public class TopicConsumer {

public static void main(String[] args) throws Exception {
// 1)创建Broker代表
BrokerConfig brokerConfig = new BrokerConfig();
brokerConfig.setServerAddress("127.0.0.1:15555");
Broker broker = new SingleBroker(brokerConfig);


MqConfig config = new MqConfig();
config.setBroker(broker);
config.setTopic("MyTopic");
config.setMode(MqMode.PubSub);

// 2) 创建消费者
Consumer c = new Consumer(config);
while (true) {
Message msg = c.recv(10000);
if (msg == null) continue;
System.out.println("消费者1接收到消息: " + msg);
}
}


}

生产者

public class TopicProducer {


public static void main(String[] args) throws Exception {
//1)创建Broker代表
BrokerConfig config = new BrokerConfig();
   config.setServerAddress("127.0.0.1:15555");
   Broker broker = new SingleBroker(config);


   //2) 创建生产者
   Producer p = new Producer(broker, "MyTopic", MqMode.PubSub);
   Message msg = new Message();
   msg.setBody("hello world topic");


   p.sendSync(msg);
   broker.close();
}
}

我运行消费者然后调用生产者发送消息,消费者一直没收到消息怎么回事?你帮我看看代码有问题吗


加载中
0
木易大虾
木易大虾

问题解决了,原来 zbus中topic必须属于某个队列

代码改成下面就可以了:

public class TopicProducer {


public static void main(String[] args) throws Exception {
//1)创建Broker代表
BrokerConfig config = new BrokerConfig();
   config.setServerAddress("127.0.0.1:15555");
   Broker broker = new SingleBroker(config);


   //2) 创建生产者
   Producer p = new Producer(broker, "MyTopicMQ", MqMode.PubSub);
   Message msg = new Message();
   msg.setTopic("MyTopic");
   msg.setBody("hello world topic");


   p.sendSync(msg);
   broker.close();
}
}

public class TopicConsumer {

public static void main(String[] args) throws Exception {
// 1)创建Broker代表
BrokerConfig brokerConfig = new BrokerConfig();
brokerConfig.setServerAddress("127.0.0.1:15555");
Broker broker = new SingleBroker(brokerConfig);


MqConfig config = new MqConfig();
config.setBroker(broker);
config.setMq("MyTopicMQ");
config.setTopic("MyTopic");
config.setMode(MqMode.PubSub);

// 2) 创建消费者
Consumer c = new Consumer(config);
while (true) {
Message msg = c.recv(10000);
if (msg == null) continue;
System.out.println("消费者1接收到消息: " + msg);
}
}


}

少帮主
少帮主
是的,topic不是全局的:)
返回顶部
顶部