@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; } }
/** * Receive a message as a String with the default charset. * * @deprecated use {@link #recvStr(int, Charset)} instead. * @param flags the flags to apply to the receive operation. * @return the message received, as a String; null on error. */ @Deprecated public String recvStr(int flags) { return recvStr(flags, Charset.defaultCharset()); }
/** * Receive a message as a String with the default Charset. * * @deprecated use {@link #recvStr(Charset)} instead. * @return the message received, as a String; null on error. */ @Deprecated public String recvStr() { return recvStr(0); }
((FileHistory) console.getHistory()).flush(); this.socket.send(line); String serverInstruction = rendererSock.recvStr(); if (Objects.equals(serverInstruction, "shutdown")) { close();
@Override public void run() { while (true) { String request = clients.recvStr (0); String [] parts = request.split("\\?"); if (parts.length > 1){ try{ long startTime = System.currentTimeMillis(); if (parts.length > 2){ startTime = Long.parseLong(parts[2]); } ZeroMQDeliverySync d = new ZeroMQDeliverySync(parts[0], parts[1]); final DefaultDistributionRequest distributionReq = DefaultDistributionRequest.create(d, Mappings.getVSensorConfig(parts[0]), "select * from "+parts[0], startTime); logger.info("ZMQ request received: "+distributionReq.toString()); DataDistributer.getInstance(d.getClass()).addListener(distributionReq); }catch (Exception e){ logger.warn("ZMQ request parsing error: " + request, e); } } byte[] b=new byte[0]; ByteArrayOutputStream bais = new ByteArrayOutputStream(); Output o = new Output(bais); kryo.writeObjectOrNull(o,structures.get(parts[0]),DataField[].class); o.close(); b = bais.toByteArray(); clients.send(b, 0); } } });
@Override public boolean backstage(ZMQ.Socket pipe, ZPoller poller, int evts) { final String command = pipe.recvStr(); if (command == null) { System.out.printf("ZMonitor: Closing monitor %s : No command%n", monitored); return false; } switch (command) { case VERBOSE: verbose = Boolean.parseBoolean(pipe.recvStr()); return pipe.send(OK); case ADD_EVENTS: return addEvents(pipe); case REMOVE_EVENTS: return removeEvents(pipe); case START: return start(poller, pipe); case CLOSE: return close(poller, pipe); default: System.out.printf("ZMonitor: Closing monitor %s : Unknown command %s%n", monitored, command); pipe.send(ERROR); return false; } }
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(); } }
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(); } }
import org.zeromq.ZMQ; public class Subscriber { public static void main(String[] a){ final ZMQ.Context ctx = ZMQ.context(1); final ZMQ.Socket sub = ctx.socket(ZMQ.SUB); // sub.connect("tcp://localhost:6001"); sub.connect("ipc://001"); sub.subscribe("".getBytes()); while (true){ String msg = sub.recvStr(); System.out.println(msg); } } }
/** * * @return the message received, as a String object; null on no message. */ public String recvStr() { return recvStr(0); }
/** * Receive a message as a String with a given Charset. * * @param charset the charset of the resulting string. * @return the message received, as a String; null on error. */ public String recvStr(Charset charset) { return recvStr(0, charset); }