Java Code Examples for org.zeromq.ZMQ.Socket#bind()

The following examples show how to use org.zeromq.ZMQ.Socket#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZThread.java    From aion with MIT License 6 votes vote down vote up
public static Socket fork(ZContext ctx, IAttachedRunnable runnable, Object... args) {
    Socket pipe = ctx.createSocket(ZMQ.PAIR);

    if (pipe != null) {
        pipe.bind(String.format("inproc://zctx-pipe-%d", pipe.hashCode()));
    } else {
        return null;
    }

    // Connect child pipe to our pipe
    ZContext ccontext = ZContext.shadow(ctx);
    Socket cpipe = ccontext.createSocket(ZMQ.PAIR);
    if (cpipe == null)
        return null;
    cpipe.connect(String.format("inproc://zctx-pipe-%d", pipe.hashCode()));

    // Prepare child thread
    Thread shim = new ShimThread(ccontext, runnable, args, cpipe);
    shim.start();

    return pipe;
}
 
Example 2
Source File: ZeroMQBroker.java    From ignite-book-code-samples with GNU General Public License v3.0 6 votes vote down vote up
public static void main (String[] args) {
    //  Prepare our context and sockets
    Context context = ZMQ.context(1);

    //  Socket facing clients
    Socket frontend = context.socket(ZMQ.ROUTER);
    frontend.bind("tcp://*:5559");

    //  Socket facing services
    Socket backend = context.socket(ZMQ.DEALER);
    backend.bind("tcp://*:5560");

    //  Start the proxy
    ZMQ.proxy (frontend, backend, null);

    //  We never get here but clean up anyhow
    frontend.close();
    backend.close();
    context.term();
}
 
Example 3
Source File: Session.java    From jupyter-kernel-jsr223 with Apache License 2.0 5 votes vote down vote up
public static void runKernelDebug(Session session) throws FileNotFoundException,
        InvalidKeyException,
        UnsupportedEncodingException,
        IOException {
    Session._DEBUG_ = true;
    ZContext ctx = new ZContext();
    Socket channel = ctx.createSocket(ZMQ.REP);
    channel.bind("tcp://127.0.0.1:2222");
    byte[] msg = channel.recv();
    String sArgs = new String(msg, StandardCharsets.UTF_8);
    String[] newArgs = sArgs.split(" ");
    channel.send("ok");
    runKernel(session, newArgs);
}
 
Example 4
Source File: ProtocolProcessor.java    From aion with MIT License 4 votes vote down vote up
@Override
public void run() {
    LOG.info("Starting Aion Api Server <port={}>", cfgApi.getPort());
    String bindAddr = "tcp://" + cfgApi.getIp() + ":" + cfgApi.getPort();
    int msgTh = 5;

    try {
        // create context.
        Context ctx = ZMQ.context(1);

        // create router sock.
        Socket feSock = ctx.socket(ROUTER);
        if (cfgApi.isSecureConnectEnabledEnabled()) {
            // Currently the system will only load the first pair of the key files.
            loadCurveKeyPair();

            if (curveSecKey != null && curvePubKey != null) {
                feSock.setZAPDomain("global".getBytes());
                feSock.setCurveServer(true);
                feSock.setCurvePublicKey(curvePubKey);
                feSock.setCurveSecretKey(curveSecKey);
                LOG.info("Secure connection enabled!");
            } else {
                LOG.info(
                        "Can't find the keyfile for setup the connection. Secure connection disabled!");
            }
        } else {
            LOG.info("Secure connection disabled!");
        }

        feSock.setSndHWM(zmqHWM);
        feSock.bind(bindAddr);

        Socket wkSocks = ctx.socket(DEALER);
        wkSocks.bind(AION_ZMQ_WK_TH);

        Socket cbSock = ctx.socket(DEALER);
        cbSock.bind(AION_ZMQ_CB_TH);

        Socket evSock = ctx.socket(DEALER);
        evSock.bind(AION_ZMQ_EV_TH);

        Socket hbSock = ctx.socket(DEALER);
        hbSock.bind(AION_ZMQ_HB_TH);

        ExecutorService es = Executors.newFixedThreadPool(msgTh);
        es.execute(() -> callbackRun(ctx));
        es.execute(this::txWaitRun);
        es.execute(() -> eventRun(ctx));
        es.execute(() -> workerRun(ctx));
        es.execute(() -> hbRun(ctx));

        Proxy.proxy(feSock, wkSocks, cbSock, evSock, hbSock);

        if (LOG.isInfoEnabled()) {
            LOG.info("ProtocolProcessor.run thread finish.");
        }

        if (LOG.isInfoEnabled()) {
            LOG.info("Shutting down Zmq sockets...");
        }
        // Shutdown HdlrZmq
        ((HdlrZmq) handler).shutdown();
        // Shutdown ZmqSocket
        feSock.close();
        wkSocks.close();
        cbSock.close();
        evSock.close();
        hbSock.close();
        // Shutdown ExecutorService
        es.shutdown();

        ctx.close();
        if (LOG.isInfoEnabled()) {
            LOG.info("Shutdown Zmq sockets... Done!");
        }

    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("ProtocolProcessor.run exception: " + e.getMessage());
        }
    }
}
 
Example 5
Source File: ZmqSendingMessageHandler.java    From spring-integration-zmq with Apache License 2.0 4 votes vote down vote up
public void run() {
	
	Socket socket = null;
	
	synchronized (startupMonitor) {
		try {
			socket = contextManager.context().createSocket(socketType);
			if (bind) {
				socket.bind(address);
			} else {
				socket.connect(address);
			}
		} finally {
			startupMonitor.notify();
		}
	}
	
	while (!Thread.currentThread().isInterrupted()) {
		try {
			Message<?> message = messageQueue.take();
			byte[] payload = converter.convert(message.getPayload());
			if (topicBytes == null) {
				socket.send(payload);
			} else {
				byte[] msgTopic = null;
				if (message.getHeaders().containsKey("zmq.topic")) {
					msgTopic = message.getHeaders().get("zmq.topic", String.class).getBytes(ZMQ.CHARSET);
				} else {
					msgTopic = topicBytes;
				}
				byte[] topicPayload = new byte[msgTopic.length + payload.length];
				System.arraycopy(msgTopic, 0, topicPayload, 0, msgTopic.length);
				System.arraycopy(payload, 0, topicPayload, msgTopic.length, payload.length);
				socket.send(topicPayload);
			}
		} catch (Throwable t) {
               if (!running) {
               	break;
               }
               logger.error("Exception in zmq sending message handler", t);
		}
	}
	
	socket.close();
}