Java Code Examples for org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer#setMessageListener()

The following examples show how to use org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer#setMessageListener() . 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: RabbitConfigurer.java    From bird-java with MIT License 6 votes vote down vote up
@Bean
@ConditionalOnProperty(value = EventbusConstant.Rabbit.LISTENER_PACKAGES)
public SimpleMessageListenerContainer messageContainer(Queue queue, EventDispatcher eventDispatcher) {
    for (String topic : eventDispatcher.getAllTopics()) {
        this.initExchange(topic,queue);
    }

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
    container.setQueues(queue);
    container.setMessageConverter(messageConverter());
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(1);
    container.setConcurrentConsumers(1);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认

    RabbitEventArgListener listener = new RabbitEventArgListener(eventDispatcher);
    container.setMessageListener(listener);
    return container;
}
 
Example 2
Source File: AmqpConfig.java    From myth with Apache License 2.0 6 votes vote down vote up
/**
 * Message container simple message listener container.
 *
 * @return the simple message listener container
 */
@Bean
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host")
public SimpleMessageListenerContainer messageContainer() {
    SimpleMessageListenerContainer container =
            new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(3);
    container.setConcurrentConsumers(1);
    //设置确认模式手工确认
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> {
        byte[] messageBody = message.getBody();
        LogUtil.debug(LOGGER, () -> "springcloud  account服务  amqp接收消息");
        //确认消息成功消费
        final Boolean success = mythMqReceiveService.processMessage(messageBody);
        if (success) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    });
    return container;
}
 
Example 3
Source File: AmqpConfig.java    From myth with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "host")
public SimpleMessageListenerContainer messageContainer() {
    SimpleMessageListenerContainer container =
            new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(1);
    container.setConcurrentConsumers(1);
    //设置确认模式手工确认
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    container.setMessageListener((ChannelAwareMessageListener) (message, channel) -> {
        byte[] messageBody = message.getBody();
        //确认消息成功消费
        final Boolean success = mythMqReceiveService.processMessage(messageBody);
        if (success) {
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    return container;
}
 
Example 4
Source File: AmqpMessagingApplication.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(
		ConnectionFactory connectionFactory,
		MessageListenerAdapter listenerAdapter) {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setConnectionFactory(connectionFactory);
	container.setQueueNames("test.queue");
	container.setMessageListener(listenerAdapter);

	return container;
}
 
Example 5
Source File: RabbitAutoConfiguration.java    From easy-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(MessageConverter converter,
    ConnectionFactory connectionFactory, @Autowired(required = false) List<Consumer<?>> list) {
  SimpleMessageListenerContainer container =
      new SimpleMessageListenerContainer(connectionFactory);
  init(list);
  Receiver receiver = new Receiver(converter);
  container.setMessageListener(new MessageListenerAdapter(receiver, converter));
  container.setQueueNames(queueNames.toArray(new String[0]));
  container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
  container.setPrefetchCount(prefetchCount);
  container.setTxSize(txSize);
  return container;
}
 
Example 6
Source File: SpringIntegrationConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "destroy")
public SimpleMessageListenerContainer simpleMessageListenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(queue());
    container.setMessageListener(loggingMessageListenerAdapter());
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);

    return container;
}
 
Example 7
Source File: AmqpConfiguration.java    From SkyEye with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(this.queue);
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(this.maxConcurrentConsumers);
    container.setConcurrentConsumers(this.concurrentConsumers);
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example 8
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter, Queue queue) {
    log.info("init simpleMessageListenerContainer: {}", queue.getName());
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(queue.getName());
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example 9
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter, Queue queue) {
    log.info("init simpleMessageListenerContainer {}", queue.getName());
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(queue.getName());
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example 10
Source File: BusConfig.java    From SpringCloud with Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer mqContainer(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    log.info("init mqContainer");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(QUEUE_NAME);
    container.setMessageListener(listenerAdapter);
    return container;
}
 
Example 11
Source File: MessageListenerConfig_Pre_1_4_0.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory, TestMessageHandler testMessageHandler) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(RabbitMQTestConstants.QUEUE_PUSH);
    container.setMessageListener(new MessageListenerAdapter(testMessageHandler));
    return container;
}
 
Example 12
Source File: BusConfig.java    From JetfireCloud with Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer mqContainer(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    log.info("init mqContainer");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(QUEUE_NAME);
    container.setMessageListener(listenerAdapter);
    return container;
}
 
Example 13
Source File: RabbitConfig.java    From notes with Apache License 2.0 5 votes vote down vote up
/**
     * 创建制定的 监听容器
     *
     * @param queueName  监听的队列名字
     * @param listenerChannel 设置是否将监听的频道 公开给已注册的
     * @param PrefetchCount  告诉代理一次请求多少条消息过来
     * @param ConcurrentConsumers  制定创建多少个并发的消费者数量
     * @param acknowledgeMode  消息确认模式
     * @param listener 监听器
     * @return org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
     * @author fruiqi
     * @date 19-2-11 下午4:13
     **/
    public SimpleMessageListenerContainer setSimpleMessageListenerContainer(String queueName, boolean listenerChannel,
                                                                            int PrefetchCount, int ConcurrentConsumers,
                                                                            AcknowledgeMode acknowledgeMode,
                                                                            ChannelAwareMessageListener listener) throws Exception {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(getConnection());
//        container.setQueueNames(getMACAddressRrefix(queueName));
        container.setQueueNames(queueName);
        container.setExposeListenerChannel(listenerChannel);
        container.setPrefetchCount(PrefetchCount);
        container.setConcurrentConsumers(ConcurrentConsumers);
        container.setAcknowledgeMode(acknowledgeMode);
        container.setMessageListener(listener);
        return container;
    }
 
