|
| 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 com.typesafe.config.Config; |
| 9 | +import io.jooby.Extension; |
| 10 | +import io.jooby.Jooby; |
| 11 | +import io.jooby.ServiceRegistry; |
| 12 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 13 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 14 | + |
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +/** |
| 19 | + * Redis module: https://jooby.io/modules/redis. |
| 20 | + * <p> |
| 21 | + * Usage: |
| 22 | + * |
| 23 | + * <pre>{@code |
| 24 | + * { |
| 25 | + * install(new RedisModule()); |
| 26 | + * |
| 27 | + * get("/", ctx -> { |
| 28 | + * StatefulRedisConnection redis = require(StatefulRedisConnection.class); |
| 29 | + * // work with redis |
| 30 | + * }); |
| 31 | + * } |
| 32 | + * }</pre> |
| 33 | + * <p> |
| 34 | + * application.conf: |
| 35 | + * |
| 36 | + * <pre> |
| 37 | + * redis = "redis://localhost:6379" |
| 38 | + * </pre> |
| 39 | + * <p> |
| 40 | + * Module is built on top of <a href="https://lettuce.io/">lettuce</a>. Once installed you are able |
| 41 | + * to work with: |
| 42 | + * |
| 43 | + * <ul> |
| 44 | + * <li>io.lettuce.core.RedisClient</li> |
| 45 | + * <li>io.lettuce.core.api.StatefulRedisConnection</li> |
| 46 | + * <li>io.lettuce.core.pubsub.StatefulRedisPubSubConnection</li> |
| 47 | + * </ul> |
| 48 | + * <p> |
| 49 | + * Alternative you can pass a redis URI: |
| 50 | + * <pre> |
| 51 | + * install(new RedisModule("redis://localhost:6379")); |
| 52 | + * </pre> |
| 53 | + * |
| 54 | + * @author edgar |
| 55 | + * @since 2.8.5 |
| 56 | + */ |
| 57 | +public class KafkaModule implements Extension { |
| 58 | + Properties producerProps; |
| 59 | + Properties consumerProps; |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a new kafka module. Value must be: |
| 63 | + * - Valid redis URI; or |
| 64 | + * - Property name |
| 65 | + * |
| 66 | + * @param producerProps kafka producer properties. |
| 67 | + * @param consumerProps kafka consumer properties. |
| 68 | + */ |
| 69 | + public KafkaModule(@Nonnull Properties producerProps, @Nonnull Properties consumerProps) { |
| 70 | + this.producerProps = producerProps; |
| 71 | + this.consumerProps = consumerProps; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Create a new redis module. The application configuration file must have a redis property, like: |
| 76 | + * <pre> |
| 77 | + * redis = "redis://localhost:6379" |
| 78 | + * </pre> |
| 79 | + */ |
| 80 | + public KafkaModule() { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void install(@Nonnull Jooby application) { |
| 85 | + Config config = application.getConfig(); |
| 86 | + |
| 87 | + if (this.producerProps == null) { |
| 88 | + this.producerProps = properties(config, "kafka.producer"); |
| 89 | + } |
| 90 | + |
| 91 | + if (this.consumerProps == null) { |
| 92 | + this.consumerProps = properties(config, "kafka.consumer"); |
| 93 | + } |
| 94 | + |
| 95 | + ServiceRegistry registry = application.getServices(); |
| 96 | + |
| 97 | + if (this.producerProps != null) { |
| 98 | + KafkaProducer producer = new KafkaProducer(producerProps); |
| 99 | + application.onStop(producer::close); |
| 100 | + registry.putIfAbsent(KafkaProducer.class, producer); |
| 101 | + } |
| 102 | + |
| 103 | + if (this.consumerProps != null) { |
| 104 | + KafkaConsumer consumer = new KafkaConsumer(consumerProps); |
| 105 | + application.onStop(consumer::close); |
| 106 | + registry.putIfAbsent(KafkaConsumer.class, consumer); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static Properties properties(final Config config, final String key) { |
| 111 | + Properties props = new Properties(); |
| 112 | + if (config.hasPath(key)) { |
| 113 | + // dump |
| 114 | + config.getConfig(key).entrySet().forEach( |
| 115 | + e -> props.setProperty(e.getKey(), e.getValue().unwrapped().toString())); |
| 116 | + } |
| 117 | + return props; |
| 118 | + } |
| 119 | +} |
0 commit comments