Java Code Examples for org.zeromq.ZMQ#Socket

The following examples show how to use org.zeromq.ZMQ#Socket . 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: Pull.java    From XRTB with Apache License 2.0 8 votes vote down vote up
public Pull(HttpServletResponse response, String port, String timeout, String limit) throws Exception {
	int lim = 1;
	if (limit != null) {
		lim = Integer.parseInt(limit);
	}
	ZMQ.Context context = ZMQ.context(1);
	ZMQ.Socket rcv = context.socket(ZMQ.PULL);
	rcv.bind("tcp://*:" + port);
	if (timeout != null) {
		int t = Integer.parseInt(timeout);
		rcv.setReceiveTimeOut(t);
	}
	
	int k = 0;
	while(k < lim || lim == 0) {
		String str = rcv.recvStr();		
		response.getWriter().println(str);
		k++;
	}
	
	rcv.close();
	context.term();
}
 
Example 2
Source File: Pull.java    From bidder with Apache License 2.0 7 votes vote down vote up
public Pull(HttpServletResponse response, String port, String timeout, String limit) throws Exception {
	int lim = 1;
	if (limit != null) {
		lim = Integer.parseInt(limit);
	}
	ZMQ.Context context = ZMQ.context(1);
	ZMQ.Socket rcv = context.socket(ZMQ.PULL);
	rcv.bind("tcp://*:" + port);
	if (timeout != null) {
		int t = Integer.parseInt(timeout);
		rcv.setReceiveTimeOut(t);
	}
	
	int k = 0;
	while(k < lim || lim == 0) {
		String str = rcv.recvStr();		
		response.getWriter().println(str);
		k++;
	}
	
	rcv.close();
	context.term();
}
 
Example 3
Source File: KernelSocketsZMQ.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private synchronized void sendMsg(ZMQ.Socket socket, List<Message> messages) {
  if (!isShutdown()) {
    messages.forEach(message -> {
      String header = toJson(message.getHeader());
      String parent = toJson(message.getParentHeader());
      String meta = toJson(message.getMetadata());
      String content = toJson(message.getContent());
      String digest = hmac.sign(Arrays.asList(header, parent, meta, content));

      ZMsg newZmsg = new ZMsg();
      message.getIdentities().forEach(newZmsg::add);
      newZmsg.add(DELIM);
      newZmsg.add(digest.getBytes(StandardCharsets.UTF_8));
      newZmsg.add(header.getBytes(StandardCharsets.UTF_8));
      newZmsg.add(parent.getBytes(StandardCharsets.UTF_8));
      newZmsg.add(meta.getBytes(StandardCharsets.UTF_8));
      newZmsg.add(content.getBytes(StandardCharsets.UTF_8));
      message.getBuffers().forEach(x -> newZmsg.add(x));
      newZmsg.send(socket);
    });
  }
}
 
Example 4
Source File: PushPull.java    From bidder with Apache License 2.0 5 votes vote down vote up
public void push() {
	ZMQ.Context context = ZMQ.context(1);
	ZMQ.Socket sender = context.socket(ZMQ.PUSH);
	sender.connect("tcp://localhost:8086");
	sender.send("MESSAGE");
	sender.close();
	context.term();
}
 
Example 5
Source File: ZMQIntegrationTest.java    From netty-zmtp with Apache License 2.0 5 votes vote down vote up
@Theory
public void test_NettyBindDealer_ZmqConnectRouter(
    @FromDataPoints("identities") final String zmqIdentity,
    @FromDataPoints("identities") final String nettyIdentity,
    @FromDataPoints("versions") final ZMTPProtocol nettyProtocol
)
    throws InterruptedException, TimeoutException, ExecutionException {
  // XXX (dano): jeromq fails on identities longer than 127 bytes due to a signedness issue
  assumeFalse(MAX_IDENTITY.equals(zmqIdentity));

  final ZMTPSocket dealer = nettyBind(DEALER, nettyIdentity, nettyProtocol);
  final ZMQ.Socket router = zmqConnect(ZMQ.ROUTER, zmqIdentity);
  testReqRep(dealer, router, zmqIdentity);
}
 
