简体   繁体   English

如何检查 JZMQ 套接字是否已连接

[英]How to check if JZMQ socket is connected

Is there way to check if JZMQ (java binding of zmq) socket is connected?有没有办法检查 JZMQ(zmq 的 java 绑定)套接字是否已连接?

ZContext zmqContext = new ZContext();
ZMQ.Socket workerSocket = zmqContext.createSocket(ZMQ.DEALER);
workerSocket.setIdentity("ID".getBytes());
workerSocket.connect("tcp://localhost:5556");

After code above I would like to check if workerSocket is connected.在上面的代码之后,我想检查 workerSocket 是否已连接。 It would be nice to check connection status.检查连接状态会很好。

No, there's no method in the API to check if a socket is connected. 不,API中没有方法可以检查套接字是否已连接。

ZeroMq abstracts the network; ZeroMq抽象网络; client and server connections are completely transparent to the peer making the connection. 客户端和服务器连接对进行连接的对等方完全透明。 A client or server may send messages to non-existent peers; 客户端或服务器可以向不存在的对等体发送消息; no errors will be generated; 不会产生任何错误; instead, they'll queue up in socket buffers based on HWM config. 相反,它们将基于HWM配置在套接字缓冲区中排队。

To check for peer availability, do it manually using a synchronous request/reply heartbeat with a timeout factor; 要检查对等可用性,请使用具有超时因子的同步请求/回复心跳手动执行此操作; here's an example, hope it helps! 这是一个例子,希望它有所帮助!

Check out samples for request/reply here! 在这里查看样品的请求/回复! https://github.com/imatix/zguide/tree/master/examples/ https://github.com/imatix/zguide/tree/master/examples/

i think I found a trick that works for me to check if a socket is connected.我想我找到了一个对我有用的技巧来检查套接字是否已连接。 The best solution to your client side is to create a socket poller and poll on the pull socket until a message is received.客户端的最佳解决方案是创建套接字轮询器并轮询拉式套接字,直到收到消息。 This avoids wasteful sleeps, and makes for generally tighter code:这避免了浪费睡眠,并使代码更紧凑:

Here is the code that do the works:这是完成工作的代码:

 private void blockUntilConnected() {
        ZMQ.Poller poller = ctx.createPoller(1);
        poller.register(this.subscriber, ZMQ.Poller.POLLIN);
        int rc = -1;
        while (rc == -1) {
            rc = poller.poll(1000);
        }
        poller.pollin(0);
    }

I will also supply the full source code:我还将提供完整的源代码:

Server Part:服务器部分:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

import java.net.InetSocketAddress;
import java.net.Socket;

import static io.Adrestus.config.ConsensusConfiguration.*;

public class ConsensusServer {

    private static Logger LOG = LoggerFactory.getLogger(ConsensusServer.class);

    private final ZContext ctx;
    private final String IP;
    private final ZMQ.Socket publisher;
    private final ZMQ.Socket collector;


    public ConsensusServer(String IP) {
        this.IP = IP;
        this.ctx = new ZContext();
        this.publisher = ctx.createSocket(SocketType.PUB);
        this.publisher.setHeartbeatIvl(2);
        this.collector = ctx.createSocket(SocketType.PULL);

        this.publisher.bind("tcp://" + IP + ":" + PUBLISHER_PORT);
        this.collector.bind("tcp://" + IP + ":" + COLLECTOR_PORT);
        this.collector.setReceiveTimeOut(CONSENSUS_TIMEOUT);
        this.publisher.setSendTimeOut(CONSENSUS_TIMEOUT);
    }

    public ConsensusServer() {
        this.IP = findIP();
        this.ctx = new ZContext();
        this.publisher = ctx.createSocket(SocketType.PUB);
        this.collector = ctx.createSocket(SocketType.PULL);

        this.publisher.bind("tcp://" + IP + ":" + PUBLISHER_PORT);
        this.collector.bind("tcp://" + IP + ":" + COLLECTOR_PORT);
        this.publisher.setSendTimeOut(CONSENSUS_TIMEOUT);
        this.collector.setReceiveTimeOut(CONSENSUS_TIMEOUT);
    }


    private String findIP() {
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress("google.com", 80));
            return socket.getLocalAddress().getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new IllegalArgumentException("Make sure you intern connection is working");
    }

    public void publishMessage(byte[] data) {
        publisher.send(data, 0);
    }

    public byte[] receiveData() {
        byte[] data = null;
        try {
            data = collector.recv();
        } catch (Exception e) {
            LOG.info("Socket Closed");
        }
        return data;
    }

    public static Logger getLOG() {
        return LOG;
    }

    public static void setLOG(Logger LOG) {
        ConsensusServer.LOG = LOG;
    }

    public ZContext getCtx() {
        return ctx;
    }

    public String getIP() {
        return IP;
    }

    public ZMQ.Socket getPublisher() {
        return publisher;
    }

    public ZMQ.Socket getCollector() {
        return collector;
    }

    public void close() {
        this.publisher.close();
        this.collector.close();
        this.ctx.close();
    }
}

Client Part:客户端部分:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;

import static io.Adrestus.config.ConsensusConfiguration.*;

public class ConsensusClient {

    private static Logger LOG = LoggerFactory.getLogger(ConsensusClient.class);
    private final String IP;
    private ZContext ctx;
    private final ZMQ.Socket subscriber;
    private final ZMQ.Socket push;


    public ConsensusClient(String IP) {
        this.ctx = new ZContext();
        this.IP = IP;
        this.subscriber = ctx.createSocket(SocketType.SUB);
        this.push = ctx.createSocket(SocketType.PUSH);
        this.subscriber.connect("tcp://" + IP + ":" + SUBSCRIBER_PORT);
        this.subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL);
        this.subscriber.setReceiveTimeOut(CONSENSUS_TIMEOUT);
        blockUntilConnected();
        this.push.connect("tcp://" + IP + ":" + COLLECTOR_PORT);
    }


    private void blockUntilConnected() {
        ZMQ.Poller poller = ctx.createPoller(1);
        poller.register(this.subscriber, ZMQ.Poller.POLLIN);
        int rc = -1;
        while (rc == -1) {
            rc = poller.poll(1000);
        }
        poller.pollin(0);
    }

    public void pushMessage(byte[] data) {
        push.send(data);
    }

    public byte[] receiveData() {
        byte[] data = subscriber.recv();
        return data;
    }

    public void close() {
        this.subscriber.close();
        this.push.close();
        this.ctx.close();
    }
}

Main part: ( Notice that the client is first initialized and it's blocked until a server is started and connected. You can simply add a timeout if you don't want to hang on forever)主要部分:(请注意,客户端首先被初始化,并且在服务器启动和连接之前一直处于阻塞状态。如果你不想永远挂起,你可以简单地添加一个超时)

import java.nio.charset.StandardCharsets;

public class CustomTest {

    public static void main(String[] args) {

        //client already started and block until server is connected
        (new Thread() {
            public void run() {
                ConsensusClient Client = new ConsensusClient("localhost");
                while (true) {
                    byte[] res = Client.receiveData();
                    System.out.println(new String(res));
                }
            }
        }).start();


        Thread.sleep(3000);

        //server started
        ConsensusServer Server = new ConsensusServer("localhost");

        Thread.sleep(100);
        Server.publishMessage("Message".getBytes(StandardCharsets.UTF_8));
        Server.publishMessage("Message".getBytes(StandardCharsets.UTF_8));
        Server.publishMessage("Message".getBytes(StandardCharsets.UTF_8));
        Thread.sleep(10000);

        Server.close();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM