use of io.vertx.kafka.client.producer.RecordMetadata in project hono by eclipse.
the class AbstractKafkaBasedMessageSender method sendAndWaitForOutcome.
/**
* Sends a message to a Kafka broker and waits for the outcome.
*
* @param topic The topic to send the message to.
* @param tenantId The tenant that the device belongs to.
* @param deviceId The device identifier.
* @param payload The data to send or {@code null} if the message has no payload.
* @param headers Additional meta data that should be included in the message.
* @param currentSpan The <em>OpenTracing</em> span used to use for tracking the sending of the message.
* The span will <em>not</em> be finished by this method.
* @return A future indicating the outcome of the operation.
* <p>
* The future will be succeeded if the message has been sent.
* <p>
* The future will be failed with a {@link org.eclipse.hono.client.ServerErrorException} if the data could
* not be sent. The error code contained in the exception indicates the cause of the failure.
* @throws NullPointerException if topic, tenantId, deviceId, headers or span are {@code null}.
*/
protected final Future<Void> sendAndWaitForOutcome(final String topic, final String tenantId, final String deviceId, final Buffer payload, final List<KafkaHeader> headers, final Span currentSpan) {
Objects.requireNonNull(topic);
Objects.requireNonNull(tenantId);
Objects.requireNonNull(deviceId);
Objects.requireNonNull(headers);
Objects.requireNonNull(currentSpan);
if (stopped) {
return Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "sender already stopped"));
}
final KafkaProducerRecord<String, Buffer> record = KafkaProducerRecord.create(topic, deviceId, payload);
log.trace("sending message to Kafka [topic: {}, tenantId: {}, deviceId: {}]", topic, tenantId, deviceId);
record.addHeaders(headers);
KafkaTracingHelper.injectSpanContext(tracer, record, currentSpan.context());
logProducerRecord(currentSpan, record);
return getOrCreateProducer().send(record).onSuccess(recordMetadata -> logRecordMetadata(currentSpan, deviceId, recordMetadata)).otherwise(t -> {
logError(currentSpan, topic, tenantId, deviceId, t);
throw new ServerErrorException(tenantId, getErrorCode(t), t);
}).mapEmpty();
}
use of io.vertx.kafka.client.producer.RecordMetadata in project hono by eclipse.
the class KafkaClientUnitTestHelper method newKafkaProducer.
/**
* Returns a new {@link KafkaProducer}.
*
* @param producer The mock producer to wrap.
* @param <K> The type of the key.
* @param <V> The type of the value.
* @return The new Kafka producer.
*/
public static <K, V> KafkaProducer<K, V> newKafkaProducer(final MockProducer<K, V> producer) {
final VertxInternal vertxMock = mock(VertxInternal.class);
final ContextInternal context = VertxMockSupport.mockContextInternal(vertxMock);
doAnswer(invocation -> Promise.promise()).when(context).promise();
doAnswer(invocation -> {
final Promise<RecordMetadata> result = Promise.promise();
final Handler<Promise<RecordMetadata>> handler = invocation.getArgument(0);
handler.handle(result);
return result.future();
}).when(context).executeBlocking(VertxMockSupport.anyHandler());
VertxMockSupport.executeBlockingCodeImmediately(vertxMock, context);
return KafkaProducer.create(vertxMock, producer);
}
Aggregations