Search in sources :

Example 1 with MessagingKafkaConsumerConfigProperties

use of org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties in project hono by eclipse.

the class HonoExampleApplicationBase method createKafkaApplicationClient.

/**
 * Creates an application client for Kafka based messaging. Unlike with AMQP, the Kafka clients manage their
 * connections to the cluster internally.
 * <p>
 * NB: if you want to integrate this code with your own software, it might be necessary to copy the trust store to
 * your project as well and adopt the file path.
 */
private ApplicationClient<? extends MessageContext> createKafkaApplicationClient() {
    final Map<String, String> properties = new HashMap<>();
    properties.put("bootstrap.servers", HonoExampleConstants.HONO_MESSAGING_HOST + ":" + port);
    // add the following lines with appropriate values to enable TLS
    // properties.put("ssl.truststore.location", "/path/to/file");
    // properties.put("ssl.truststore.password", "secret");
    final CommonKafkaClientConfigProperties commonClientConfig = new CommonKafkaClientConfigProperties();
    commonClientConfig.setCommonClientConfig(properties);
    final MessagingKafkaConsumerConfigProperties consumerConfig = new MessagingKafkaConsumerConfigProperties();
    consumerConfig.setCommonClientConfig(commonClientConfig);
    consumerConfig.setConsumerConfig(Map.of("group.id", KAFKA_CONSUMER_GROUP_ID));
    final MessagingKafkaProducerConfigProperties producerConfig = new MessagingKafkaProducerConfigProperties();
    producerConfig.setCommonClientConfig(commonClientConfig);
    final KafkaProducerFactory<String, Buffer> producerFactory = CachingKafkaProducerFactory.sharedFactory(vertx);
    return new KafkaApplicationClientImpl(vertx, consumerConfig, producerFactory, producerConfig);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties) HashMap(java.util.HashMap) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties) KafkaApplicationClientImpl(org.eclipse.hono.application.client.kafka.impl.KafkaApplicationClientImpl)

Example 2 with MessagingKafkaConsumerConfigProperties

use of org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties in project hono by eclipse.

the class KafkaApplicationClientImplTest method setUp.

/**
 * Sets up fixture.
 *
 * @param vertx The vert.x instance to use.
 */
@BeforeEach
void setUp(final Vertx vertx) {
    final MockProducer<String, Buffer> mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final CachingKafkaProducerFactory<String, Buffer> producerFactory = CachingKafkaProducerFactory.testFactory(vertx, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    tenantId = UUID.randomUUID().toString();
    mockConsumer = new KafkaMockConsumer(OffsetResetStrategy.EARLIEST);
    final CommonKafkaClientConfigProperties commonConfig = new CommonKafkaClientConfigProperties();
    commonConfig.setCommonClientConfig(Map.of(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, "kafka"));
    final MessagingKafkaConsumerConfigProperties consumerConfig = new MessagingKafkaConsumerConfigProperties();
    consumerConfig.setCommonClientConfig(commonConfig);
    consumerConfig.setConsumerConfig(Map.of("client.id", "application-test-consumer"));
    final MessagingKafkaProducerConfigProperties producerConfig = new MessagingKafkaProducerConfigProperties();
    producerConfig.setCommonClientConfig(commonConfig);
    producerConfig.setProducerConfig(Map.of("client.id", "application-test-sender"));
    client = new KafkaApplicationClientImpl(vertx, consumerConfig, producerFactory, producerConfig);
    client.setKafkaConsumerFactory(() -> mockConsumer);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties) KafkaMockConsumer(org.eclipse.hono.kafka.test.KafkaMockConsumer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with MessagingKafkaConsumerConfigProperties

use of org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties in project hono by eclipse.

the class KafkaBasedCommandConsumerFactoryImplIT method getKafkaBasedCommandConsumerFactory.

private KafkaBasedCommandConsumerFactoryImpl getKafkaBasedCommandConsumerFactory(final Supplier<Future<Void>> targetAdapterInstanceGetterCompletionFutureSupplier, final String tenantToHandleCommandsFor) {
    final KafkaProducerFactory<String, Buffer> producerFactory = CachingKafkaProducerFactory.sharedFactory(vertx);
    final TenantClient tenantClient = getTenantClient();
    final CommandTargetMapper commandTargetMapper = new CommandTargetMapper() {

        @Override
        public Future<JsonObject> getTargetGatewayAndAdapterInstance(final String tenantId, final String deviceId, final SpanContext context) {
            final JsonObject jsonObject = new JsonObject();
            jsonObject.put(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID, adapterInstanceId);
            jsonObject.put(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId);
            if (!tenantId.equals(tenantToHandleCommandsFor)) {
                return Future.failedFuture("ignoring command for other tenant " + tenantId);
            }
            if (targetAdapterInstanceGetterCompletionFutureSupplier == null) {
                return Future.succeededFuture(jsonObject);
            }
            return targetAdapterInstanceGetterCompletionFutureSupplier.get().map(jsonObject);
        }
    };
    final Span span = TracingMockSupport.mockSpan();
    final Tracer tracer = TracingMockSupport.mockTracer(span);
    final MessagingKafkaConsumerConfigProperties kafkaConsumerConfig = new MessagingKafkaConsumerConfigProperties();
    kafkaConsumerConfig.setConsumerConfig(Map.of(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, IntegrationTestSupport.DOWNSTREAM_BOOTSTRAP_SERVERS));
    final CommandRouterMetrics metrics = mock(CommandRouterMetrics.class);
    when(metrics.startTimer()).thenReturn(Timer.start());
    final var kafkaBasedCommandConsumerFactoryImpl = new KafkaBasedCommandConsumerFactoryImpl(vertx, tenantClient, commandTargetMapper, producerFactory, IntegrationTestSupport.getKafkaProducerConfig(), IntegrationTestSupport.getKafkaProducerConfig(), kafkaConsumerConfig, metrics, NoopKafkaClientMetricsSupport.INSTANCE, tracer, null);
    kafkaBasedCommandConsumerFactoryImpl.setGroupId(commandRouterGroupId);
    componentsToStopAfterTest.add(kafkaBasedCommandConsumerFactoryImpl);
    return kafkaBasedCommandConsumerFactoryImpl;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) KafkaBasedCommandConsumerFactoryImpl(org.eclipse.hono.commandrouter.impl.kafka.KafkaBasedCommandConsumerFactoryImpl) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span) MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties) CommandRouterMetrics(org.eclipse.hono.commandrouter.CommandRouterMetrics) CommandTargetMapper(org.eclipse.hono.commandrouter.CommandTargetMapper) TenantClient(org.eclipse.hono.client.registry.TenantClient)

