在Java中使用带有ZeroMQ的SUB时没有消息匹配 [英] No messages match using SUB in Java with ZeroMQ

查看:409
本文介绍了在Java中使用带有ZeroMQ的SUB时没有消息匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Java客户端与ZeroMQ一起使用.订阅任何前缀时,Java客户端均不匹配任何消息,尽管类似的Python客户端也可以按预期匹配消息.

I'm trying to use Java client with ZeroMQ. When subscribing to any prefix, the Java client matches no messages, although a similar Python client matches messages as expected.

Python服务器

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")

for i in range(100):
    r = "XXX " + i
    socket.send_string(r)

    time.sleep(random.randint(0,10))

Python客户端运行正常

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5556")

zip_filter = "XXX"
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)

for update_nbr in range(5):
    s = socket.recv_string()
    print(s)

没有消息匹配的Java客户端

context = ZMQ.context(1);
subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5556");

String filter = "XXX";
subscriber.subscribe(filter.getBytes(Charset.forName("UTF-8")));
while (true) {
  String msg = subscriber.recvStr(0, Charset.forName("UTF-8"));
  // ...
}

使用上述Python服务器,Python客户端将按预期匹配所有以XXX开头的消息.

Using the above Python server, the Python client matches all messages starting with XXX as expected.

使用相同的Python服务器,Java客户端不匹配任何消息.

Using the same Python server, the Java client matches no messages.

您是否知道Java客户端中对subscribe()的调用出了什么问题?

Do you have any idea what is wrong with the call to subscribe() in the Java client?

推荐答案

好,所以我重新创建了您的配置,遗憾的是,一切都正常-在python和java中都可以. (这是证明))

Ok, so I've recreated your configuration and sadly, everything works fine - both in python and java. (here's the proof) )

Java代码:

public class Client {

    public static void main(String[] args) {
        final Context context = context(1);
        final Socket subscriber = context.socket(SUB);
        subscriber.connect("tcp://localhost:5556");

        String filter = "XXX";
        subscriber.subscribe(filter.getBytes(Charset.forName("UTF-8")));
        while (true) {
            String msg = subscriber.recvStr();
            System.out.println(msg);
        }
    }
}

Maven依赖项:

<dependency>
    <groupId>org.zeromq</groupId>
    <artifactId>jeromq</artifactId>
    <version>0.3.4</version>
</dependency>

zeromq版本:4.1.0

zeromq version: 4.1.0

您使用什么版本的jeromq?我什至没有方法recvStr(int, Java.nio.charset.Charset).

What version of jeromq do you use? I don't even have a method recvStr(int, Java.nio.charset.Charset).

这篇关于在Java中使用带有ZeroMQ的SUB时没有消息匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