Example 6
Source File: ZMQIntegrationTest.java    From netty-zmtp with Apache License 2.0 5 votes vote down vote up
@Theory
public void test_ZmqBindRouter_NettyConnectDealer(
    @FromDataPoints("identities") final String zmqIdentity,
    @FromDataPoints("identities") final String nettyIdentity,
    @FromDataPoints("versions") final ZMTPProtocol nettyProtocol
)
    throws InterruptedException, TimeoutException, ExecutionException {
  // XXX (dano): jeromq fails on identities longer than 127 bytes due to a signedness issue
  assumeFalse(MAX_IDENTITY.equals(zmqIdentity));

  final ZMQ.Socket router = zmqBind(ZMQ.ROUTER, zmqIdentity);
  final ZMTPSocket dealer = nettyConnect(DEALER, nettyIdentity, nettyProtocol);
  testReqRep(dealer, router, zmqIdentity);
}
 
Example 7
Source File: Push.java    From XRTB with Apache License 2.0 5 votes vote down vote up
public Push(String port, String message)  {
	ZMQ.Context context = ZMQ.context(1);
	ZMQ.Socket sender = context.socket(ZMQ.PUSH);
	sender.connect("tcp://localhost:" + port);
	sender.send(message);
	sender.close();
	context.term();
}
 
Example 8
Source File: PushPull.java    From XRTB with Apache License 2.0 5 votes vote down vote up
public void pull() {
	Runnable redisupdater = () -> {
		ZMQ.Context context = ZMQ.context(1);
		ZMQ.Socket rcv = context.socket(ZMQ.PULL);
		rcv.bind("tcp://*:8086");
		rcv.setReceiveTimeOut(1000);
		String str = rcv.recvStr();
		System.out.println("Received: " + str);
		rcv.close();
		context.term();
	};
	Thread nthread = new Thread(redisupdater);
	nthread.start();
}
 
Example 9
Source File: PushPull.java    From XRTB with Apache License 2.0 5 votes vote down vote up
public void push() {
	ZMQ.Context context = ZMQ.context(1);
	ZMQ.Socket sender = context.socket(ZMQ.PUSH);
	sender.connect("tcp://localhost:8086");
	sender.send("MESSAGE");
	sender.close();
	context.term();
}
 
Example 10
Source File: ZeroMQEventSubscriber.java    From support-rulesengine with Apache License 2.0 5 votes vote down vote up
private ZMQ.Socket getSubscriber() {
	if (subscriber == null) {
		try {
			subscriber = context.socket(SocketType.SUB);
			subscriber.connect(zeromqAddress + ":" + zeromqAddressPort);
			subscriber.subscribe("".getBytes());
		} catch (Exception e) {
			logger.error("Unable to get a ZMQ subscriber.  Error:  " + e);
			subscriber = null;
		}
	}
	return subscriber;
}
 
Example 11
Source File: ZMQIntegrationTest.java    From netty-zmtp with Apache License 2.0 5 votes vote down vote up
private void testReqRep(final ZMTPSocket req, final ZMQ.Socket rep, final String zmqIdentity)
    throws InterruptedException, TimeoutException {

  // Verify that sockets are connected
  verify(handler, timeout(5000)).connected(eq(req), any(ZMTPSocket.ZMTPPeer.class));

  // Verify that the peer identity was correctly received
  verifyPeerIdentity(req, zmqIdentity);

  // Send request
  final ZMTPMessage request = ZMTPMessage.fromUTF8("envelope", "", "hello", "world");
  request.retain();
  req.send(request);

  // Receive request
  final ZMsg receivedRequest = ZMsg.recvMsg(rep);

  // Send reply
  receivedRequest.send(rep, false);

  // Receive reply
  verify(handler, timeout(5000)).message(
      eq(req), any(ZMTPSocket.ZMTPPeer.class), messageCaptor.capture());
  final ZMTPMessage reply = messageCaptor.getValue();
  ReferenceCountUtil.releaseLater(reply);

  // Verify echo
  assertEquals(request, reply);
  request.release();
}
 
