org.springframework.kafka.listener.MessageListenerContainer Java Examples

The following examples show how to use org.springframework.kafka.listener.MessageListenerContainer. 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: SpringKafkaReceiverTest.java    From spring-kafka with 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 File: SpringKafkaReceiverTest.java    From spring-kafka with 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
Source File: KafkaChannelDefinitionProcessor.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@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 File: SpringKafkaApplicationTest.java    From spring-kafka with 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 File: SpringKafkaApplicationTest.java    From spring-kafka with 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 File: SpringKafkaApplicationTest.java    From spring-kafka with 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 File: SpringKafkaApplicationTest.java    From spring-kafka with 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 File: SpringKafkaApplicationTest.java    From spring-kafka with 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 File: SpringKafkaApplicationTest.java    From spring-kafka with 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
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@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
Source File: SimpleKafkaListenerEndpoint.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@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
Source File: TraceMessagingAutoConfiguration1664Tests.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Override
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp)
		throws Throwable {
	this.adapterWrapped = true;
	return Mockito.mock(MessageListenerContainer.class);
}
 
Example #13
Source File: TraceMessagingAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Override
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp)
		throws Throwable {
	this.adapterWrapped = true;
	return Mockito.mock(MessageListenerContainer.class);
}