org.springframework.amqp.rabbit.listener.MessageListenerContainer Java Examples

The following examples show how to use org.springframework.amqp.rabbit.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: MessageListenerAccessor.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private List<SimpleMessageListenerContainer> collectListenerContainers() {
	List<SimpleMessageListenerContainer> listenerContainers = new ArrayList<>();
	if (this.simpleMessageListenerContainers != null) {
		listenerContainers.addAll(this.simpleMessageListenerContainers);
	}
	if (this.rabbitListenerEndpointRegistry != null) {
		for (MessageListenerContainer listenerContainer : this.rabbitListenerEndpointRegistry
				.getListenerContainers()) {
			if (listenerContainer instanceof SimpleMessageListenerContainer) {
				listenerContainers
						.add((SimpleMessageListenerContainer) listenerContainer);
			}
		}
	}
	return listenerContainers;
}
 
Example #2
Source File: RabbitChannelDefinitionProcessor.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 #3
Source File: AmqpReactiveController.java    From tutorials with MIT License 4 votes vote down vote up
/**
 * Receive messages for the given queue
 * @param name
 * @param errorHandler 
 * @return
 */
@GetMapping(value = "/queue/{name}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<?> receiveMessagesFromQueue(@PathVariable String name) {

    DestinationsConfig.DestinationInfo d = destinationsConfig.getQueues()
        .get(name);

    if (d == null) {
        return Flux.just(ResponseEntity.notFound()
            .build());
    }

    MessageListenerContainer mlc = messageListenerContainerFactory.createMessageListenerContainer(d.getRoutingKey());

    Flux<String> f = Flux.<String> create(emitter -> {

        log.info("[I168] Adding listener, queue={}", d.getRoutingKey());
        mlc.setupMessageListener((MessageListener) m -> {

            String qname = m.getMessageProperties()
                .getConsumerQueue();

            log.info("[I137] Message received, queue={}", qname);

            if (emitter.isCancelled()) {
                log.info("[I166] cancelled, queue={}", qname);
                mlc.stop();
                return;
            }

            String payload = new String(m.getBody());
            emitter.next(payload);

            log.info("[I176] Message sent to client, queue={}", qname);

        });

        emitter.onRequest(v -> {
            log.info("[I171] Starting container, queue={}", d.getRoutingKey());
            mlc.start();
        });

        emitter.onDispose(() -> {
            log.info("[I176] onDispose: queue={}", d.getRoutingKey());
            mlc.stop();
        });

        log.info("[I171] Container started, queue={}", d.getRoutingKey());

    });
    

    return Flux.interval(Duration.ofSeconds(5))
        .map(v -> {
            log.info("[I209] sending keepalive message...");
            return "No news is good news";
        })
        .mergeWith(f);
}
 
Example #4
Source File: AmqpReactiveController.java    From tutorials with MIT License 4 votes vote down vote up
@GetMapping(value = "/topic/{name}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<?> receiveMessagesFromTopic(@PathVariable String name) {

    DestinationsConfig.DestinationInfo d = destinationsConfig.getTopics()
        .get(name);

    if (d == null) {
        return Flux.just(ResponseEntity.notFound()
            .build());
    }

    Queue topicQueue = createTopicQueue(d);
    String qname = topicQueue.getName();

    MessageListenerContainer mlc = messageListenerContainerFactory.createMessageListenerContainer(qname);

    Flux<String> f = Flux.<String> create(emitter -> {

        log.info("[I168] Adding listener, queue={}", qname);

        mlc.setupMessageListener((MessageListener) m -> {

            log.info("[I137] Message received, queue={}", qname);

            if (emitter.isCancelled()) {
                log.info("[I166] cancelled, queue={}", qname);
                mlc.stop();
                return;
            }

            String payload = new String(m.getBody());
            emitter.next(payload);

            log.info("[I176] Message sent to client, queue={}", qname);

        });

        emitter.onRequest(v -> {
            log.info("[I171] Starting container, queue={}", qname);
            mlc.start();
        });

        emitter.onDispose(() -> {
            log.info("[I176] onDispose: queue={}", qname);
            amqpAdmin.deleteQueue(qname);
            mlc.stop();
        });            

        log.info("[I171] Container started, queue={}", qname);

      });
    
    return Flux.interval(Duration.ofSeconds(5))
      .map(v -> {
            log.info("[I209] sending keepalive message...");
            return "No news is good news";
      })
      .mergeWith(f);

}
 
Example #5
Source File: MessageListenerContainerFactory.java    From tutorials with MIT License 3 votes vote down vote up
public MessageListenerContainer createMessageListenerContainer(String queueName) {

        SimpleMessageListenerContainer mlc = new SimpleMessageListenerContainer(connectionFactory);

        mlc.addQueueNames(queueName);
        mlc.setAcknowledgeMode(AcknowledgeMode.AUTO);

        return mlc;
    }