Skip to content

Commit b9098a7

Browse files
committed
Kafka module cleanup
1 parent 8a90204 commit b9098a7

File tree

9 files changed

+306
-107
lines changed

9 files changed

+306
-107
lines changed

docs/asciidoc/modules/kafka.adoc

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
== Kafka
2+
3+
Kafka module for https://kafka.apache.org/documentation
4+
5+
=== Usage
6+
7+
1) Add the dependency:
8+
9+
[dependency, artifactId="jooby-kafka"]
10+
.
11+
12+
2) Setup properties:
13+
14+
.application.conf
15+
[source, properties]
16+
----
17+
kafka.producer.bootstrap.servers = "localhost:9092"
18+
kafka.producer.key.serializer = "org.apache.kafka.common.serialization.StringSerializer"
19+
kafka.producer.value.serializer = "org.apache.kafka.common.serialization.StringSerializer"
20+
21+
kafka.consumer.bootstrap.servers = "localhost:9092"
22+
kafka.consumer.key.deserializer = "org.apache.kafka.common.serialization.StringDeserializer"
23+
kafka.consumer.value.deserializer = "org.apache.kafka.common.serialization.StringDeserializer"
24+
----
25+
26+
3) Install
27+
28+
.Java
29+
[source, java, role="primary"]
30+
----
31+
import io.jooby.kafka.KafkaModule;
32+
33+
{
34+
install(new KafkaModule()); <1>
35+
36+
get("/", ctx -> {
37+
KafkaProducer producer = require(KafkaProducer.class); <2>
38+
// work with producer
39+
40+
KafkaConsumer consumer = require(KafkaConsumer.class); <3>
41+
// work with consumer
42+
43+
...
44+
});
45+
}
46+
----
47+
48+
.Kotlin
49+
[source, kt, role="secondary"]
50+
----
51+
import io.jooby.kafka.KafkaModule
52+
53+
{
54+
install(KafkaModule()) <1>
55+
56+
get("/") {
57+
val producer = require(KafkaProducer::class); <2>
58+
// work with producer
59+
60+
val consumer = require(KafkaConsumer::class); <3>
61+
// work with consumer
62+
...
63+
}
64+
}
65+
----
66+
67+
<1> Install module
68+
<2> Use KafkaProducer
69+
<3> Use KafkaConsumer
70+
71+
Optionally, you can install just a consumer:
72+
73+
install(new KafkaConsumerModule());
74+
75+
Or producer:
76+
77+
install(new KafkaProducerModule());

docs/asciidoc/modules/modules.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Available modules are listed next.
2323
* link:/modules/hikari[HikariCP]: A high-performance JDBC connection pool.
2424
* link:/modules/hibernate[Hibernate]: Hibernate ORM module.
2525
* link:/modules/jdbi[Jdbi]: Jdbi module.
26+
* link:/modules/kafka[Kafka]: Kafka module.
2627
* link:/modules/redis[Redis]: Redis module.
2728

2829
=== Development Tools

modules/jooby-bom/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<jsonwebtoken.version>0.11.2</jsonwebtoken.version>
6464
<jsr305.version>3.0.2</jsr305.version>
6565
<junit.version>5.7.0</junit.version>
66+
<kafka.version>2.6.0</kafka.version>
6667
<kotlin.version>1.4.10</kotlin.version>
6768
<kotlinx-coroutines-core.version>1.4.1</kotlinx-coroutines-core.version>
6869
<lettuce.version>6.0.0.RELEASE</lettuce.version>
@@ -184,6 +185,12 @@
184185
<version>${jooby.version}</version>
185186
<type>jar</type>
186187
</dependency>
188+
<dependency>
189+
<groupId>io.jooby</groupId>
190+
<artifactId>jooby-kafka</artifactId>
191+
<version>${jooby.version}</version>
192+
<type>jar</type>
193+
</dependency>
187194
<dependency>
188195
<groupId>io.jooby</groupId>
189196
<artifactId>jooby-hikari</artifactId>
@@ -982,6 +989,12 @@
982989
<version>${lettuce.version}</version>
983990
<type>jar</type>
984991
</dependency>
992+
<dependency>
993+
<groupId>org.apache.kafka</groupId>
994+
<artifactId>kafka-clients</artifactId>
995+
<version>${kafka.version}</version>
996+
<type>jar</type>
997+
</dependency>
985998
<dependency>
986999
<groupId>org.codehaus.mojo</groupId>
9871000
<artifactId>exec-maven-plugin</artifactId>

modules/jooby-kafka/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<dependency>
3030
<groupId>org.apache.kafka</groupId>
3131
<artifactId>kafka-clients</artifactId>
32-
<version>2.6.0</version>
3332
</dependency>
3433

3534
<!-- Test dependencies -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 java.util.Properties;
9+
import java.util.function.Function;
10+
11+
import io.jooby.Environment;
12+
import io.jooby.Jooby;
13+
import io.jooby.ServiceKey;
14+
import io.jooby.ServiceRegistry;
15+
16+
class KafkaHelper {
17+
18+
public static void install(Jooby application, String key,
19+
Function<Properties, AutoCloseable> factory) {
20+
Environment environment = application.getEnvironment();
21+
22+
Properties properties = new Properties();
23+
properties.putAll(environment.getProperties(key, null));
24+
25+
AutoCloseable service = factory.apply(properties);
26+
ServiceRegistry registry = application.getServices();
27+
28+
application.onStop(service::close);
29+
Class serviceType = service.getClass();
30+
registry.putIfAbsent(serviceType, service);
31+
registry.put(ServiceKey.key(serviceType, key), service);
32+
}
33+
}

0 commit comments

Comments
 (0)