use of org.apache.kafka.clients.producer.Callback in project nakadi by zalando.
the class KafkaTopicRepositoryTest method whenKafkaPublishCallbackWithExceptionThenEventPublishingException.
@Test
public void whenKafkaPublishCallbackWithExceptionThenEventPublishingException() throws Exception {
final BatchItem firstItem = new BatchItem("{}", BatchItem.EmptyInjectionConfiguration.build(1, true), new BatchItem.InjectionConfiguration[BatchItem.Injection.values().length], Collections.emptyList());
firstItem.setPartition("1");
final BatchItem secondItem = new BatchItem("{}", BatchItem.EmptyInjectionConfiguration.build(1, true), new BatchItem.InjectionConfiguration[BatchItem.Injection.values().length], Collections.emptyList());
secondItem.setPartition("2");
final List<BatchItem> batch = ImmutableList.of(firstItem, secondItem);
when(kafkaProducer.partitionsFor(EXPECTED_PRODUCER_RECORD.topic())).thenReturn(ImmutableList.of(new PartitionInfo(EXPECTED_PRODUCER_RECORD.topic(), 1, new Node(1, "host", 9091), null, null), new PartitionInfo(EXPECTED_PRODUCER_RECORD.topic(), 2, new Node(1, "host", 9091), null, null)));
when(kafkaProducer.send(any(), any())).thenAnswer(invocation -> {
final ProducerRecord record = (ProducerRecord) invocation.getArguments()[0];
final Callback callback = (Callback) invocation.getArguments()[1];
if (record.partition() == 2) {
// return exception only for second event
callback.onCompletion(null, new Exception());
} else {
callback.onCompletion(null, null);
}
return null;
});
try {
kafkaTopicRepository.syncPostBatch(EXPECTED_PRODUCER_RECORD.topic(), batch);
fail();
} catch (final EventPublishingException e) {
assertThat(firstItem.getResponse().getPublishingStatus(), equalTo(EventPublishingStatus.SUBMITTED));
assertThat(firstItem.getResponse().getDetail(), equalTo(""));
assertThat(secondItem.getResponse().getPublishingStatus(), equalTo(EventPublishingStatus.FAILED));
assertThat(secondItem.getResponse().getDetail(), equalTo("internal error"));
}
}
use of org.apache.kafka.clients.producer.Callback in project open-kilda by telstra.
the class KafkaBolt method execute.
@Override
public void execute(final Tuple input) {
K key = null;
V message = null;
String topic = null;
try {
key = mapper.getKeyFromTuple(input);
message = mapper.getMessageFromTuple(input);
topic = topicSelector.getTopic(input);
if (topic != null) {
Callback callback = null;
if (!fireAndForget && async) {
callback = new Callback() {
@Override
public void onCompletion(RecordMetadata ignored, Exception e) {
synchronized (collector) {
if (e != null) {
collector.reportError(e);
collector.fail(input);
} else {
collector.ack(input);
}
}
}
};
}
Future<RecordMetadata> result = producer.send(new ProducerRecord<K, V>(topic, key, message), callback);
if (!async) {
try {
result.get();
collector.ack(input);
} catch (ExecutionException err) {
collector.reportError(err);
collector.fail(input);
}
} else if (fireAndForget) {
collector.ack(input);
}
} else {
LOG.warn("skipping key = " + key + ", topic selector returned null.");
collector.ack(input);
}
} catch (Exception ex) {
collector.reportError(ex);
collector.fail(input);
}
}
use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.
the class TracingCallbackTest method on_completion_should_tag_if_exception.
@Test
public void on_completion_should_tag_if_exception() {
Span span = tracing.tracer().nextSpan().start();
Callback tracingCallback = TracingCallback.create(null, span, currentTraceContext);
tracingCallback.onCompletion(null, error);
assertThat(spans.get(0).finishTimestamp()).isNotZero();
assertThat(spans.get(0).error()).isEqualTo(error);
}
use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.
the class TracingCallbackTest method on_completion_should_forward_then_tag_if_exception.
@Test
public void on_completion_should_forward_then_tag_if_exception() {
Span span = tracing.tracer().nextSpan().start();
Callback delegate = mock(Callback.class);
Callback tracingCallback = TracingCallback.create(delegate, span, currentTraceContext);
RecordMetadata md = createRecordMetadata();
tracingCallback.onCompletion(md, error);
verify(delegate).onCompletion(md, error);
assertThat(spans.get(0).finishTimestamp()).isNotZero();
assertThat(spans.get(0).error()).isEqualTo(error);
}
use of org.apache.kafka.clients.producer.Callback in project brave by openzipkin.
the class TracingCallbackTest method on_completion_should_have_span_in_scope.
@Test
public void on_completion_should_have_span_in_scope() {
Span span = tracing.tracer().nextSpan().start();
Callback delegate = (metadata, exception) -> assertThat(currentTraceContext.get()).isSameAs(span.context());
TracingCallback.create(delegate, span, currentTraceContext).onCompletion(createRecordMetadata(), null);
assertThat(spans.get(0).finishTimestamp()).isNotZero();
}
Aggregations