Search in sources :

Example 1 with CommonKafkaClientConfigProperties

use of org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties 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 CommonKafkaClientConfigProperties

use of org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties 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 CommonKafkaClientConfigProperties

use of org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties in project hono by eclipse.

the class NotificationKafkaConsumerConfigPropertiesTest method testThatGetConsumerConfigReturnsGivenPropertiesWithCommonProperties.

/**
 * Verifies that properties provided with {@link NotificationKafkaConsumerConfigProperties#setConsumerConfig(Map)}
 * and {@link AbstractKafkaConfigProperties#setCommonClientConfig(CommonKafkaClientConfigProperties)} are returned
 * in {@link NotificationKafkaConsumerConfigProperties#getConsumerConfig(String)}, with the consumer config
 * properties having precedence.
 */
@Test
public void testThatGetConsumerConfigReturnsGivenPropertiesWithCommonProperties() {
    final CommonKafkaClientConfigProperties commonConfig = new CommonKafkaClientConfigProperties();
    commonConfig.setCommonClientConfig(Map.of("foo", "toBeOverridden", "common", "commonValue"));
    final NotificationKafkaConsumerConfigProperties config = new NotificationKafkaConsumerConfigProperties();
    config.setCommonClientConfig(commonConfig);
    config.setConsumerConfig(Map.of("foo", "bar"));
    final Map<String, String> consumerConfig = config.getConsumerConfig("consumerName");
    assertThat(consumerConfig.get("foo")).isEqualTo("bar");
    assertThat(consumerConfig.get("common")).isEqualTo("commonValue");
}
Also used : CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties) Test(org.junit.jupiter.api.Test)

Example 4 with CommonKafkaClientConfigProperties

use of org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties in project hono by eclipse.

the class NotificationKafkaProducerConfigPropertiesTest method testThatGetProducerConfigReturnsGivenPropertiesWithCommonProperties.

/**
 * Verifies that properties provided with {@link NotificationKafkaProducerConfigProperties#setProducerConfig(Map)}
 * and {@link AbstractKafkaConfigProperties#setCommonClientConfig(CommonKafkaClientConfigProperties)} are returned
 * in {@link NotificationKafkaProducerConfigProperties#getProducerConfig(String)}, with the producer config
 * properties having precedence.
 */
@Test
public void testThatGetProducerConfigReturnsGivenPropertiesWithCommonProperties() {
    final CommonKafkaClientConfigProperties commonConfig = new CommonKafkaClientConfigProperties();
    commonConfig.setCommonClientConfig(Map.of("foo", "toBeOverridden", "common", "commonValue"));
    final NotificationKafkaProducerConfigProperties config = new NotificationKafkaProducerConfigProperties();
    config.setCommonClientConfig(commonConfig);
    config.setProducerConfig(Map.of("foo", "bar"));
    final Map<String, String> producerConfig = config.getProducerConfig("producerName");
    assertThat(producerConfig.get("foo")).isEqualTo("bar");
    assertThat(producerConfig.get("common")).isEqualTo("commonValue");
}
Also used : CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties) Test(org.junit.jupiter.api.Test)

Example 5 with CommonKafkaClientConfigProperties

use of org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties in project hono by eclipse.

the class KafkaProducerConfigPropertiesTest method testThatGetProducerConfigReturnsGivenPropertiesWithCommonProperties.

/**
 * Verifies that properties provided with {@link KafkaProducerConfigProperties#setProducerConfig(Map)} and
 * {@link AbstractKafkaConfigProperties#setCommonClientConfig(CommonKafkaClientConfigProperties)} are returned in
 * {@link KafkaProducerConfigProperties#getProducerConfig(String)}, with the producer config properties having
 * precedence.
 */
@Test
public void testThatGetProducerConfigReturnsGivenPropertiesWithCommonProperties() {
    final CommonKafkaClientConfigProperties commonConfig = new CommonKafkaClientConfigProperties();
    commonConfig.setCommonClientConfig(Map.of("foo", "toBeOverridden", "common", "commonValue"));
    config.setCommonClientConfig(commonConfig);
    config.setProducerConfig(Map.of("foo", "bar"));
    final Map<String, String> producerConfig = config.getProducerConfig("producerName");
    assertThat(producerConfig.get("foo")).isEqualTo("bar");
    assertThat(producerConfig.get("common")).isEqualTo("commonValue");
}
Also used : CommonKafkaClientConfigProperties(org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties) Test(org.junit.jupiter.api.Test)

Aggregations

CommonKafkaClientConfigProperties (org.eclipse.hono.client.kafka.CommonKafkaClientConfigProperties)6 Test (org.junit.jupiter.api.Test)4 Buffer (io.vertx.core.buffer.Buffer)2 MessagingKafkaConsumerConfigProperties (org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties)2 MessagingKafkaProducerConfigProperties (org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties)2 HashMap (java.util.HashMap)1 KafkaApplicationClientImpl (org.eclipse.hono.application.client.kafka.impl.KafkaApplicationClientImpl)1 KafkaMockConsumer (org.eclipse.hono.kafka.test.KafkaMockConsumer)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1