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

The following examples show how to use org.springframework.kafka.listener.MessageListenerContainer. 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
Source Project: spring-kafka   Source File: SpringKafkaReceiverTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  // set up the Kafka producer properties
  Map<String, Object> senderProperties =
      KafkaTestUtils.senderProps(AllSpringKafkaTests.embeddedKafka.getBrokersAsString());

  // create a Kafka producer factory
  ProducerFactory<String, String> producerFactory =
      new DefaultKafkaProducerFactory<String, String>(senderProperties);

  // create a Kafka template
  template = new KafkaTemplate<>(producerFactory);
  // set the default topic to send to
  template.setDefaultTopic(AllSpringKafkaTests.RECEIVER_TOPIC);

  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        AllSpringKafkaTests.embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 2
Source Project: spring-kafka   Source File: SpringKafkaReceiverTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  // set up the Kafka producer properties
  Map<String, Object> senderProperties =
      KafkaTestUtils.senderProps(
          embeddedKafka.getEmbeddedKafka().getBrokersAsString());

  // create a Kafka producer factory
  ProducerFactory<String, String> producerFactory =
      new DefaultKafkaProducerFactory<String, String>(
          senderProperties);

  // create a Kafka template
  template = new KafkaTemplate<>(producerFactory);
  // set the default topic to send to
  template.setDefaultTopic(RECEIVER_TOPIC);

  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getEmbeddedKafka().getPartitionsPerTopic());
  }
}
 
Example 3
@Override
public void unregisterChannelModel(ChannelModel channelModel, String tenantId, EventRepositoryService eventRepositoryService) {
    String endpointId = getEndpointId(channelModel, tenantId);
    // currently it is not possible to unregister a listener container
    // In order not to do a lot of the logic that Spring does we are manually accessing the containers to remove them
    // see https://github.com/spring-projects/spring-framework/issues/24228
    MessageListenerContainer listenerContainer = endpointRegistry.getListenerContainer(endpointId);
    if (listenerContainer != null) {
        listenerContainer.stop();
    }

    if (listenerContainer instanceof DisposableBean) {
        try {
            ((DisposableBean) listenerContainer).destroy();
        } catch (Exception e) {
            throw new RuntimeException("Failed to destroy listener container", e);
        }
    }

    Field listenerContainersField = ReflectionUtils.findField(endpointRegistry.getClass(), "listenerContainers");
    if (listenerContainersField != null) {
        listenerContainersField.setAccessible(true);
        @SuppressWarnings("unchecked")
        Map<String, MessageListenerContainer> listenerContainers = (Map<String, MessageListenerContainer>) ReflectionUtils
            .getField(listenerContainersField, endpointRegistry);
        if (listenerContainers != null) {
            listenerContainers.remove(endpointId);
        }
    } else {
        throw new IllegalStateException("Endpoint registry " + endpointRegistry + " does not have listenerContainers field");
    }
}
 
Example 4
Source Project: spring-kafka   Source File: SpringKafkaApplicationTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 5
Source Project: spring-kafka   Source File: SpringKafkaApplicationTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 6
Source Project: spring-kafka   Source File: SpringKafkaApplicationTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        AllSpringKafkaTests.embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 7
Source Project: spring-kafka   Source File: SpringKafkaApplicationTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 8
Source Project: spring-kafka   Source File: SpringKafkaApplicationTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 9
Source Project: spring-kafka   Source File: SpringKafkaApplicationTest.java    License: MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  // wait until the partitions are assigned
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
      .getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer,
        embeddedKafka.getPartitionsPerTopic());
  }
}
 
Example 10
@Around("anyCreateListenerContainer() || anyCreateContainer()")
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp)
		throws Throwable {
	MessageListenerContainer listener = (MessageListenerContainer) pjp.proceed();
	if (listener instanceof AbstractMessageListenerContainer) {
		AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) listener;
		Object someMessageListener = container.getContainerProperties()
				.getMessageListener();
		if (someMessageListener == null) {
			if (log.isDebugEnabled()) {
				log.debug("No message listener to wrap. Proceeding");
			}
		}
		else if (someMessageListener instanceof MessageListener) {
			container.setupMessageListener(createProxy(someMessageListener));
		}
		else {
			if (log.isDebugEnabled()) {
				log.debug("ATM we don't support Batch message listeners");
			}
		}
	}
	else {
		if (log.isDebugEnabled()) {
			log.debug("Can't wrap this listener. Proceeding");
		}
	}
	return listener;
}
 
Example 11
@Override
public void setupListenerContainer(MessageListenerContainer listenerContainer, MessageConverter messageConverter) {
    GenericMessageListener<ConsumerRecord<K, V>> messageListener = getMessageListener();
    Assert.state(messageListener != null, () -> "Endpoint [" + this + "] must provide a non null message listener");
    listenerContainer.setupMessageListener(messageListener);
}
 
Example 12
@Override
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp)
		throws Throwable {
	this.adapterWrapped = true;
	return Mockito.mock(MessageListenerContainer.class);
}
 
Example 13
@Override
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp)
		throws Throwable {
	this.adapterWrapped = true;
	return Mockito.mock(MessageListenerContainer.class);
}