use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedCommandResponseSender method sendCommandResponse.
@Override
public Future<Void> sendCommandResponse(final TenantObject tenant, final RegistrationAssertion device, final CommandResponse response, final SpanContext context) {
Objects.requireNonNull(tenant);
Objects.requireNonNull(device);
Objects.requireNonNull(response);
if (log.isTraceEnabled()) {
log.trace("publish command response [{}]", response);
}
final String topic = new HonoTopic(HonoTopic.Type.COMMAND_RESPONSE, response.getTenantId()).toString();
final Span span = startChildSpan("forward Command response", topic, response.getTenantId(), response.getDeviceId(), context);
if (response.getMessagingType() != getMessagingType()) {
span.log(String.format("using messaging type %s instead of type %s used for the original command", getMessagingType(), response.getMessagingType()));
}
return sendAndWaitForOutcome(topic, response.getTenantId(), response.getDeviceId(), response.getPayload(), getHeaders(response, tenant, device), span).onComplete(ar -> span.finish());
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaApplicationClientImplTest method createConsumer.
private Future<MessageConsumer> createConsumer(final String tenantId, final Type type, final Handler<DownstreamMessage<KafkaMessageContext>> msgHandler, final Handler<Throwable> closeHandler) {
final String topic = new HonoTopic(type, tenantId).toString();
final TopicPartition topicPartition = new TopicPartition(topic, 0);
mockConsumer.updateBeginningOffsets(Map.of(topicPartition, ((long) 0)));
mockConsumer.updatePartitions(topicPartition, KafkaMockConsumer.DEFAULT_NODE);
mockConsumer.setRebalancePartitionAssignmentAfterSubscribe(List.of(topicPartition));
switch(type) {
case TELEMETRY:
return client.createTelemetryConsumer(tenantId, msgHandler, closeHandler);
case EVENT:
return client.createEventConsumer(tenantId, msgHandler, closeHandler);
default:
return client.createCommandResponseConsumer(tenantId, null, msgHandler, closeHandler);
}
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedEventSenderTest method testSendEventCreatesCorrectRecord.
/**
* Verifies that the Kafka record is created as expected.
*
* @param ctx The vert.x test context.
*/
@Test
public void testSendEventCreatesCorrectRecord(final VertxTestContext ctx) {
// GIVEN a sender
final String contentType = "text/plain";
final String payload = "the-payload";
final Map<String, Object> properties = Map.of("foo", "bar", MessageHelper.SYS_HEADER_PROPERTY_TTL, 5);
final var span = TracingMockSupport.mockSpan();
final var tracer = TracingMockSupport.mockTracer(span);
final var mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
final var factory = newProducerFactory(mockProducer);
final var sender = new KafkaBasedEventSender(vertxMock, factory, kafkaProducerConfig, true, tracer);
// WHEN sending a message
sender.sendEvent(tenant, device, contentType, Buffer.buffer(payload), properties, null).onComplete(ctx.succeeding(t -> {
ctx.verify(() -> {
// THEN the producer record is created from the given values...
final var producerRecord = mockProducer.history().get(0);
assertThat(producerRecord.key()).isEqualTo(device.getDeviceId());
assertThat(producerRecord.topic()).isEqualTo(new HonoTopic(HonoTopic.Type.EVENT, tenant.getTenantId()).toString());
assertThat(producerRecord.value().toString()).isEqualTo(payload);
KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), "foo", "bar");
KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), MessageHelper.SYS_HEADER_PROPERTY_TTL, 5000L);
// ...AND contains the standard headers
KafkaClientUnitTestHelper.assertStandardHeaders(producerRecord, device.getDeviceId(), contentType, QoS.AT_LEAST_ONCE.ordinal());
verify(span).finish();
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class AbstractKafkaBasedDownstreamSender method removeTenantTopicBasedProducerMetrics.
private void removeTenantTopicBasedProducerMetrics(final KafkaProducer<String, Buffer> producer, final String tenantId) {
final HonoTopic topic = new HonoTopic(getTopicType(), tenantId);
KafkaProducerHelper.removeTopicMetrics(producer, Stream.of(topic.toString()));
}
use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.
the class KafkaBasedTelemetrySender method sendTelemetry.
@Override
public Future<Void> sendTelemetry(final TenantObject tenant, final RegistrationAssertion device, final QoS qos, final String contentType, final Buffer payload, final Map<String, Object> properties, final SpanContext context) {
Objects.requireNonNull(tenant);
Objects.requireNonNull(device);
Objects.requireNonNull(qos);
if (log.isTraceEnabled()) {
log.trace("send telemetry data [tenantId: {}, deviceId: {}, qos: {}, contentType: {}, properties: {}]", tenant.getTenantId(), device.getDeviceId(), qos, contentType, properties);
}
final HonoTopic topic = new HonoTopic(HonoTopic.Type.TELEMETRY, tenant.getTenantId());
final Map<String, Object> propsWithDefaults = addDefaults(topic.getType().endpoint, tenant, device, qos, contentType, payload, properties);
final String topicName = topic.toString();
final Span currentSpan = startSpan("forward Telemetry", topicName, tenant.getTenantId(), device.getDeviceId(), qos == QoS.AT_MOST_ONCE ? References.FOLLOWS_FROM : References.CHILD_OF, context);
final var outcome = sendAndWaitForOutcome(topic.toString(), tenant.getTenantId(), device.getDeviceId(), payload, propsWithDefaults, currentSpan).onComplete(ar -> currentSpan.finish());
if (qos == QoS.AT_MOST_ONCE) {
return Future.succeededFuture();
} else {
return outcome;
}
}
Aggregations