Example 12
Source File: NativeMessageQueueTest.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void startSubscribeThread(){
  Thread thread =
          new Thread(() -> {
            ZContext context = new ZContext();
            ZMQ.Socket subscriber = context.createSocket(SocketType.SUB);

            Assert.assertEquals(true, subscriber.connect(String.format("tcp://localhost:%d", bindPort)));
            Assert.assertEquals(true, subscriber.subscribe(topic));

            while (!Thread.currentThread().isInterrupted()) {
              byte[] message = subscriber.recv();
              String triggerMsg = new String(message);

              Assert.assertEquals(true, triggerMsg.contains(dataToSend) || triggerMsg.contains(topic));

            }
          });
  thread.start();
}
 
Example 13
Source File: Push.java    From bidder with Apache License 2.0 5 votes vote down vote up
public Push(String port, String message)  {
	ZMQ.Context context = ZMQ.context(1);
	ZMQ.Socket sender = context.socket(ZMQ.PUSH);
	sender.connect("tcp://localhost:" + port);
	sender.send(message);
	sender.close();
	context.term();
}
 
Example 14
Source File: JeroMqTestClient.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> call() throws Exception {
    try (ZMQ.Socket subscriber = context.socket(ZMQ.SUB)) {
        subscriber.connect(endpoint);
        subscriber.subscribe(new byte[0]);
        for (int messageNum = 0; messageNum < receiveCount
                && !Thread.currentThread().isInterrupted(); messageNum++) {
            // Use trim to remove the tailing '0' character
            messages.add(subscriber.recvStr(0).trim());
        }
    }
    return messages;
}
 
Example 15
Source File: ZMQIntegrationTest.java    From netty-zmtp with Apache License 2.0 4 votes vote down vote up
private ZMQ.Socket zmqConnect(final int zmqType, final String identity) {
  zmqSocket = context.socket(zmqType);
  setIdentity(zmqSocket, identity);
  zmqSocket.connect("tcp://127.0.0.1:" + port);
  return zmqSocket;
}
 
Example 16
Source File: ZMQIntegrationTest.java    From netty-zmtp with Apache License 2.0 4 votes vote down vote up
private void setIdentity(final ZMQ.Socket socket, final String identity) {
  if (!identity.equals(ANONYMOUS)) {
    socket.setIdentity(identity.getBytes(UTF_8));
  }
}
 
Example 17
Source File: ConnectionManager.java    From trex-stateless-gui with Apache License 2.0 4 votes vote down vote up
private ZMQ.Socket buildRequester() {
    ZMQ.Socket s = context.createSocket(ZMQ.REQ);
    s.setReceiveTimeOut(INTERNAL_TIMEOUT);
    s.setSendTimeOut(INTERNAL_TIMEOUT);
    return s;
}
 
Example 18
Source File: CompletionServer.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
public CompletionServer(ZMQ.Socket socket, Set<String> commandNames) {
    this.socket = socket;
    this.commandNames = commandNames;
}
 
Example 19
Source File: RemoteCompleter.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
public RemoteCompleter(ZMQ.Socket socket) {
    this.socket = socket;
    socket.setReceiveTimeOut(5000);
}
 
Example 20
Source File: KernelSocketsZMQ.java    From beakerx with Apache License 2.0 4 votes vote down vote up
private ZMQ.Socket getNewSocket(int type, int port, String connection, ZMQ.Context context) {
  ZMQ.Socket socket = context.socket(type);
  socket.bind(connection + ":" + String.valueOf(port));
  return socket;
}