简体   繁体   English

Spring Boot - 使用 RabbitMQ 发送消息以连续监听消息队列

[英]Spring boot - Messaging with RabbitMQ to listen message queue continuously

When I run the Messaging with RabbitMQ guide [I tried this demo application][1] , the listener only receive the message once ( the runner send once and then exited ).当我使用 RabbitMQ 指南运行消息传递 [我尝试了这个演示应用程序][1] 时,侦听器只接收一次消息(运行程序发送一次然后退出)。 I hope the listener can continuously listen the message queue, how should I achieve it ?我希望监听器可以持续监听消息队列,我该如何实现呢?

the receiver code:接收器代码:

    package hello;

    import java.util.concurrent.CountDownLatch;
    import org.springframework.stereotype.Component;

    @Component
    public class Receiver {

      private CountDownLatch latch = new CountDownLatch(1);

      public void receiveMessage(String message) {
          System.out.println("Received <" + message + ">");
          latch.countDown();
      }

      public CountDownLatch getLatch() {
          return latch;
      }

  }
the runner code: 跑步者代码:
    [opuser@iZ25fprd8a9Z java]$ java -jar gs-messaging-rabbitmq-0.1.0.jar 

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.1.1.RELEASE)

    2019-01-10 09:40:53.846  INFO 412 --- [           main] hello.Application                        : Starting Application on iZ25fprd8a9Z with PID 412 (/data/workspace/test/java/gs-messaging-rabbitmq-0.1.0.jar started by opuser in /data/workspace/test/java)
    2019-01-10 09:40:53.854  INFO 412 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
    2019-01-10 09:40:55.019  INFO 412 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$870d6481] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2019-01-10 09:40:56.047  INFO 412 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [10.171.135.109:5672]
    2019-01-10 09:40:56.145  INFO 412 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#76f2b07d:0/SimpleConnection@49c43f4e [delegate=amqp://petmq@10.171.135.109:5672/, localPort= 41068]
    2019-01-10 09:40:56.152  INFO 412 --- [           main] o.s.amqp.rabbit.core.RabbitAdmin         : Auto-declaring a non-durable, auto-delete, or exclusive Queue (spring-boot) durable:false, auto-delete:false, exclusive:false. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
    2019-01-10 09:40:56.244  INFO 412 --- [           main] hello.Application                        : Started Application in 3.116 seconds (JVM running for 4.001)
    Sending message...
    Received <====1===========Hello from RabbitMQ!>
    Received <====2===========Hello from RabbitMQ!>
    Received <=====3==========Hello from RabbitMQ!>
    2019-01-10 09:40:56.269  INFO 412 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2019-01-10 09:40:57.267  INFO 412 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2019-01-10 09:40:57.272  INFO 412 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Shutdown ignored - container is not active already
the application code: 应用程序代码:
 ackage hello; import hello.test.TestListener; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { static final String topicExchangeName = "spring-boot-exchange"; public static final String queueName = "spring-boot"; @Bean Queue queue() { return new Queue(queueName, false); } @Bean TopicExchange exchange() { return new TopicExchange(topicExchangeName); } @Bean Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#"); } @Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver, "receiveMessage"); } public static void main(String[] args) throws InterruptedException { SpringApplication.run(Application.class, args).close(); } }
the log 日志
 [opuser@iZ25fprd8a9Z java]$ java -jar gs-messaging-rabbitmq-0.1.0.jar . ____ _ __ _ _ /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\ ( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\ \\\\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.1.RELEASE) 2019-01-10 09:40:53.846 INFO 412 --- [ main] hello.Application : Starting Application on iZ25fprd8a9Z with PID 412 (/data/workspace/test/java/gs-messaging-rabbitmq-0.1.0.jar started by opuser in /data/workspace/test/java) 2019-01-10 09:40:53.854 INFO 412 --- [ main] hello.Application : No active profile set, falling back to default profiles: default 2019-01-10 09:40:55.019 INFO 412 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$870d6481] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2019-01-10 09:40:56.047 INFO 412 --- [ main] osarcCachingConnectionFactory : Attempting to connect to: [10.171.135.109:5672] 2019-01-10 09:40:56.145 INFO 412 --- [ main] osarcCachingConnectionFactory : Created new connection: rabbitConnectionFactory#76f2b07d:0/SimpleConnection@49c43f4e [delegate=amqp://petmq@10.171.135.109:5672/, localPort= 41068] 2019-01-10 09:40:56.152 INFO 412 --- [ main] osamqp.rabbit.core.RabbitAdmin : Auto-declaring a non-durable, auto-delete, or exclusive Queue (spring-boot) durable:false, auto-delete:false, exclusive:false. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost. 2019-01-10 09:40:56.244 INFO 412 --- [ main] hello.Application : Started Application in 3.116 seconds (JVM running for 4.001) Sending message... Received <====1===========Hello from RabbitMQ!> Received <====2===========Hello from RabbitMQ!> Received <=====3==========Hello from RabbitMQ!> 2019-01-10 09:40:56.269 INFO 412 --- [ main] osarlSimpleMessageListenerContainer : Waiting for workers to finish. 2019-01-10 09:40:57.267 INFO 412 --- [ main] osarlSimpleMessageListenerContainer : Successfully waited for workers to finish. 2019-01-10 09:40:57.272 INFO 412 --- [ main] osarlSimpleMessageListenerContainer : Shutdown ignored - container is not active already

If you add the dependency如果添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Your application will not close out.您的应用程序不会关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM