|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.kafka; |
| 7 | + |
| 8 | +import javax.annotation.Nonnull; |
| 9 | + |
| 10 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 11 | + |
| 12 | +import io.jooby.Extension; |
| 13 | +import io.jooby.Jooby; |
| 14 | + |
| 15 | +/** |
| 16 | + * Kafka consumer module: https://jooby.io/modules/kafka. |
| 17 | + * <p> |
| 18 | + * Usage: |
| 19 | + * </p> |
| 20 | + * |
| 21 | + * <pre>{@code |
| 22 | + * { |
| 23 | + * install(new KafkaConsumerModule()); |
| 24 | + * |
| 25 | + * get("/", ctx -> { |
| 26 | + * KafkaConsumer producer = require(KafkaConsumer.class); |
| 27 | + * // work with consumer |
| 28 | + * }); |
| 29 | + * } |
| 30 | + * }</pre> |
| 31 | + * |
| 32 | + * application.conf: |
| 33 | + * Creates a new kafka consumer module using the <code>kafka.consumer</code> property key. |
| 34 | + * This key must be present in the application configuration file, like: |
| 35 | + * |
| 36 | + * <pre>{@code |
| 37 | + * kafka.consumer.bootstrap.servers = "localhost:9092" |
| 38 | + * kafka.consumer.group.id = "group A" |
| 39 | + * kafka.consumer.key.deserializer = "org.apache.kafka.common.serialization.StringDeserializer" |
| 40 | + * kafka.consumer.value.deserializer = "org.apache.kafka.common.serialization.StringDeserializer" |
| 41 | + * }</pre> |
| 42 | + * |
| 43 | + * @author edgar |
| 44 | + * @since 2.9.3 |
| 45 | + */ |
| 46 | +public class KafkaConsumerModule implements Extension { |
| 47 | + private final String key; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new kafka consumer module. |
| 51 | + * |
| 52 | + * @param key Kafka key. |
| 53 | + */ |
| 54 | + public KafkaConsumerModule(@Nonnull String key) { |
| 55 | + this.key = key; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a new kafka consumer module. Uses the default key: <code>kafka.consumer</code>. |
| 60 | + */ |
| 61 | + public KafkaConsumerModule() { |
| 62 | + this("kafka.consumer"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override public void install(@Nonnull Jooby application) { |
| 66 | + KafkaHelper.install(application, key, KafkaConsumer::new); |
| 67 | + } |
| 68 | +} |
0 commit comments