Example 14
Source File: ScrapingResultConsumerConfiguration.java    From scraping-microservice-java-python-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer listenerContainer() {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setConnectionFactory(connectionFactory());
	container.setQueueNames(this.scrapingResultQueue);
	container.setMessageListener(messageListenerAdapter());

	return container;
}
 
Example 15
Source File: SpringIntegrationTest.java    From rabbitmq-mock with Apache License 2.0 5 votes vote down vote up
@Test
void basic_consume_case() {
    String messageBody = "Hello world!";
    try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AmqpConfiguration.class)) {
        RabbitTemplate rabbitTemplate = queueAndExchangeSetup(context);

        Receiver receiver = new Receiver();
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(context.getBean(ConnectionFactory.class));
        container.setQueueNames(QUEUE_NAME);
        container.setMessageListener(new MessageListenerAdapter(receiver, "receiveMessage"));
        try {
            container.start();

            rabbitTemplate.convertAndSend(EXCHANGE_NAME, "test.key2", messageBody);

            List<String> receivedMessages = new ArrayList<>();
            assertTimeoutPreemptively(ofMillis(500L), () -> {
                    while (receivedMessages.isEmpty()) {
                        receivedMessages.addAll(receiver.getMessages());
                        TimeUnit.MILLISECONDS.sleep(100L);
                    }
                }
            );

            assertThat(receivedMessages).containsExactly(messageBody);
        } finally {
            container.stop();
        }
    }
}
 
Example 16
Source File: RabbitWithoutRabbitTemplateConfig.java    From java-spring-rabbitmq with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer messageListenerContainer(
    TestMessageListener messageListener, ConnectionFactory connectionFactory, Queue queue) {
  SimpleMessageListenerContainer container =
      new SimpleMessageListenerContainer(connectionFactory);
  container.setQueues(queue);
  container.setMessageListener(messageListener);

  return container;
}
 
Example 17
Source File: SzzRestApplication.java    From OpenSZZ-Cloud-Native with GNU General Public License v3.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer containerAnaylsis(ConnectionFactory connectionFactory) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setQueueNames(queueNameSzz);
    container.setMessageListener(new MessageReceivedComponent(rabbitTemplate(connectionFactory())));
    return container;
}
 
Example 18
Source File: SzzRestApplication.java    From OpenSZZ-Cloud-Native with GNU General Public License v3.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer containerAnaylsis(ConnectionFactory connectionFactory) {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setConnectionFactory(connectionFactory());
	container.setQueueNames(queueNameSzz);
	container.setMessageListener(new MessageReceivedComponent(rabbitTemplate(connectionFactory()), dbEntryDao));
	return container;
}
 
Example 19
Source File: AmqpServer.java    From tutorials with MIT License 4 votes vote down vote up
@Bean SimpleMessageListenerContainer listener(ConnectionFactory factory, AmqpInvokerServiceExporter exporter, Queue queue) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(factory);
    container.setMessageListener(exporter);
    container.setQueueNames(queue.getName());
    return container;
}
 
Example 20
Source File: OrderConsumer.java    From SpringBoot-Course with MIT License 4 votes vote down vote up
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);

    // 监听队列,可以同时设置多个
    container.setQueues(orderDirectQueue());

    // 设置当前的消费者数量
    container.setConcurrentConsumers(2);

    // 设置最大的消费者数量
    container.setMaxConcurrentConsumers(10);

    // 设置重回队列,false 不会重回队列
    container.setDefaultRequeueRejected(false);

    // 设置签收模式为手动签收,自动签收为 AcknowledgeMode.AUTO
    // container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 手动签收
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);

    // 监听消费端方法一
    OrderConsumerMessage orderConsumerMessage = new OrderConsumerMessage();
    container.setMessageListener(orderConsumerMessage);

    // 监听消费端方法二
    //        container.setMessageListener(new ChannelAwareMessageListener() {
    //            @Override
    //            public void onMessage(Message message, Channel channel) throws Exception {
    //                String msg = new String(message.getBody());
    //
    //                System.out.println("----------- Message -----------");
    //                System.out.println(msg);
    //            }
    //        });

    // 监听消费端方法三 | 一
    // 适配器方式
    //        MessageListenerAdapter adapter = new MessageListenerAdapter(new OrderConsumerMessageDelegate());
    //        adapter.setDefaultListenerMethod("orderConsumerMsg"); // 自定义适配器方法名
    //        container.setMessageListener(adapter);

    // 监听消费端方法三 | 二
    // 适配器方式
    //        MessageListenerAdapter adapter = new MessageListenerAdapter(new OrderConsumerMessageDelegate());
    //        adapter.setDefaultListenerMethod("jsonOrderConsumerMsg"); // 自定义适配器方法名
    //
    //        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
    //        adapter.setMessageConverter(jackson2JsonMessageConverter); // 消息转为 json
    //
    //        container.setMessageListener(adapter);

    System.out.println("消费端重启成功");

    return container;
}