Search in sources :

Example 1 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedCommandConsumerFactoryImplIT method getOneWayCommandRecord.

private static KafkaProducerRecord<String, Buffer> getOneWayCommandRecord(final String tenantId, final String deviceId, final String subject) {
    final List<KafkaHeader> headers = List.of(KafkaRecordHelper.createDeviceIdHeader(deviceId), KafkaRecordHelper.createSubjectHeader(subject));
    final String commandTopic = new HonoTopic(HonoTopic.Type.COMMAND, tenantId).toString();
    final KafkaProducerRecord<String, Buffer> record = KafkaProducerRecord.create(commandTopic, deviceId, Buffer.buffer(subject + "_payload"));
    record.addHeaders(headers);
    return record;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader)

Example 2 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaHeadersInjectExtractAdapterTest method testJaegerTracerCanUseAdapter.

/**
 * Verifies that the Jaeger tracer implementation can successfully use the adapters to inject and extract
 * a SpanContext.
 */
@Test
public void testJaegerTracerCanUseAdapter() {
    final Configuration config = new Configuration("test");
    final Tracer tracer = config.getTracer();
    final Span span = tracer.buildSpan("do").start();
    final List<KafkaHeader> headers = new ArrayList<>();
    final KafkaHeadersInjectAdapter injectAdapter = new KafkaHeadersInjectAdapter(headers);
    tracer.inject(span.context(), Format.Builtin.TEXT_MAP, injectAdapter);
    final SpanContext context = tracer.extract(Format.Builtin.TEXT_MAP, new KafkaHeadersExtractAdapter(headers));
    assertThat(context.toSpanId()).isEqualTo(span.context().toSpanId());
}
Also used : SpanContext(io.opentracing.SpanContext) Configuration(io.jaegertracing.Configuration) Tracer(io.opentracing.Tracer) ArrayList(java.util.ArrayList) Span(io.opentracing.Span) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test)

Example 3 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader 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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpURLConnection(java.net.HttpURLConnection) Json(io.vertx.core.json.Json) RecordMetadata(io.vertx.kafka.client.producer.RecordMetadata) Lifecycle(org.eclipse.hono.util.Lifecycle) LoggerFactory(org.slf4j.LoggerFactory) KafkaClientFactory(org.eclipse.hono.client.kafka.KafkaClientFactory) Tags(io.opentracing.tag.Tags) KafkaProducer(io.vertx.kafka.client.producer.KafkaProducer) ArrayList(java.util.ArrayList) Status(io.vertx.ext.healthchecks.Status) MessagingType(org.eclipse.hono.util.MessagingType) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) References(io.opentracing.References) Map(java.util.Map) TracingHelper(org.eclipse.hono.tracing.TracingHelper) MessagingClient(org.eclipse.hono.util.MessagingClient) Strings(org.eclipse.hono.util.Strings) KafkaTracingHelper(org.eclipse.hono.client.kafka.tracing.KafkaTracingHelper) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) ServerErrorException(org.eclipse.hono.client.ServerErrorException) UUID(java.util.UUID) KafkaRecordHelper(org.eclipse.hono.client.kafka.KafkaRecordHelper) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) ServiceClient(org.eclipse.hono.client.util.ServiceClient) Buffer(io.vertx.core.buffer.Buffer) EncodeException(io.vertx.core.json.EncodeException) Span(io.opentracing.Span) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) KafkaProducerRecord(io.vertx.kafka.client.producer.KafkaProducerRecord) ServerErrorException(org.eclipse.hono.client.ServerErrorException)

Example 4 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedMappingAndDelegatingCommandHandlerTest method getCommandRecord.

@SuppressWarnings("unchecked")
private KafkaConsumerRecord<String, Buffer> getCommandRecord(final String tenantId, final String deviceId, final String subject, final int partition, final long offset) {
    final List<KafkaHeader> headers = new ArrayList<>();
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND, tenantId).toString();
    final KafkaConsumerRecord<String, Buffer> consumerRecord = mock(KafkaConsumerRecord.class);
    Optional.ofNullable(deviceId).ifPresent(ok -> headers.add(KafkaRecordHelper.createDeviceIdHeader(deviceId)));
    Optional.ofNullable(subject).ifPresent(ok -> headers.add(KafkaRecordHelper.createSubjectHeader(subject)));
    when(consumerRecord.headers()).thenReturn(headers);
    when(consumerRecord.topic()).thenReturn(topic);
    when(consumerRecord.key()).thenReturn(deviceId);
    when(consumerRecord.partition()).thenReturn(partition);
    when(consumerRecord.offset()).thenReturn(offset);
    return consumerRecord;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader)

Example 5 with KafkaHeader

use of io.vertx.kafka.client.producer.KafkaHeader in project hono by eclipse.

the class KafkaBasedCommandTest method testFromRoutedCommandRecordSucceeds.

/**
 * Verifies that a command can be created from a valid record representing a routed command
 * message with a <em>via</em> header containing a gateway identifier.
 */
@Test
public void testFromRoutedCommandRecordSucceeds() {
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND_INTERNAL, "myAdapterInstanceId").toString();
    final String correlationId = "the-correlation-id";
    final String gatewayId = "gw-1";
    final String targetDeviceId = "4711";
    final String subject = "doThis";
    final List<KafkaHeader> headers = new ArrayList<>(getHeaders(targetDeviceId, subject, correlationId));
    headers.add(KafkaRecordHelper.createViaHeader(gatewayId));
    headers.add(KafkaRecordHelper.createTenantIdHeader(Constants.DEFAULT_TENANT));
    final KafkaConsumerRecord<String, Buffer> commandRecord = getCommandRecord(topic, targetDeviceId, headers);
    final KafkaBasedCommand cmd = KafkaBasedCommand.fromRoutedCommandRecord(commandRecord);
    assertTrue(cmd.isValid());
    assertThat(cmd.getName()).isEqualTo(subject);
    assertThat(cmd.getDeviceId()).isEqualTo(targetDeviceId);
    assertThat(cmd.getGatewayOrDeviceId()).isEqualTo(gatewayId);
    assertThat(cmd.getGatewayId()).isEqualTo(gatewayId);
    assertThat(cmd.getCorrelationId()).isEqualTo(correlationId);
    assertTrue(cmd.isOneWay());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) KafkaHeader(io.vertx.kafka.client.producer.KafkaHeader) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaHeader (io.vertx.kafka.client.producer.KafkaHeader)15 Buffer (io.vertx.core.buffer.Buffer)14 Test (org.junit.jupiter.api.Test)11 ArrayList (java.util.ArrayList)10 HonoTopic (org.eclipse.hono.client.kafka.HonoTopic)10 Span (io.opentracing.Span)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 SpanContext (io.opentracing.SpanContext)2 Tracer (io.opentracing.Tracer)2 CommandContext (org.eclipse.hono.client.command.CommandContext)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 Configuration (io.jaegertracing.Configuration)1 References (io.opentracing.References)1 Tags (io.opentracing.tag.Tags)1 Future (io.vertx.core.Future)1 EncodeException (io.vertx.core.json.EncodeException)1 Json (io.vertx.core.json.Json)1 HealthCheckHandler (io.vertx.ext.healthchecks.HealthCheckHandler)1 Status (io.vertx.ext.healthchecks.Status)1 KafkaProducer (io.vertx.kafka.client.producer.KafkaProducer)1