org.zeromq.ZMQException:错误编号48:地址已在使用中

时间:2018-12-10 09:57:32

标签: zeromq jeromq

我正在尝试使用ZeroMQ实现发布订阅示例。 我在docker容器中运行发布者的代码,在另一个容器中运行订阅者的代码。

我的订户是:

private ZMQ.Context context;

{
    context = ZMQ.context(1);
}

public void receive() {
    System.out.println("Getting subscriber, listening to tcp://localhost:5565");
    getSubscriber();
    byte[] raw;
    System.out.println("Watching for new Event messages...");
    try {
        while (!Thread.currentThread().isInterrupted()) {
            raw = subscriber.recv();
            System.out.println("Event received " + raw);
        }
    } catch (Exception e) {
        System.out.println("Unable to receive messages via ZMQ: " + e.getMessage());
    }
    if (subscriber != null)
        subscriber.close();
    subscriber = null;
    System.out.println("Attempting restart of Event message watch.");
    receive();
}

private ZMQ.Socket getSubscriber() {
    if (subscriber == null) {
        try {
            subscriber = context.socket(ZMQ.SUB);
            subscriber.connect("tcp://localhost:5565");
            subscriber.subscribe("".getBytes());
        } catch (Exception e) {
            System.out.println("Unable to get a ZMQ subscriber.  Error:  " + e);
            subscriber = null;
        }
    }
    return subscriber;
}

我的发布者是:

private ZMQ.Context context;

{
    context = ZMQ.context(1);
}

public synchronized void sendEventMessage(Event event) {
    try {
        if (publisher == null) {
            getPublisher();
        }
        if (publisher != null) {
            publisher.send(event);
        } 
    } catch (Exception e) {
        System.out.println("Unable to send message via ZMQ");
    }
}

private void getPublisher() {
    try {
        if (publisher == null) {
            publisher = context.socket(ZMQ.PUB);
            publisher.bind("tcp://192.168.32.9:5565");   //where 192.168.32.9 is the IP of the subscriber's docker container
            Thread.sleep(PUB_UP_SLEEP); // allow subscribers to connect
        }
    } catch (Exception e) {
        System.out.println("Unable to get a publisher. Error:  " + e);
        publisher = null;
    }
}

启动应用程序时,我注册了一个订户,日志为:

[2018-12-10 08:01:02.138] boot - 1  INFO [main] --- ZeroMQEventSubscriber: Getting subscriber, listening to tcp://localhost:5565
[2018-12-10 08:01:02.249] boot - 1  INFO [main] --- ZeroMQEventSubscriber: Watching for new Event messages...

我的问题是,当我调用sendEventMessage时,订阅者没有收到任何消息,而在发布者上,我收到此错误:

[2018-12-10 08:54:16.388] boot - 1 ERROR [task-scheduler-5] --- ZeroMQEventPublisherImpl: Unable to get a publisher.  Error:  org.zeromq.ZMQException: Errno 48 : Address already in use

有什么想法为什么我不能绑定到订户连接的地址?

0 个答案:

没有答案