public void stop() { Log.write("Stopping service on port " + this.port); external.close(); internal.close(); context.term(); Log.write("Stopped service on port " + this.port); }
protected synchronized void initializeQueue(String host, String queueName, Integer port) throws InterruptedException { final String bind = "tcp://" + host + ":" + port; _log.warn("binding to " + bind); if (_context == null) { _context = ZMQ.context(1); } if (_socket != null) { _executorService.shutdownNow(); _heartbeatService.shutdownNow(); Thread.sleep(1 * 1000); _log.warn("_executorService.isTerminated=" + _executorService.isTerminated()); _socket.close(); _executorService = Executors.newFixedThreadPool(1); _heartbeatService = Executors.newFixedThreadPool(1); } _socket = _context.socket(ZMQ.PUB); _socket.connect(bind); _executorService.execute(new SendThread(_socket, queueName)); _heartbeatService.execute(new HeartbeatThread(HEARTBEAT_INTERVAL)); _log.debug("Inference output queue is sending to " + bind); _initialized = true; }
@Override public void startSubscriber() { context = ZMQ.context(1); subscriber = context.socket(ZMQ.SUB); subscriber.connect("tcp://" + addr + ":" + port); subscriber.subscribe("UPDATE".getBytes()); Thread executor = new Thread(this); executor.start(); }
@Override public void run() { while (true) { String address = new String(subscriber.recv(0)); String className = new String(subscriber.recv(0)); byte[] classData = subscriber.recv(0); System.out.println(address + " : " + className + " " + Arrays.toString(classData)); for (ISubscriptionListener listener : this.listeners) { listener.update(className, classData); } } }
public void run() { while (!Thread.currentThread().isInterrupted()) { try { OrderBook ob = inboundQueue.take(); String message = gson.toJson(ob); socket.send(message, 0); } catch (InterruptedException e) { log.severe( "ZeroMQPublisher interrupted waiting for next order book in queue: " + e.getMessage()); e.printStackTrace(); } } socket.close(); context.term(); }
public static void main(String[] args) throws IOException { ZMQ.Context context = ZMQ.context(1); ZMQ.Socket subscriber = context.socket(ZMQ.SUB); subscriber.connect("tcp://localhost:7817"); subscriber.subscribe("/GOVI/KV8".getBytes()); while (true) { ZMsg msg = ZMsg.recvMsg(subscriber); try { Iterator<ZFrame> msgs = msg.iterator(); msgs.next(); ArrayList<Byte> receivedMsgs = new ArrayList<Byte>(); while (msgs.hasNext()) { for (byte b : msgs.next().getData()) { receivedMsgs.add(b); } } byte[] fullMsg = new byte[receivedMsgs.size()]; for (int i = 0; i < fullMsg.length; i++) { fullMsg[i] = receivedMsgs.get(i); } InputStream gzipped = new ByteArrayInputStream(fullMsg); InputStream in = new GZIPInputStream(gzipped); StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1; ) { out.append(new String(b, 0, n)); } String s = out.toString(); CTX c = new CTX(s); for (int i = 0; i < c.rows.size(); i++) { HashMap<String, String> row = c.rows.get(i); System.out.println( row.get("LinePlanningNumber") + " " + row.get("TripStopStatus") + " " + row.get("ExpectedDepartureTime") + " " + row.get("TimingPointCode")); } } catch (Exception e) { e.printStackTrace(); } } }
@Override public void run() { while (!Thread.currentThread().isInterrupted()) { String r = null; try { r = _outputBuffer.poll(250, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { _log.error("SendThread interrupted", ie); return; } if (r != null) { if (_isPrimaryInferenceInstance) { _zmqSocket.send(_topicName, ZMQ.SNDMORE); _zmqSocket.send(r.getBytes(), 0); } } final String h = _heartbeatBuffer.poll(); if (h != null) { if (_isPrimaryInferenceInstance) { _zmqSocket.send(HEARTBEAT_TOPIC.getBytes(), ZMQ.SNDMORE); _zmqSocket.send(h.getBytes(), 0); _log.debug("heartbeat=" + h); } } if (r == null) { // skip output message below if no queue content continue; } if (processedCount > 50) { _log.warn( "Inference output queue(primary=" + _isPrimaryInferenceInstance + "): processed 50 messages in " + (new Date().getTime() - markTimestamp.getTime()) / 1000 + " seconds; current queue length is " + _outputBuffer.size()); markTimestamp = new Date(); processedCount = 0; } processedCount++; } }
/** @param args */ public static void main(String[] args) { // output version System.out.println(String.format("0MQ %s", ZMQ.getVersionString())); // call TPS calculator every second Timer timer = new Timer(); timer.schedule(new TPSCalculator(), 1000, 1000); ZMQ.Context context = ZMQ.context(1); // Socket to talk to server ZMQ.Socket requester = context.socket(ZMQ.PUSH); requester.connect("tcp://localhost:5555"); // create the request body JSONObject obj = new JSONObject(); obj.put("id", "telecell"); obj.put("type", "telecell"); obj.put("isPattern", false); obj.put("att", "value"); // create xrap request Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json"); String request = XRapLibrary.getInstance().createPOST("/updateContext/", headers, obj.toString()); for (int requestNbr = 0; requestNbr != SAMPLE_SIZE; requestNbr++) { // send a request obj.put("id", "telecell" + requestNbr); obj.put("att", "value" + requestNbr); request = XRapLibrary.getInstance().createPOST("/updateContext/", headers, obj.toString()); requester.send(request); // byte[] reply = requester.recv(0); // count the transaction transactionCount++; } // stop the timer timer.cancel(); // printout results printResults(); requester.close(); context.term(); }
public static void main(String[] args) throws Exception { ZMQ.Context context = ZMQ.context(1); // Socket to talk to clients ZMQ.Socket socket = context.socket(ZMQ.REP); socket.bind("tcp://*:5555"); while (!Thread.currentThread().isInterrupted()) { byte[] reply = socket.recv(0); System.out.println("Received Hello"); Thread.sleep(1000); // Do some 'work' String request = "World"; socket.send(request.getBytes(), 0); } socket.close(); context.term(); }
ZeroMQPublisher(Integer gatewayPort, LinkedBlockingQueue<OrderBook> inboundQueue) { log.info("Opening outbound ZeroMQ publisher on port " + gatewayPort.toString()); this.inboundQueue = inboundQueue; this.gatewayPort = gatewayPort; context = ZMQ.context(1); socket = context.socket(ZMQ.PUB); socket.bind("tcp://*:" + gatewayPort.toString()); }
@Override public void run() { ZMQ.Socket socket = context.socket(ZMQ.PULL); socket.connect(this.address); while (true) { String request = socket.recvStr(0); System.out.println(Thread.currentThread().getName() + " Received request: [" + request + "]"); // Do some 'work' try { Thread.sleep(1000); } catch (InterruptedException e) { } } }
protected void startUp() { context = ZMQ.context(1); listener = context.socket(ZMQ.REP); listener.bind("tcp://*:5555"); poller = new ZMQ.Poller(1); poller.register(listener, ZMQ.Poller.POLLIN); log.info("started"); }
public <T> void run(final Action<T> handler, final Factory<T> factory, final Class<T> typeClass) { final Schema<T> schema = RuntimeSchema.getSchema(typeClass); while (true) { final byte[] message = subscriber.recv(0); T event = factory.build(); ProtobufIOUtil.mergeFrom(message, event, schema); handler.invoke(event); } }
public static void main(String... args) { ZMQ.Context context = ZMQ.context(1); System.out.println("Connecting to Server ... "); ZMQ.Socket requester = context.socket(ZMQ.REQ); requester.connect("tcp://localhost:5555"); for (int i = 0; i < 10; i++) { String request = "hello"; System.out.println("Sending msg to server " + request); requester.send(request.getBytes(), 0); byte[] recv = requester.recv(0); System.out.println("Receiving msg from server [" + new String(recv) + "]"); } requester.close(); context.term(); }
public Closeable subscribe(final String topic) { final byte[] topicBytes = topic.getBytes(); subscriber.subscribe(topicBytes); return new Closeable() { @Override public void close() throws IOException { subscriber.unsubscribe(topicBytes); } }; }
@Override protected void run() throws Exception { while (!Thread.currentThread().isInterrupted() && isRunning()) { byte[] message = "".getBytes(); poller.poll(200); if (poller.pollin(0)) { try { message = listener.recv(0); } catch (ZMQException e) { if (e.getErrorCode() == ZMQ.Error.ETERM.getCode()) { break; } } log.info("hi: {}", message); String response = "World"; listener.send(response.getBytes(), 0); } } }
@Override protected void destroy() { pollThreadPool.shutdown(); try { pollThreadPool.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException e) { } workerThreadPool.shutdown(); try { workerThreadPool.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException e) { } pollThreadPool.shutdownNow(); workerThreadPool.shutdownNow(); workerSocketPool.destory(); serviceSocket.close(); workerSocket.close(); context.term(); LOG.info("Stopped Aps ZMQ Server"); }
/** * Simple method to send & receive zeromq message * * @param method * @param uri * @param json * @return */ private String sendAndReceive(String method, String uri, String json) { ZMQ.Socket socket = context.socket(ZMQ.DEALER); socket.connect(address); // Handshake try { Thread.sleep(100); } catch (Exception e) { Assert.fail("Handshake failed"); } StringBuilder sb = new StringBuilder(method); sb.append(ZMQSocket.SEPARATOR).append(uri).append(ZMQSocket.SEPARATOR); if (json != null) { sb.append(json); } String result = null; try { socket.send(sb.toString().getBytes("UTF-8"), 0); byte[] response = socket.recv(0); result = new String(response, Charset.forName("UTF-8")); } catch (UnsupportedEncodingException e) { Assert.fail("Exception when sending/receiving message"); } finally { try { socket.close(); } catch (Exception e2) { // ignore } } return result; }
public static void main(String[] args) { String myID = null; if (args.length == 4) { try { zooHost = args[0]; zooPort = Integer.parseInt(args[1]); myPort = Integer.parseInt(args[2]); myID = args[3]; } catch (Exception e) { e.printStackTrace(); } } else { System.err.println("Usage tracker [zooHost] [zooPort] [myPort] [myID]"); System.exit(-1); } // initialize ZMQ context = ZMQ.context(1); socket = context.socket(ZMQ.REP); socket.bind("tcp://*:" + myPort); eventBus = new EventBus("Tracker"); JobTracker t = new JobTracker(myID); eventBus.register(t); System.out.println("Starting thread"); new Thread(t.manageWorker()).start(); while (true) { // wait for client req then respond JobPacket packetFromServer = (JobPacket) SerializationUtils.deserialize(socket.recv(0)); System.out.println("From client" + packetFromServer.type); eventBus.post(packetFromServer); } }
@Subscribe public void handleJob(JobPacket jobPacket) throws Exception { JobPacket packetToClient = new JobPacket(); if (jobPacket.type == JobPacket.JOB_REQ) { jobQueue.add(jobPacket.hash); packetToClient.type = JobPacket.JOB_ACCEPTED; packetToClient.result = "none"; } if (jobPacket.type == JobPacket.JOB_STATUS) { // check under /result/<hash> try { if ((zooKeeper.exists(Joiner.on("/").join(ZK_JOBS, jobPacket.hash), false) != null) && (zooKeeper.exists(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false) == null)) { System.out.println("Job in progress, please wait!"); packetToClient.type = JobPacket.JOB_PROGRESS; packetToClient.result = "none"; } if ((zooKeeper.exists(Joiner.on("/").join(ZK_JOBS, jobPacket.hash), false) == null) && (zooKeeper.exists(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false) == null)) { System.out.println("No such Job, please enter your job again!"); packetToClient.type = JobPacket.JOB_NOTFOUND; packetToClient.result = "none"; } if (zooKeeper.exists(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false) != null) { byte[] data = zooKeeper.getData(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false, null); packetToClient.type = JobPacket.JOB_RESULT; if (data == null) { System.out.println("Result not found!"); packetToClient.result = null; } else { String result = new String(data); packetToClient.result = result; System.out.println("Result found!"); } } } catch (Exception e) { e.printStackTrace(); } } socket.send(SerializationUtils.serialize(packetToClient), 0); }
@Override public void run() { ZMQ.Context ctx = ZMQ.context(1); ZMQ.Socket publisherX = ctx.socket(ZMQ.XPUB); publisherX.bind(Util.formUrl(ip, portXPub)); ZMQ.Socket subscriberX = ctx.socket(ZMQ.XSUB); subscriberX.bind(Util.formUrl(ip, portXSub)); ZMQ.proxy(publisherX, subscriberX, null); publisherX.close(); subscriberX.close(); ctx.term(); }
private void bindSocket() { context = ZMQ.context(1); serviceSocket = context.socket(ZMQ.ROUTER); serviceSocket.setIdentity(identity.getBytes()); serviceSocket.setLinger(zmqLinger); serviceSocket.setHWM(zmqHWM); if (serviceSocket.bind(endpoint) <= 0) { serviceSocket.close(); context.term(); throw new IllegalStateException("service port binding fail"); } LOG.info("Using identity " + identity + " wait for response on " + endpoint); workerSocket = context.socket(ZMQ.DEALER); workerSocket.setLinger(0); workerSocket.bind(INPROC_ENDPOINT); workerSocketPool = new ZMQWorkerSocketPool(context, INPROC_ENDPOINT); }
public Subscriber(final String endpoint) { final ZMQ.Context context = ZMQ.context(1); subscriber = context.socket(ZMQ.SUB); subscriber.connect(endpoint); }
@Override public void run() { Message message = null; boolean stop = false; byte id[], msg[]; ZMQ.Socket frontend = null; int numLoaders = Integer.parseInt(Configuration.getProperty(Configuration.Key.NUM_LOADERS)); frontend = context.socket(ZMQ.ROUTER); frontend.bind("tcp://*:" + Configuration.getProperty(Configuration.Key.NODE_PORT)); ZMQ.Poller poller = context.poller(3 + numLoaders); poller.register(frontend, ZMQ.Poller.POLLIN); commandWorkerManager.init(context, poller); // Registers two backends loadWorkerManager.init(context, poller, numLoaders); searchWorkerManagerV5.init(context); System.out.println("ZMQ Server started...."); try { do { poller.poll(); if (poller.pollin(0)) { boolean msgFromReq = false; id = frontend.recv(0); msg = frontend.recv(0); // In case we got a message from a REQ socket if (frontend.hasReceiveMore()) { msg = frontend.recv(0); msgFromReq = true; } message = Message.readFromBytes(msg); switch (message.getType()) { case CONFIG_CLUSTER_REQ: Message configResponse = new Message(MessageType.CONFIG_CLUSTER_REP); try { SteffiGraph.getInstance().initializeMemberIndexes(); searchWorkerManagerV5.initializeClientThreads(); configResponse.setBody("OK"); } catch (Exception x) { configResponse.setBody("ERROR: " + x.getMessage()); ImgLogger.logError(x, x.getMessage()); } frontend.send(id, ZMQ.SNDMORE); frontend.send(Message.convertMessageToBytes(configResponse), 0); break; case CLEAR: break; case LOAD_REQ: loadWorkerManager.sendToLoader(id, msg); break; case SEARCH_REQ: searchMsgCounter++; searchWorkerManagerV5.sendToSearchWorker((SearchReqMsg) message); break; case STOP: loadWorkerManager.stop(); commandWorkerManager.stop(); searchWorkerManagerV5.stop(); SteffiGraph.getInstance().closeGraphClients(); stop = true; break; case END_SEARCH: searchWorkerManagerV5.endSearch(((SearchEndMsg) message).getSearchId()); break; default: commandWorkerManager.sendToCommandWorker(id, msg, msgFromReq); break; } } if (poller.pollin(1)) { commandWorkerManager.sentToFrontend(frontend, true); } if (poller.pollin(2)) { commandWorkerManager.sentToFrontend(frontend, false); } for (int i = 0; i < numLoaders; i++) { if (poller.pollin(i + 3)) { loadWorkerManager.sendToFrontend(i, frontend); } } } while (!stop); } catch (Exception x) { ImgLogger.logError(x, "Error on node server"); } finally { frontend.close(); context.term(); CacheContainer.getCellCache().stop(); CacheContainer.getCacheContainer().stop(); } System.out.println("Main node server closed..."); System.out.println(CacheContainer.getCacheContainer().getStatus()); }
@Override protected void shutDown() throws Exception { log.info("shutting down {}", listener.toString()); listener.close(); context.term(); }
@Override public void shutDown() { m_oVideoSocket.close(); }