List of usage examples for org.springframework.kafka.core KafkaTemplate KafkaTemplate
public KafkaTemplate(ProducerFactory<K, V> producerFactory)
From source file:org.s1p.JsonConfiguration.java
@Bean public KafkaTemplate<String, Foo> kafkaTemplate() { return new KafkaTemplate<>(producerFactory()); }
From source file:org.s1p.app6.S1pKafkaApplication.java
@Bean public KafkaTemplate<String, String> kafkaTemplate(ProducerFactory<String, String> producerFactory, ConfigProperties config) {/* w ww .ja va 2 s. c om*/ KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<>(producerFactory); kafkaTemplate.setMessageConverter(new StringJsonMessageConverter()); kafkaTemplate.setDefaultTopic(config.getFooTopic()); return kafkaTemplate; }
From source file:io.pivotal.cf.service.connector.KafkaRepository.java
private KafkaTemplate<Integer, String> getTemplate() { return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(senderProperties())); }
From source file:org.s1p.CommonConfiguration.java
@Bean public KafkaTemplate<String, String> kafkaTemplate() { return new KafkaTemplate<>(producerFactory()); }
From source file:org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsDlqDispatch.java
KafkaStreamsDlqDispatch(String dlqName, KafkaBinderConfigurationProperties kafkaBinderConfigurationProperties, KafkaConsumerProperties kafkaConsumerProperties) { ProducerFactory<byte[], byte[]> producerFactory = getProducerFactory( new ExtendedProducerProperties<>(kafkaConsumerProperties.getDlqProducerProperties()), kafkaBinderConfigurationProperties); this.kafkaTemplate = new KafkaTemplate<>(producerFactory); this.dlqName = dlqName; }
From source file:org.springframework.kafka.listener.ConcurrentMessageListenerContainerTests.java
@Test public void testAutoCommit() throws Exception { this.logger.info("Start auto"); Map<String, Object> props = KafkaTestUtils.consumerProps("test1", "true", embeddedKafka); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); ContainerProperties containerProps = new ContainerProperties(topic1); final CountDownLatch latch = new CountDownLatch(4); final Set<String> listenerThreadNames = new ConcurrentSkipListSet<>(); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { ConcurrentMessageListenerContainerTests.this.logger.info("auto: " + message); listenerThreadNames.add(Thread.currentThread().getName()); latch.countDown();//from w w w . j a v a2 s . c o m }); ConcurrentMessageListenerContainer<Integer, String> container = new ConcurrentMessageListenerContainer<>(cf, containerProps); container.setConcurrency(2); container.setBeanName("testAuto"); container.start(); ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic()); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf); template.setDefaultTopic(topic1); template.sendDefault(0, "foo"); template.sendDefault(2, "bar"); template.sendDefault(0, "baz"); template.sendDefault(2, "qux"); template.flush(); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(listenerThreadNames).allMatch(threadName -> threadName.contains("-consumer-")); @SuppressWarnings("unchecked") List<KafkaMessageListenerContainer<Integer, String>> containers = KafkaTestUtils.getPropertyValue(container, "containers", List.class); assertThat(containers.size()).isEqualTo(2); for (int i = 0; i < 2; i++) { assertThat(KafkaTestUtils.getPropertyValue(containers.get(i), "listenerConsumer.acks", Collection.class) .size()).isEqualTo(0); } container.stop(); this.logger.info("Stop auto"); }
From source file:org.springframework.kafka.listener.ConcurrentMessageListenerContainerTests.java
@Test public void testAutoCommitWithRebalanceListener() throws Exception { this.logger.info("Start auto"); Map<String, Object> props = KafkaTestUtils.consumerProps("test10", "true", embeddedKafka); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); ContainerProperties containerProps = new ContainerProperties(topic1); final CountDownLatch latch = new CountDownLatch(4); final Set<String> listenerThreadNames = new ConcurrentSkipListSet<>(); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { ConcurrentMessageListenerContainerTests.this.logger.info("auto: " + message); listenerThreadNames.add(Thread.currentThread().getName()); latch.countDown();//from w ww . j a v a2 s .c om }); final CountDownLatch rebalancePartitionsAssignedLatch = new CountDownLatch(2); final CountDownLatch rebalancePartitionsRevokedLatch = new CountDownLatch(2); containerProps.setConsumerRebalanceListener(new ConsumerRebalanceListener() { @Override public void onPartitionsRevoked(Collection<TopicPartition> partitions) { ConcurrentMessageListenerContainerTests.this.logger .info("In test, partitions revoked:" + partitions); rebalancePartitionsRevokedLatch.countDown(); } @Override public void onPartitionsAssigned(Collection<TopicPartition> partitions) { ConcurrentMessageListenerContainerTests.this.logger .info("In test, partitions assigned:" + partitions); rebalancePartitionsAssignedLatch.countDown(); } }); ConcurrentMessageListenerContainer<Integer, String> container = new ConcurrentMessageListenerContainer<>(cf, containerProps); container.setConcurrency(2); container.setBeanName("testAuto"); container.start(); ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic()); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf); template.setDefaultTopic(topic1); template.sendDefault(0, "foo"); template.sendDefault(2, "bar"); template.sendDefault(0, "baz"); template.sendDefault(2, "qux"); template.flush(); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(rebalancePartitionsAssignedLatch.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(rebalancePartitionsRevokedLatch.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(listenerThreadNames).allMatch(threadName -> threadName.contains("-consumer-")); container.stop(); this.logger.info("Stop auto"); }
From source file:org.springframework.kafka.listener.ConcurrentMessageListenerContainerTests.java
@Test public void testAfterListenCommit() throws Exception { this.logger.info("Start manual"); Map<String, Object> props = KafkaTestUtils.consumerProps("test2", "false", embeddedKafka); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); ContainerProperties containerProps = new ContainerProperties(topic2); final CountDownLatch latch = new CountDownLatch(4); final Set<String> listenerThreadNames = new ConcurrentSkipListSet<>(); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { ConcurrentMessageListenerContainerTests.this.logger.info("manual: " + message); listenerThreadNames.add(Thread.currentThread().getName()); latch.countDown();//from w w w .jav a2s .c om }); ConcurrentMessageListenerContainer<Integer, String> container = new ConcurrentMessageListenerContainer<>(cf, containerProps); container.setConcurrency(2); container.setBeanName("testBatch"); container.start(); ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic()); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf); template.setDefaultTopic(topic2); template.sendDefault(0, "foo"); template.sendDefault(2, "bar"); template.sendDefault(0, "baz"); template.sendDefault(2, "qux"); template.flush(); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(listenerThreadNames).allMatch(threadName -> threadName.contains("-listener-")); container.stop(); this.logger.info("Stop manual"); }
From source file:org.springframework.kafka.listener.ConcurrentMessageListenerContainerTests.java
private void testManualCommitGuts(AckMode ackMode, String topic) throws Exception { this.logger.info("Start " + ackMode); Map<String, Object> props = KafkaTestUtils.consumerProps("test" + ackMode, "false", embeddedKafka); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); ContainerProperties containerProps = new ContainerProperties(topic); final CountDownLatch latch = new CountDownLatch(4); containerProps.setMessageListener((AcknowledgingMessageListener<Integer, String>) (message, ack) -> { ConcurrentMessageListenerContainerTests.this.logger.info("manual: " + message); ack.acknowledge();//from w w w. ja va 2 s.c o m latch.countDown(); }); containerProps.setAckMode(ackMode); ConcurrentMessageListenerContainer<Integer, String> container = new ConcurrentMessageListenerContainer<>(cf, containerProps); container.setConcurrency(2); container.setBeanName("test" + ackMode); container.start(); ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic()); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf); template.setDefaultTopic(topic); template.sendDefault(0, "foo"); template.sendDefault(2, "bar"); template.sendDefault(0, "baz"); template.sendDefault(2, "qux"); template.flush(); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); container.stop(); this.logger.info("Stop " + ackMode); }
From source file:org.springframework.kafka.listener.ConcurrentMessageListenerContainerTests.java
@Test @Ignore // TODO https://github.com/spring-projects/spring-kafka/issues/62 using SYNC for avoidance public void testManualCommitExisting() throws Exception { this.logger.info("Start MANUAL_IMMEDIATE with Existing"); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf); template.setDefaultTopic(topic7);/*from w w w.j ava 2s .co m*/ template.sendDefault(0, "foo"); template.sendDefault(2, "bar"); template.sendDefault(0, "baz"); template.sendDefault(2, "qux"); template.flush(); Map<String, Object> props = KafkaTestUtils.consumerProps("testManualExisting", "false", embeddedKafka); props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); ContainerProperties containerProps = new ContainerProperties(topic7); final CountDownLatch latch = new CountDownLatch(8); containerProps.setMessageListener((AcknowledgingMessageListener<Integer, String>) (message, ack) -> { ConcurrentMessageListenerContainerTests.this.logger.info("manualExisting: " + message); ack.acknowledge(); latch.countDown(); }); containerProps.setAckMode(AckMode.MANUAL_IMMEDIATE); final CountDownLatch commits = new CountDownLatch(8); final AtomicReference<Exception> exceptionRef = new AtomicReference<>(); containerProps.setCommitCallback((offsets, exception) -> { commits.countDown(); if (exception != null) { exceptionRef.compareAndSet(null, exception); } }); ConcurrentMessageListenerContainer<Integer, String> container = new ConcurrentMessageListenerContainer<>(cf, containerProps); container.setConcurrency(1); container.setBeanName("testManualExisting"); container.start(); ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic()); template.sendDefault(0, "fooo"); template.sendDefault(2, "barr"); template.sendDefault(0, "bazz"); template.sendDefault(2, "quxx"); template.flush(); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(commits.await(60, TimeUnit.SECONDS)).isTrue(); assertThat(exceptionRef.get()).isNull(); container.stop(); this.logger.info("Stop MANUAL_IMMEDIATE with Existing"); }