Java Code Examples for org.springframework.kafka.listener.KafkaMessageListenerContainer

The following examples show how to use org.springframework.kafka.listener.KafkaMessageListenerContainer. These examples are extracted from open source projects. 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
@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 2
Source Project: bird-java   Source File: KafkaConfigurer.java    License: MIT License 6 votes vote down vote up
@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 3
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 4
Source Project: spring-kafka   Source File: SpringKafkaSenderTest.java    License: MIT License 6 votes vote down vote up
@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 5
Source Project: enode   Source File: EnodeTestKafkaConfig.java    License: MIT License 5 votes vote down vote up
@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 6
Source Project: enode   Source File: EnodeTestKafkaConfig.java    License: MIT License 5 votes vote down vote up
@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 7
Source Project: enode   Source File: EnodeTestKafkaConfig.java    License: MIT License 5 votes vote down vote up
@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 8
Source Project: enode   Source File: EnodeTestKafkaConfig.java    License: MIT License 5 votes vote down vote up
@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 9
Source Project: enode   Source File: KafkaCommandConfig.java    License: MIT License 5 votes vote down vote up
@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 10
Source Project: enode   Source File: KafkaEventConfig.java    License: MIT License 5 votes vote down vote up
@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 11
Source Project: enode   Source File: KafkaEventConfig.java    License: MIT License 5 votes vote down vote up
@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 12
Source Project: enode   Source File: KafkaEventConfig.java    License: MIT License 5 votes vote down vote up
@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 13
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 14
@Bean
public KafkaMessageListenerContainer<String, String> replyContainer(
        ConsumerFactory<String, String> cf) {
    ContainerProperties containerProperties = new ContainerProperties("asyncReplies");
    containerProperties.setGroupId("async");
    return new KafkaMessageListenerContainer<>(cf, containerProperties);
}
 
Example 15
Source Project: kafka-service-broker   Source File: KafkaRepository.java    License: Apache License 2.0 5 votes vote down vote up
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 16
Source Project: grussell-spring-kafka   Source File: S1pKafkaApplication.java    License: Apache License 2.0 5 votes vote down vote up
@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 17
Source Project: grussell-spring-kafka   Source File: S1pKafkaApplication.java    License: Apache License 2.0 5 votes vote down vote up
@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 18
private KafkaMessageListenerContainer listenerContainer(
		ConsumerFactory consumerFactory, ContainerProperties containerProperties,
		GenericMessageListener listener) {
	KafkaMessageListenerContainer container = new KafkaMessageListenerContainer(
			consumerFactory, containerProperties);
	container.setupMessageListener(listener);
	return container;
}
 
Example 19
Source Project: spring-kafka   Source File: SpringKafkaMainTest.java    License: MIT License 5 votes vote down vote up
@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 20
/**
 * @return replyContainer
 */
public KafkaMessageListenerContainer<Object, Object> replyContainer() {
    ContainerProperties containerProperties = new ContainerProperties(properties.getProducer().getReplyTopic());
    return new KafkaMessageListenerContainer<>(consumerFactory, containerProperties);
}
 
Example 21
@Bean
public ReplyingKafkaTemplate<String, String, String> kafkaTemplate(
        ProducerFactory<String, String> pf,
        KafkaMessageListenerContainer<String, String> replyContainer) {
    return new ReplyingKafkaTemplate<>(pf, replyContainer);
}
 
Example 22
Source Project: kafka-service-broker   Source File: KafkaRepository.java    License: Apache License 2.0 4 votes vote down vote up
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 Project: kafka-service-broker   Source File: KafkaSampleConsumer.java    License: Apache License 2.0 4 votes vote down vote up
private void consumeMessages() throws ExecutionException, InterruptedException {
    KafkaMessageListenerContainer<Integer, String> container = repo.getConsumer(new ConsumerListener());
    container.start();
}
 
Example 24
Source Project: spring-kafka   Source File: ReceiverConfig.java    License: MIT License 4 votes vote down vote up
public static KafkaMessageListenerContainer<String, String> createMessageListenerContainer(
    ContainerProperties containerProperties,
    String bootstrapServers) {
  return new KafkaMessageListenerContainer<>(
      createConsumerFactory(bootstrapServers), containerProperties);
}
 
Example 25
Source Project: spring-kafka   Source File: SpringKafkaSenderTest.java    License: MIT License 4 votes vote down vote up
@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());
}