use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.
the class DRPCQuerySubscriberTest method testReadingFromSpout.
@Test
public void testReadingFromSpout() throws Exception {
// The MockDRPCSpout makes messages adds 2 zero based sequences with id foo and given string content.
// It adds to metadata a string JSON with id: "fake" + foo, host: "testHost" and port: a total count of messages.
injectedMockSpout.addMessageParts("foo", "{}", "{'duration': 2000}");
injectedMockSpout.addMessageParts("bar", "{}");
PubSubMessage actual = subscriber.receive();
Assert.assertEquals(actual.getId(), "foo");
Assert.assertEquals(actual.getSequence(), 0);
Assert.assertEquals(actual.getContent(), "{}");
Assert.assertFalse(actual.hasSignal());
Assert.assertTrue(actual.hasMetadata());
Metadata metadata = actual.getMetadata();
Assert.assertEquals(metadata.getContent(), makeReturnInfo("fakefoo", "testHost", 0));
actual = subscriber.receive();
Assert.assertEquals(actual.getId(), "foo");
Assert.assertEquals(actual.getSequence(), 1);
Assert.assertEquals(actual.getContent(), "{'duration': 2000}");
Assert.assertFalse(actual.hasSignal());
Assert.assertTrue(actual.hasMetadata());
metadata = actual.getMetadata();
Assert.assertEquals(metadata.getContent(), makeReturnInfo("fakefoo", "testHost", 1));
actual = subscriber.receive();
Assert.assertEquals(actual.getId(), "bar");
Assert.assertEquals(actual.getSequence(), 0);
Assert.assertEquals(actual.getContent(), "{}");
Assert.assertFalse(actual.hasSignal());
Assert.assertTrue(actual.hasMetadata());
metadata = actual.getMetadata();
Assert.assertEquals(metadata.getContent(), makeReturnInfo("fakebar", "testHost", 2));
}
use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.
the class LoopBolt method execute.
@Override
public void execute(Tuple tuple) {
String id = tuple.getString(TopologyConstants.ID_POSITION);
Metadata metadata = (Metadata) tuple.getValue(TopologyConstants.METADATA_POSITION);
log.info("Looping back metadata with signal {} for {}", metadata.getSignal(), id);
publish(new PubSubMessage(id, null, metadata), tuple);
}
use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.
the class ResultBolt method execute.
@Override
public void execute(Tuple tuple) {
String id = tuple.getString(TopologyConstants.ID_POSITION);
String result = tuple.getString(TopologyConstants.RESULT_POSITION);
Metadata metadata = (Metadata) tuple.getValue(TopologyConstants.RESULT_METADATA_POSITION);
publish(new PubSubMessage(id, result, metadata), tuple);
}
use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.
the class DRPCQuerySubscriber method getMessages.
@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);
}
use of com.yahoo.bullet.pubsub.PubSubMessage in project bullet-storm by yahoo.
the class DRPCQueryResultPubscriber method handleResponse.
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);
}
Aggregations