org.springframework.kafka.listener.config.ContainerProperties Java Examples

The following examples show how to use org.springframework.kafka.listener.config.ContainerProperties. 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: UmcReceiveAutoConfiguration.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Bean(BEAN_KAFKA_BATCH_FACTORY)
@EnableKafkaCollectReceiver
@SuppressWarnings({ "unchecked", "rawtypes" })
public KafkaListenerContainerFactory<?> batchFactory(ReceiverProperties conf) {
	// Create consumer factory.
	Properties properties = conf.getKafka().getProperties();
	Map<String, Object> config = (Map) properties;
	ConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>(config);

	// Create concurrent consumer container factory.
	ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
	factory.setConsumerFactory(cf);
	factory.setConcurrency(conf.getKafka().getConcurrency());
	factory.setBatchListener(true);

	// Spring kafka container properties.
	ContainerProperties containerProps = factory.getContainerProperties();
	containerProps.setPollTimeout(conf.getKafka().getPollTimeout());
	// Bulk consumption change buffer queue size.
	containerProps.setQueueDepth(conf.getKafka().getQueueDepth());
	containerProps.setAckMode(AckMode.MANUAL_IMMEDIATE);
	return factory;
}
 
Example #2
Source File: KafkaConfigurer.java    From bird-java with 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
Source File: SpringKafkaSenderTest.java    From spring-kafka with 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 #4
Source File: SpringBootAsyncProducerApplication.java    From Spring-Boot-2.0-Projects with MIT License 5 votes vote down vote up
@Bean
public KafkaMessageListenerContainer<String, String> replyContainer(
        ConsumerFactory<String, String> cf) {
    ContainerProperties containerProperties = new ContainerProperties("asyncReplies");
    containerProperties.setGroupId("async");
    return new KafkaMessageListenerContainer<>(cf, containerProperties);
}
 
Example #5
Source File: KafkaRepository.java    From kafka-service-broker with 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 #6
Source File: S1pKafkaApplication.java    From grussell-spring-kafka with 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 #7
Source File: S1pKafkaApplication.java    From grussell-spring-kafka with 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 #8
Source File: SpringKafkaMainTest.java    From spring-kafka with 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 #9
Source File: ConsumingChannelConfig.java    From spring-kafka with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Bean
public ConcurrentMessageListenerContainer<String, String> kafkaListenerContainer() {
  ContainerProperties containerProps = new ContainerProperties(springIntegrationKafkaTopic);

  return (ConcurrentMessageListenerContainer<String, String>) new ConcurrentMessageListenerContainer<>(
      consumerFactory(), containerProps);
}
 
Example #10
Source File: KafkaRepository.java    From kafka-service-broker with 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 #11
Source File: ReceiverConfig.java    From spring-kafka with MIT License 4 votes vote down vote up
public static KafkaMessageListenerContainer<String, String> createMessageListenerContainer(
    ContainerProperties containerProperties,
    String bootstrapServers) {
  return new KafkaMessageListenerContainer<>(
      createConsumerFactory(bootstrapServers), containerProperties);
}