org.springframework.kafka.listener.KafkaMessageListenerContainer Java Examples
The following examples show how to use
org.springframework.kafka.listener.KafkaMessageListenerContainer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringKafkaSenderTest.java From spring-kafka with MIT License | 6 votes |
@Before public void setUp() throws Exception { // set up the Kafka consumer properties Map<String, Object> consumerProperties = KafkaTestUtils.consumerProps("sender_group", "false", AllSpringKafkaTests.embeddedKafka); // create a Kafka consumer factory DefaultKafkaConsumerFactory<String, String> consumerFactory = new DefaultKafkaConsumerFactory<String, String>(consumerProperties); // set the topic that needs to be consumed ContainerProperties containerProperties = new ContainerProperties(AllSpringKafkaTests.SENDER_TOPIC); // create a Kafka MessageListenerContainer container = new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); // create a thread safe queue to store the received message records = new LinkedBlockingQueue<>(); // setup a Kafka message listener container.setupMessageListener(new MessageListener<String, String>() { @Override public void onMessage(ConsumerRecord<String, String> record) { LOGGER.debug("test-listener received message='{}'", record.toString()); records.add(record); } }); // start the container and underlying message listener container.start(); // wait until the container has the required number of assigned partitions ContainerTestUtils.waitForAssignment(container, AllSpringKafkaTests.embeddedKafka.getPartitionsPerTopic()); }
Example #2
Source File: StubRunnerKafkaConfiguration.java From spring-cloud-contract with Apache License 2.0 | 6 votes |
private void registerContainers(ConfigurableListableBeanFactory beanFactory, List<Contract> matchingContracts, String flowName, StubRunnerKafkaRouter listener) { // listener's container ConsumerFactory consumerFactory = beanFactory.getBean(ConsumerFactory.class); for (Contract matchingContract : matchingContracts) { if (matchingContract.getInput() == null) { continue; } String destination = MapConverter.getStubSideValuesForNonBody( matchingContract.getInput().getMessageFrom()).toString(); ContainerProperties containerProperties = new ContainerProperties( destination); KafkaMessageListenerContainer container = listenerContainer(consumerFactory, containerProperties, listener); String containerName = flowName + ".container"; Object initializedContainer = beanFactory.initializeBean(container, containerName); beanFactory.registerSingleton(containerName, initializedContainer); if (log.isDebugEnabled()) { log.debug( "Initialized kafka message container with name [" + containerName + "] listening to destination [" + destination + "]"); } } }
Example #3
Source File: SpringKafkaTest.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
@Test public void test() throws InterruptedException { log.info("Start auto"); ContainerProperties containerProps = new ContainerProperties("topic1", "topic2"); final CountDownLatch latch = new CountDownLatch(4); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { log.info("received: " + message); latch.countDown(); }); KafkaMessageListenerContainer<Integer, String> container = createContainer(containerProps); container.setBeanName("testAuto"); container.start(); Thread.sleep(1000); // wait a bit for the container to start KafkaTemplate<Integer, String> template = createTemplate(); template.setDefaultTopic("zptest"); 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(); log.info("Stop auto"); }
Example #4
Source File: KafkaConfigurer.java From bird-java with MIT License | 6 votes |
@Bean @ConditionalOnProperty(value = EventbusConstant.Kafka.LISTENER_PACKAGES) public KafkaMessageListenerContainer kafkaListenerContainer(EventDispatcher eventDispatcher) { KafkaEventArgListener listener = new KafkaEventArgListener(eventDispatcher); ContainerProperties containerProperties = new ContainerProperties(eventDispatcher.getAllTopics()); containerProperties.setMessageListener(listener); containerProperties.setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL_IMMEDIATE); HashMap<String,Object> properties = new HashMap<>(8); properties.put("bootstrap.servers", kafkaProperties.getHost()); KafkaListenerProperties listenerProperties = kafkaProperties.getListener(); properties.put("group.id", listenerProperties.getGroupId()); properties.put("auto.offset.reset", "earliest"); properties.put("enable.auto.commit", false); properties.put("auto.commit.interval.ms", 1000); properties.put("session.timeout.ms", 15000); properties.put("key.deserializer", StringDeserializer.class); properties.put("value.deserializer", EventArgDeserializer.class); DefaultKafkaConsumerFactory<String,EventArg> consumerFactory = new DefaultKafkaConsumerFactory<>(properties); return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); }
Example #5
Source File: EnodeTestKafkaConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> domainEventListenerContainer(KafkaDomainEventListener domainEventListener, ConsumerFactory<String, String> consumerFactory) { ContainerProperties properties = new ContainerProperties(eventTopic); properties.setGroupId(Constants.DEFAULT_PRODUCER_GROUP); properties.setMessageListener(domainEventListener); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory, properties); }
Example #6
Source File: SpringKafkaMainTest.java From spring-kafka with MIT License | 5 votes |
@Test public void testReceive() throws Exception { String bootstrapServers = embeddedKafka.getBrokersAsString(); LOGGER.info("bootstrapServers='{}'", bootstrapServers); ContainerProperties containerProperties = new ContainerProperties(JAVA_TOPIC); Receiver receiver = new Receiver(); KafkaMessageListenerContainer<String, String> messageListenerContainer = ReceiverConfig.createMessageListenerContainer( containerProperties, bootstrapServers); messageListenerContainer.setupMessageListener(receiver); messageListenerContainer.start(); // wait a bit for the container to start Thread.sleep(1000); KafkaTemplate<String, String> kafkaTemplate = SenderConfig.createKafkaTemplate(bootstrapServers); kafkaTemplate.send(JAVA_TOPIC, "Hello Java!"); receiver.getLatch().await(10000, TimeUnit.MILLISECONDS); assertThat(receiver.getLatch().getCount()).isEqualTo(0); messageListenerContainer.stop(); }
Example #7
Source File: StubRunnerKafkaConfiguration.java From spring-cloud-contract with Apache License 2.0 | 5 votes |
private KafkaMessageListenerContainer listenerContainer( ConsumerFactory consumerFactory, ContainerProperties containerProperties, GenericMessageListener listener) { KafkaMessageListenerContainer container = new KafkaMessageListenerContainer( consumerFactory, containerProperties); container.setupMessageListener(listener); return container; }
Example #8
Source File: S1pKafkaApplication.java From grussell-spring-kafka with Apache License 2.0 | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> container( ConsumerFactory<String, String> consumerFactory, ConfigProperties config) { ContainerProperties containerProperties = new ContainerProperties(config.getTopic()); containerProperties.setMessageListener(listener()); return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); }
Example #9
Source File: S1pKafkaApplication.java From grussell-spring-kafka with Apache License 2.0 | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> container( ConsumerFactory<String, String> consumerFactory, ConfigProperties config) { ContainerProperties containerProperties = new ContainerProperties(config.getTopic()); containerProperties.setMessageListener(listener()); containerProperties.setAckMode(AckMode.MANUAL_IMMEDIATE); return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); }
Example #10
Source File: KafkaRepository.java From kafka-service-broker with Apache License 2.0 | 5 votes |
private KafkaMessageListenerContainer<Integer, String> createContainer( ContainerProperties containerProps) { Map<String, Object> props = consumerProps(); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<Integer, String>(props); KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, containerProps); return container; }
Example #11
Source File: SpringBootAsyncProducerApplication.java From Spring-Boot-2.0-Projects with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> replyContainer( ConsumerFactory<String, String> cf) { ContainerProperties containerProperties = new ContainerProperties("asyncReplies"); containerProperties.setGroupId("async"); return new KafkaMessageListenerContainer<>(cf, containerProperties); }
Example #12
Source File: SpringKafkaTest.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
private KafkaMessageListenerContainer<Integer, String> createContainer( ContainerProperties containerProps) { Map<String, Object> props = consumerProps(); DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(props); KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, containerProps); return container; }
Example #13
Source File: KafkaEventConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer publishableExceptionListenerContainer(KafkaPublishableExceptionListener publishableExceptionListener, RetryTemplate retryTemplate) { ContainerProperties properties = new ContainerProperties(QueueProperties.EXCEPTION_TOPIC); properties.setGroupId(QueueProperties.DEFAULT_PRODUCER_GROUP); RetryingMessageListenerAdapter listenerAdapter = new RetryingMessageListenerAdapter(publishableExceptionListener, retryTemplate); properties.setMessageListener(listenerAdapter); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory(), properties); }
Example #14
Source File: KafkaEventConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer applicationMessageListenerContainer(KafkaApplicationMessageListener applicationMessageListener, RetryTemplate retryTemplate) { ContainerProperties properties = new ContainerProperties(QueueProperties.APPLICATION_TOPIC); properties.setGroupId(QueueProperties.DEFAULT_PRODUCER_GROUP); RetryingMessageListenerAdapter listenerAdapter = new RetryingMessageListenerAdapter(applicationMessageListener, retryTemplate); properties.setMessageListener(listenerAdapter); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory(), properties); }
Example #15
Source File: KafkaEventConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer domainEventListenerContainer(KafkaDomainEventListener domainEventListener, RetryTemplate retryTemplate) { ContainerProperties properties = new ContainerProperties(QueueProperties.EVENT_TOPIC); properties.setGroupId(QueueProperties.DEFAULT_PRODUCER_GROUP); RetryingMessageListenerAdapter listenerAdapter = new RetryingMessageListenerAdapter(domainEventListener, retryTemplate); properties.setMessageListener(listenerAdapter); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory(), properties); }
Example #16
Source File: KafkaCommandConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer kafkaMessageListenerContainer(KafkaCommandListener commandListener, RetryTemplate retryTemplate) { ContainerProperties properties = new ContainerProperties(COMMAND_TOPIC); properties.setGroupId(DEFAULT_CONSUMER_GROUP); RetryingMessageListenerAdapter listenerAdapter = new RetryingMessageListenerAdapter(commandListener, retryTemplate); properties.setMessageListener(listenerAdapter); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory(), properties); }
Example #17
Source File: EnodeTestKafkaConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> publishableExceptionListenerContainer(KafkaPublishableExceptionListener publishableExceptionListener, ConsumerFactory<String, String> consumerFactory) { ContainerProperties properties = new ContainerProperties(exceptionTopic); properties.setGroupId(Constants.DEFAULT_PRODUCER_GROUP); properties.setMessageListener(publishableExceptionListener); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory, properties); }
Example #18
Source File: EnodeTestKafkaConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> applicationMessageListenerContainer(KafkaApplicationMessageListener applicationMessageListener, ConsumerFactory<String, String> consumerFactory) { ContainerProperties properties = new ContainerProperties(applicationTopic); properties.setGroupId(Constants.DEFAULT_PRODUCER_GROUP); properties.setMessageListener(applicationMessageListener); properties.setMissingTopicsFatal(false); properties.setAckMode(ContainerProperties.AckMode.MANUAL); return new KafkaMessageListenerContainer<>(consumerFactory, properties); }
Example #19
Source File: EnodeTestKafkaConfig.java From enode with MIT License | 5 votes |
@Bean public KafkaMessageListenerContainer<String, String> commandListenerContainer(KafkaCommandListener commandListener, ConsumerFactory<String, String> consumerFactory) { ContainerProperties properties = new ContainerProperties(commandTopic); properties.setGroupId(Constants.DEFAULT_CONSUMER_GROUP); properties.setMessageListener(commandListener); properties.setMissingTopicsFatal(false); return new KafkaMessageListenerContainer<>(consumerFactory, properties); }
Example #20
Source File: SpringBootAsyncProducerApplication.java From Spring-Boot-2.0-Projects with MIT License | 4 votes |
@Bean public ReplyingKafkaTemplate<String, String, String> kafkaTemplate( ProducerFactory<String, String> pf, KafkaMessageListenerContainer<String, String> replyContainer) { return new ReplyingKafkaTemplate<>(pf, replyContainer); }
Example #21
Source File: ReplyConfiguration.java From faster-framework-project with Apache License 2.0 | 4 votes |
/** * @return replyContainer */ public KafkaMessageListenerContainer<Object, Object> replyContainer() { ContainerProperties containerProperties = new ContainerProperties(properties.getProducer().getReplyTopic()); return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); }
Example #22
Source File: KafkaRepository.java From kafka-service-broker with Apache License 2.0 | 4 votes |
public KafkaMessageListenerContainer<Integer, String> getConsumer(KafkaDataListener listener) throws ExecutionException, InterruptedException { ContainerProperties containerProps = new ContainerProperties(info.getTopicName()); containerProps.setMessageListener(listener); return createContainer(containerProps); }
Example #23
Source File: KafkaSampleConsumer.java From kafka-service-broker with Apache License 2.0 | 4 votes |
private void consumeMessages() throws ExecutionException, InterruptedException { KafkaMessageListenerContainer<Integer, String> container = repo.getConsumer(new ConsumerListener()); container.start(); }
Example #24
Source File: ReceiverConfig.java From spring-kafka with MIT License | 4 votes |
public static KafkaMessageListenerContainer<String, String> createMessageListenerContainer( ContainerProperties containerProperties, String bootstrapServers) { return new KafkaMessageListenerContainer<>( createConsumerFactory(bootstrapServers), containerProperties); }
Example #25
Source File: SpringKafkaSenderTest.java From spring-kafka with MIT License | 4 votes |
@Before public void setUp() throws Exception { // set up the Kafka consumer properties Map<String, Object> consumerProperties = KafkaTestUtils.consumerProps("sender", "false", embeddedKafka.getEmbeddedKafka()); // create a Kafka consumer factory DefaultKafkaConsumerFactory<String, String> consumerFactory = new DefaultKafkaConsumerFactory<String, String>( consumerProperties); // set the topic that needs to be consumed ContainerProperties containerProperties = new ContainerProperties(SENDER_TOPIC); // create a Kafka MessageListenerContainer container = new KafkaMessageListenerContainer<>(consumerFactory, containerProperties); // create a thread safe queue to store the received message records = new LinkedBlockingQueue<>(); // setup a Kafka message listener container .setupMessageListener(new MessageListener<String, String>() { @Override public void onMessage( ConsumerRecord<String, String> record) { LOGGER.debug("test-listener received message='{}'", record.toString()); records.add(record); } }); // start the container and underlying message listener container.start(); // wait until the container has the required number of assigned partitions ContainerTestUtils.waitForAssignment(container, embeddedKafka.getEmbeddedKafka().getPartitionsPerTopic()); }