@Override public List<PubSubMessage> getMessages() { List<PubSubMessage> messages = new ArrayList<>(); long currentTime = System.currentTimeMillis(); if (currentTime - lastRequest <= minWait) { return messages; } lastRequest = currentTime; for (String url : urls) { try (CloseableHttpResponse response = client.execute(makeHttpGet(url))) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == RESTPubSub.OK_200) { HttpEntity httpEntity = response.getEntity(); String message = EntityUtils.toString(httpEntity, RESTPubSub.UTF_8); log.debug("Received message from url: {}. Message was {}", url, message); messages.add(PubSubMessage.fromJSON(message)); EntityUtils.consume(httpEntity); } else if (statusCode != RESTPubSub.NO_CONTENT_204) { // NO_CONTENT_204 indicates there are no new messages - anything else indicates a problem log.error("HTTP call to {} failed with status code {} and response {}.", url, statusCode, response); } } catch (Exception e) { log.error("HTTP call to {} failed with error:", url, e); } } return messages; }
private void handleResponse(String id, Response response) { if (response == null || response.getStatusCode() != OK_200) { log.error("Handling error for id {} with response {}", id, response); responses.offer(new PubSubMessage(id, DRPCError.CANNOT_REACH_DRPC.asJSONClip())); return; } log.info("Received for id {}: {} {}", response.getStatusCode(), id, response.getStatusText()); String body = response.getResponseBody(); PubSubMessage message = PubSubMessage.fromJSON(body); log.debug("Received for id {}:\n{}", message.getId(), message.getContent()); responses.offer(message); } }
private void handleResponse(String id, Response response) { if (response == null || response.getStatusCode() != OK_200) { log.error("Handling error for id {} with response {}", id, response); responses.offer(new PubSubMessage(id, DRPCError.CANNOT_REACH_DRPC.asJSONClip())); return; } log.info("Received for id {}: {} {}", response.getStatusCode(), id, response.getStatusText()); String body = response.getResponseBody(); PubSubMessage message = PubSubMessage.fromJSON(body); log.debug("Received for id {}:\n{}", message.getId(), message.getContent()); responses.offer(message); } }
@Override public List<PubSubMessage> getMessages() throws PubSubException { // Try and read from DRPC. The DRPCSpout does a sleep for 1 ms if there are no tuples, so we don't have to do it. spout.nextTuple(); if (!collector.haveOutput()) { return null; } // The DRPCSpout only should have emitted one tuple List<List<Object>> tuples = collector.reset(); log.debug("Have a message through DRPC {}", tuples); List<Object> tupleAndID = tuples.get(0); // The first object is the actual DRPCSpout tuple and the second is the DRPC messageID. List<Object> tuple = (List<Object>) tupleAndID.get(0); Object drpcID = tupleAndID.get(1); // The first object in the tuple is our PubSubMessage as JSON String pubSubMessageJSON = (String) tuple.get(0); // The second object in the tuple is the serialized returnInfo added by the DRPCSpout String returnInfo = (String) tuple.get(1); log.debug("Read message\n{}\nfrom DRPC with return information {}", pubSubMessageJSON, returnInfo); PubSubMessage pubSubMessage = PubSubMessage.fromJSON(pubSubMessageJSON); // Add returnInfo as metadata. Cannot add it to pubSubMessage String id = pubSubMessage.getId(); String content = pubSubMessage.getContent(); int sequence = pubSubMessage.getSequence(); PubSubMessage message = new PubSubMessage(id, content, new Metadata(null, returnInfo), sequence); emittedIDs.put(ImmutablePair.of(id, sequence), drpcID); return Collections.singletonList(message); }
@Override public List<PubSubMessage> getMessages() throws PubSubException { // Try and read from DRPC. The DRPCSpout does a sleep for 1 ms if there are no tuples, so we don't have to do it. spout.nextTuple(); if (!collector.haveOutput()) { return null; } // The DRPCSpout only should have emitted one tuple List<List<Object>> tuples = collector.reset(); log.debug("Have a message through DRPC {}", tuples); List<Object> tupleAndID = tuples.get(0); // The first object is the actual DRPCSpout tuple and the second is the DRPC messageID. List<Object> tuple = (List<Object>) tupleAndID.get(0); Object drpcID = tupleAndID.get(1); // The first object in the tuple is our PubSubMessage as JSON String pubSubMessageJSON = (String) tuple.get(0); // The second object in the tuple is the serialized returnInfo added by the DRPCSpout String returnInfo = (String) tuple.get(1); log.debug("Read message\n{}\nfrom DRPC with return information {}", pubSubMessageJSON, returnInfo); PubSubMessage pubSubMessage = PubSubMessage.fromJSON(pubSubMessageJSON); // Add returnInfo as metadata. Cannot add it to pubSubMessage String id = pubSubMessage.getId(); String content = pubSubMessage.getContent(); int sequence = pubSubMessage.getSequence(); PubSubMessage message = new PubSubMessage(id, content, new Metadata(null, returnInfo), sequence); emittedIDs.put(ImmutablePair.of(id, sequence), drpcID); return Collections.singletonList(message); }