List of usage examples for org.springframework.kafka.test.utils KafkaTestUtils getPropertyValue
@SuppressWarnings("unchecked") public static <T> T getPropertyValue(Object root, String propertyPath, Class<T> type)
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 a 2 s .c om*/ }); 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 @SuppressWarnings("unchecked") public void testConcurrencyWithPartitions() { TopicPartitionInitialOffset[] topic1PartitionS = new TopicPartitionInitialOffset[] { new TopicPartitionInitialOffset(topic1, 0), new TopicPartitionInitialOffset(topic1, 1), new TopicPartitionInitialOffset(topic1, 2), new TopicPartitionInitialOffset(topic1, 3), new TopicPartitionInitialOffset(topic1, 4), new TopicPartitionInitialOffset(topic1, 5), new TopicPartitionInitialOffset(topic1, 6) }; ConsumerFactory<Integer, String> cf = mock(ConsumerFactory.class); Consumer<Integer, String> consumer = mock(Consumer.class); given(cf.createConsumer()).willReturn(consumer); given(consumer.poll(anyLong())).willAnswer(new Answer<ConsumerRecords<Integer, String>>() { @Override//from w w w . j av a 2 s . c o m public ConsumerRecords<Integer, String> answer(InvocationOnMock invocation) throws Throwable { Thread.sleep(100); return null; } }); ContainerProperties containerProps = new ContainerProperties(topic1PartitionS); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { }); ConcurrentMessageListenerContainer<Integer, String> container = new ConcurrentMessageListenerContainer<>(cf, containerProps); container.setConcurrency(3); container.start(); List<KafkaMessageListenerContainer<Integer, String>> containers = KafkaTestUtils.getPropertyValue(container, "containers", List.class); assertThat(containers.size()).isEqualTo(3); for (int i = 0; i < 3; i++) { assertThat(KafkaTestUtils.getPropertyValue(containers.get(i), "topicPartitions", TopicPartitionInitialOffset[].class).length).isEqualTo(i < 2 ? 2 : 3); } container.stop(); }
From source file:org.springframework.kafka.listener.KafkaMessageListenerContainerTests.java
private Consumer<?, ?> spyOnConsumer(KafkaMessageListenerContainer<Integer, String> container) { Consumer<?, ?> consumer = spy( KafkaTestUtils.getPropertyValue(container, "listenerConsumer.consumer", Consumer.class)); new DirectFieldAccessor(KafkaTestUtils.getPropertyValue(container, "listenerConsumer")) .setPropertyValue("consumer", consumer); return consumer; }
From source file:org.springframework.kafka.listener.TransactionalContainerTests.java
@SuppressWarnings("unchecked") @Test/*from w w w .j a v a2 s . c o m*/ public void testRollbackRecord() throws Exception { logger.info("Start testRollbackRecord"); Map<String, Object> props = KafkaTestUtils.consumerProps("txTest1", "false", embeddedKafka); props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); props.put(ConsumerConfig.GROUP_ID_CONFIG, "group"); props.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed"); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); ContainerProperties containerProps = new ContainerProperties(topic1, topic2); containerProps.setGroupId("group"); containerProps.setPollTimeout(10_000); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); senderProps.put(ProducerConfig.RETRIES_CONFIG, 1); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); pf.setTransactionIdPrefix("rr."); final KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf); final AtomicBoolean failed = new AtomicBoolean(); final CountDownLatch latch = new CountDownLatch(3); final AtomicReference<String> transactionalId = new AtomicReference<>(); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { latch.countDown(); if (failed.compareAndSet(false, true)) { throw new RuntimeException("fail"); } /* * Send a message to topic2 and wait for it so we don't stop the container too soon. */ if (message.topic().equals(topic1)) { template.send(topic2, "bar"); template.flush(); transactionalId.set(KafkaTestUtils.getPropertyValue( ProducerFactoryUtils.getTransactionalResourceHolder(pf).getProducer(), "delegate.transactionManager.transactionalId", String.class)); } }); @SuppressWarnings({ "rawtypes" }) KafkaTransactionManager tm = new KafkaTransactionManager(pf); containerProps.setTransactionManager(tm); KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, containerProps); container.setBeanName("testRollbackRecord"); container.start(); template.setDefaultTopic(topic1); template.executeInTransaction(t -> { template.sendDefault(0, 0, "foo"); return null; }); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); container.stop(); Consumer<Integer, String> consumer = cf.createConsumer(); final CountDownLatch subsLatch = new CountDownLatch(1); consumer.subscribe(Arrays.asList(topic1), new ConsumerRebalanceListener() { @Override public void onPartitionsRevoked(Collection<TopicPartition> partitions) { // empty } @Override public void onPartitionsAssigned(Collection<TopicPartition> partitions) { subsLatch.countDown(); } }); ConsumerRecords<Integer, String> records = null; int n = 0; while (subsLatch.getCount() > 0 && n++ < 600) { records = consumer.poll(Duration.ofMillis(100)); } assertThat(subsLatch.await(1, TimeUnit.MILLISECONDS)).isTrue(); assertThat(records.count()).isEqualTo(0); // depending on timing, the position might include the offset representing the commit in the log assertThat(consumer.position(new TopicPartition(topic1, 0))).isGreaterThanOrEqualTo(1L); assertThat(transactionalId.get()).startsWith("rr.group.txTopic"); assertThat(KafkaTestUtils.getPropertyValue(pf, "consumerProducers", Map.class)).isEmpty(); logger.info("Stop testRollbackRecord"); pf.destroy(); consumer.close(); }