use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.
the class Emitter method window.
public void window(MessageCollector collector, TaskCoordinator coordinator) {
Integer epoch = getInt(EPOCH);
if (epoch == null) {
resetEpoch();
return;
}
int counter = getInt(COUNT);
if (counter < max) {
logger.info("Emitting: " + counter + ", epoch = " + epoch + ", task = " + taskName);
OutgoingMessageEnvelope envelope = new OutgoingMessageEnvelope(new SystemStream("kafka", "emitted"), Integer.toString(counter), epoch + "-" + taskName.toString());
collector.send(envelope);
this.state.put(COUNT, Integer.toString(getInt(COUNT) + 1));
}
}
use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.
the class NegateNumberTask method process.
@Override
public void process(IncomingMessageEnvelope envelope, MessageCollector collector, TaskCoordinator coordinator) {
messagesProcessed += 1;
String input = (String) envelope.getMessage();
Integer number = Integer.valueOf(input);
Integer output = number.intValue() * -1;
collector.send(new OutgoingMessageEnvelope(outputSystemStream, output.toString()));
if (messagesProcessed >= maxMessages) {
coordinator.shutdown(RequestScope.ALL_TASKS_IN_CONTAINER);
}
}
use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.
the class TestOperatorSpecs method testCreateSinkOperator.
@Test
public void testCreateSinkOperator() {
SystemStream testStream = new SystemStream("test-sys", "test-stream");
SinkFunction<TestMessageEnvelope> sinkFn = (TestMessageEnvelope message, MessageCollector messageCollector, TaskCoordinator taskCoordinator) -> {
messageCollector.send(new OutgoingMessageEnvelope(testStream, message.getKey(), message.getMessage()));
};
SinkOperatorSpec<TestMessageEnvelope> sinkOp = OperatorSpecs.createSinkOperatorSpec(sinkFn, 1);
assertEquals(sinkOp.getSinkFn(), sinkFn);
TestMessageEnvelope mockInput = mock(TestMessageEnvelope.class);
when(mockInput.getKey()).thenReturn("my-test-msg-key");
MessageType mockMsgBody = mock(MessageType.class);
when(mockInput.getMessage()).thenReturn(mockMsgBody);
final List<OutgoingMessageEnvelope> outputMsgs = new ArrayList<>();
MessageCollector mockCollector = mock(MessageCollector.class);
doAnswer(invocation -> {
outputMsgs.add((OutgoingMessageEnvelope) invocation.getArguments()[0]);
return null;
}).when(mockCollector).send(any());
sinkOp.getSinkFn().apply(mockInput, mockCollector, null);
assertEquals(1, outputMsgs.size());
assertEquals(outputMsgs.get(0).getKey(), "my-test-msg-key");
assertEquals(outputMsgs.get(0).getMessage(), mockMsgBody);
assertEquals(sinkOp.getOpCode(), OperatorSpec.OpCode.SINK);
assertEquals(sinkOp.getNextStream(), null);
}
use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.
the class TestCoordinatorStreamWriter method testSendMessage.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testSendMessage() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//check a correct message
assertEquals(0, systemProducer.getEnvelopes().size());
coordinatorStreamWriter.sendMessage("set-config", "key0", "value0");
assertEquals(1, systemProducer.getEnvelopes().size());
//check invalid input is handled
boolean exceptionHappened = false;
try {
coordinatorStreamWriter.sendMessage("invalid-type", "key-invalid", "value-invalid");
} catch (IllegalArgumentException e) {
exceptionHappened = true;
}
assertTrue(exceptionHappened);
assertEquals(1, systemProducer.getEnvelopes().size());
//check sendSetConfigMessage method works correctly
Class[] sendArgs = { String.class, String.class };
Method sendSetConfigMethod = coordinatorStreamWriter.getClass().getDeclaredMethod("sendSetConfigMessage", sendArgs);
sendSetConfigMethod.setAccessible(true);
sendSetConfigMethod.invoke(coordinatorStreamWriter, "key1", "value1");
assertEquals(2, systemProducer.getEnvelopes().size());
//check the messages are correct
List<OutgoingMessageEnvelope> envelopes = systemProducer.getEnvelopes();
OutgoingMessageEnvelope envelope0 = envelopes.get(0);
OutgoingMessageEnvelope envelope1 = envelopes.get(1);
TypeReference<Object[]> keyRef = new TypeReference<Object[]>() {
};
TypeReference<Map<String, Object>> msgRef = new TypeReference<Map<String, Object>>() {
};
assertEquals(2, envelopes.size());
assertEquals("key0", deserialize((byte[]) envelope0.getKey(), keyRef)[CoordinatorStreamMessage.KEY_INDEX]);
Map<String, String> values = (Map<String, String>) deserialize((byte[]) envelope0.getMessage(), msgRef).get("values");
assertEquals("value0", values.get("value"));
assertEquals("key1", deserialize((byte[]) envelope1.getKey(), keyRef)[CoordinatorStreamMessage.KEY_INDEX]);
values = (Map<String, String>) deserialize((byte[]) envelope1.getMessage(), msgRef).get("values");
assertEquals("value1", values.get("value"));
}
use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.
the class DefaultIndexRequestFactoryTest method testGetIndexRequestNoId.
@Test
public void testGetIndexRequestNoId() throws Exception {
IndexRequest indexRequest = indexRequestFactory.getIndexRequest(new OutgoingMessageEnvelope(SYSTEM, EMPTY_MSG));
assertNull(indexRequest.id());
}
Aggregations