Example 4 with MessagingKafkaConsumerConfigProperties

use of org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties in project hono by eclipse.

the class KafkaBasedCommandSenderTest method setUp.

/**
 * Sets up fixture.
 *
 * @param vertx The vert.x instance to use.
 */
@BeforeEach
void setUp(final Vertx vertx) {
    this.vertx = vertx;
    consumerConfig = new MessagingKafkaConsumerConfigProperties();
    mockConsumer = new KafkaMockConsumer(OffsetResetStrategy.LATEST);
    producerConfig = new MessagingKafkaProducerConfigProperties();
    producerConfig.setProducerConfig(Map.of("client.id", "application-test-sender"));
    span = TracingMockSupport.mockSpan();
    tracer = TracingMockSupport.mockTracer(span);
    mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final var producerFactory = CachingKafkaProducerFactory.testFactory(vertx, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    commandSender = new KafkaBasedCommandSender(vertx, consumerConfig, producerFactory, producerConfig, tracer);
    tenantId = UUID.randomUUID().toString();
    deviceId = UUID.randomUUID().toString();
}
Also used : MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) KafkaMockConsumer(org.eclipse.hono.kafka.test.KafkaMockConsumer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with MessagingKafkaConsumerConfigProperties

use of org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties in project hono by eclipse.

the class IntegrationTestSupport method getKafkaConsumerConfig.

/**
 * Creates properties for connecting a consumer to Kafka.
 *
 * @return The properties.
 */
public static MessagingKafkaConsumerConfigProperties getKafkaConsumerConfig() {
    LOGGER.info("Configured to connect to Kafka on {}", IntegrationTestSupport.DOWNSTREAM_BOOTSTRAP_SERVERS);
    final MessagingKafkaConsumerConfigProperties consumerConfig = new MessagingKafkaConsumerConfigProperties();
    consumerConfig.setConsumerConfig(Map.of(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, IntegrationTestSupport.DOWNSTREAM_BOOTSTRAP_SERVERS, ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest", ConsumerConfig.GROUP_ID_CONFIG, "its-" + UUID.randomUUID()));
    return consumerConfig;
}
Also used : MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties)

Aggregations

MessagingKafkaConsumerConfigProperties (org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties)6 Buffer (io.vertx.core.buffer.Buffer)3 MessagingKafkaProducerConfigProperties (org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties)3 CommonKafkaClientConfigProperties (org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties)2 KafkaMockConsumer (org.eclipse.hono.kafka.test.KafkaMockConsumer)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 JsonObject (io.vertx.core.json.JsonObject)1 HashMap (java.util.HashMap)1 KafkaApplicationClientImpl (org.eclipse.hono.application.client.kafka.impl.KafkaApplicationClientImpl)1 TenantClient (org.eclipse.hono.client.registry.TenantClient)1 CommandRouterMetrics (org.eclipse.hono.commandrouter.CommandRouterMetrics)1 CommandTargetMapper (org.eclipse.hono.commandrouter.CommandTargetMapper)1 KafkaBasedCommandConsumerFactoryImpl (org.eclipse.hono.commandrouter.impl.kafka.KafkaBasedCommandConsumerFactoryImpl)1 ConfigurationProperties (org.springframework.boot.context.properties.ConfigurationProperties)1 Bean (org.springframework.context.annotation.Bean)1 Profile (org.springframework.context.annotation.Profile)1