/** * Get or create a KafkaProducer instance which shares its stream with any other KafkaProducer created with the same <code>name</code> * @param vertx Vert.x instance to use * @param name the producer name to identify it * @param config Kafka producer configuration * @return an instance of the KafkaProducer */ public static <K, V> io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> createShared(io.vertx.rxjava.core.Vertx vertx, String name, Map<String, String> config) { io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> ret = io.vertx.rxjava.kafka.client.producer.KafkaProducer.newInstance(io.vertx.kafka.client.producer.KafkaProducer.createShared(vertx.getDelegate(), name, config), io.vertx.lang.rx.TypeArg.unknown(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * Get or create a KafkaProducer instance which shares its stream with any other KafkaProducer created with the same <code>name</code> * @param vertx Vert.x instance to use * @param name the producer name to identify it * @param config Kafka producer configuration * @return an instance of the KafkaProducer */ public static <K, V> io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> createShared(io.vertx.rxjava.core.Vertx vertx, String name, Map<String, String> config) { io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> ret = io.vertx.rxjava.kafka.client.producer.KafkaProducer.newInstance(io.vertx.kafka.client.producer.KafkaProducer.createShared(vertx.getDelegate(), name, config), io.vertx.lang.rx.TypeArg.unknown(), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * Get or create a KafkaProducer instance which shares its stream with any other KafkaProducer created with the same <code>name</code> * @param vertx Vert.x instance to use * @param name the producer name to identify it * @param config Kafka producer configuration * @param keyType class type for the key serialization * @param valueType class type for the value serialization * @return an instance of the KafkaProducer */ public static <K, V> io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> createShared(io.vertx.rxjava.core.Vertx vertx, String name, Map<String, String> config, Class<K> keyType, Class<V> valueType) { io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> ret = io.vertx.rxjava.kafka.client.producer.KafkaProducer.newInstance(io.vertx.kafka.client.producer.KafkaProducer.createShared(vertx.getDelegate(), name, config, io.vertx.lang.rxjava.Helper.unwrap(keyType), io.vertx.lang.rxjava.Helper.unwrap(valueType)), io.vertx.lang.rx.TypeArg.of(keyType), io.vertx.lang.rx.TypeArg.of(valueType)); return ret; }
/** * Get or create a KafkaProducer instance which shares its stream with any other KafkaProducer created with the same <code>name</code> * @param vertx Vert.x instance to use * @param name the producer name to identify it * @param config Kafka producer configuration * @param keyType class type for the key serialization * @param valueType class type for the value serialization * @return an instance of the KafkaProducer */ public static <K, V> io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> createShared(io.vertx.rxjava.core.Vertx vertx, String name, Map<String, String> config, Class<K> keyType, Class<V> valueType) { io.vertx.rxjava.kafka.client.producer.KafkaProducer<K, V> ret = io.vertx.rxjava.kafka.client.producer.KafkaProducer.newInstance(io.vertx.kafka.client.producer.KafkaProducer.createShared(vertx.getDelegate(), name, config, io.vertx.lang.rxjava.Helper.unwrap(keyType), io.vertx.lang.rxjava.Helper.unwrap(valueType)), io.vertx.lang.rx.TypeArg.of(keyType), io.vertx.lang.rx.TypeArg.of(valueType)); return ret; }
@Override public void start(Future<Void> startFuture) throws Exception { Properties config = new Properties(); config.putAll(context.config().getMap()); KafkaProducer<String, String> producer = KafkaProducer.createShared(vertx, "the-name", config); producer.write(KafkaProducerRecord.create("the_topic", "the_value"), ar -> startFuture.handle(ar.map((Void) null))); } }
@Override public void start(Future<Void> startFuture) throws Exception { Properties config = new Properties(); config.putAll(context.config().getMap()); KafkaProducer<String, String> producer = KafkaProducer.createShared(vertx, "the-name", config); producer.write(KafkaProducerRecord.create("the_topic", "the_value"), ar -> startFuture.handle(ar.map((Void) null))); } }
@Test public void testSharedProducer(TestContext ctx) { Properties config = kafkaCluster.useTo().getProducerProperties("the_producer"); config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); int num = 3; Async sentLatch = ctx.async(num); LinkedList<KafkaProducer<String, String>> producers = new LinkedList<>(); for (int i = 0;i < num;i++) { KafkaProducer<String, String> producer = KafkaProducer.createShared(vertx, "the-name", config); producer.write(KafkaProducerRecord.create("the_topic", "the_value"), ctx.asyncAssertSuccess(v -> { sentLatch.countDown(); })); producers.add(producer); } sentLatch.awaitSuccess(10000); Async async = ctx.async(); kafkaCluster.useTo().consumeStrings("the_topic", num, 10, TimeUnit.SECONDS, () -> { close(ctx, producers, async::complete); }); async.awaitSuccess(10000); waitUntil(() -> countThreads("kafka-producer-network-thread") == numKafkaProducerNetworkThread); }
@Test public void testSharedProducer(TestContext ctx) { Properties config = kafkaCluster.useTo().getProducerProperties("the_producer"); config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); int num = 3; Async sentLatch = ctx.async(num); LinkedList<KafkaProducer<String, String>> producers = new LinkedList<>(); for (int i = 0;i < num;i++) { KafkaProducer<String, String> producer = KafkaProducer.createShared(vertx, "the-name", config); producer.write(KafkaProducerRecord.create("the_topic", "the_value"), ctx.asyncAssertSuccess(v -> { sentLatch.countDown(); })); producers.add(producer); } sentLatch.awaitSuccess(10000); Async async = ctx.async(); kafkaCluster.useTo().consumeStrings("the_topic", num, 10, TimeUnit.SECONDS, () -> { close(ctx, producers, async::complete); }); async.awaitSuccess(10000); waitUntil(() -> countThreads("kafka-producer-network-thread") == numKafkaProducerNetworkThread); }