@Override public String toString() { return asJSON(); }
private HttpPost makeHttpPost(String url, PubSubMessage message) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(message.asJSON())); httpPost.setHeader(CONTENT_TYPE, APPLICATION_JSON); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(connectTimeout).build(); httpPost.setConfig(requestConfig); return httpPost; } }
/** * Send the {@link PubSubMessage} to the given url. * * @param url The url to which to send the message. * @param message The message to send. */ protected void sendToURL(String url, PubSubMessage message) { log.debug("Sending message: {} to url: {}", message, url); try (CloseableHttpResponse response = client.execute(makeHttpPost(url, message))) { if (response == null || response.getStatusLine().getStatusCode() != RESTPubSub.OK_200) { log.error("Couldn't POST to REST pubsub server. Got response: {}", response); } } catch (Exception e) { log.error("Error when trying to POST. Message was: {}. Error was: ", message.asJSON(), e); } }
public static String makeMessage(String id, String content, int sequence) { return new PubSubMessage(id, content, sequence).asJSON(); } public static Object makeMessageID(String id, int index) {
@Override public void send(PubSubMessage message) throws PubSubException { String url = urls.get(); String id = message.getId(); String json = message.asJSON(); log.info("Posting to {} for id {}", url, id); log.debug("Posting to {} with body {}", url, json); client.preparePost(url).setBody(json).execute().toCompletableFuture() .exceptionally(this::handleException) .thenAcceptAsync(createResponseConsumer(id)); }
@Override public void send(PubSubMessage message) throws PubSubException { String url = urls.get(); String id = message.getId(); String json = message.asJSON(); log.info("Posting to {} for id {}", url, id); log.debug("Posting to {} with body {}", url, json); client.preparePost(url).setBody(json).execute().toCompletableFuture() .exceptionally(this::handleException) .thenAcceptAsync(createResponseConsumer(id)); }
@Override public void send(PubSubMessage message) throws PubSubException { Metadata metadata = message.getMetadata(); // Remove the content String content = metadata.getContent().toString(); log.debug("Removing metadata {} for result {}@{}: {}", content, message.getId(), message.getSequence(), message.getContent()); metadata.setContent(null); String serializedMessage = message.asJSON(); Tuple tuple = new DRPCTuple(new Values(serializedMessage, content)); // This sends the message through DRPC and not to the collector but it acks or fails accordingly. bolt.execute(tuple); if (!collector.isAcked()) { throw new PubSubException("Message not acked. Unable to send message through DRPC:\n " + serializedMessage); } // Otherwise, we're good to proceed collector.reset(); }
@Override public void send(PubSubMessage message) throws PubSubException { Metadata metadata = message.getMetadata(); // Remove the content String content = metadata.getContent().toString(); log.debug("Removing metadata {} for result {}@{}: {}", content, message.getId(), message.getSequence(), message.getContent()); metadata.setContent(null); String serializedMessage = message.asJSON(); Tuple tuple = new DRPCTuple(new Values(serializedMessage, content)); // This sends the message through DRPC and not to the collector but it acks or fails accordingly. bolt.execute(tuple); if (!collector.isAcked()) { throw new PubSubException("Message not acked. Unable to send message through DRPC:\n " + serializedMessage); } // Otherwise, we're good to proceed collector.reset(); }
@Test(timeOut = 5000L) public void testReadingOkResponse() throws Exception { PubSubMessage expected = new PubSubMessage("foo", "response"); CompletableFuture<Response> response = getOkFuture(getOkResponse(expected.asJSON())); AsyncHttpClient mockClient = mockClientWith(response); pubscriber.setClient(mockClient); pubscriber.send(new PubSubMessage("foo", "bar")); // This is async (but practically still very fast since we mocked the response), so need a timeout. PubSubMessage actual = fetchAsync().get(); Assert.assertNotNull(actual); Assert.assertEquals(actual.getId(), expected.getId()); Assert.assertEquals(actual.getContent(), expected.getContent()); }