zoukankan      html  css  js  c++  java
  • RabbitMQ 生产与消费

    Spring Boot + RabbitMQ 整合

    新建Spring Boot 项目

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--AMQP-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    application.yml文件

    spring:
      rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
        publisher-confirms: true
        virtual-host: /
    
      application:
        name: rabbitMQ

     RabbitMQConfiguration.java

    package com.example.demo;
    
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfiguration {
    
        private static final String QUEUE_SIMPLE_NAME = "hello";
    
        // 队列
        @Bean
        public Queue queue(){
            return new Queue(QUEUE_SIMPLE_NAME);
        }
    
    }

    创建消息的消费者 Consumer,java

    package com.example.demo;
    
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * @author: DevanYan
     * @create: 2019-05-28 11:46
     */
    
    //消息的消费者
    @Component
    @RabbitListener(queues = "hello")
    public class Consumer {
    
        @RabbitHandler
        public void use(String message) {
            System.out.println("消费了一条消息 : " + message);
        }
    
    }

    创建消息的生产者 Creater.java

    package com.example.demo;
    
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    /**
     * @author: DevanYan
     * @create: 2019-05-28 11:47
     */
    
    // 消息的生产者
    @Component
    public class Creater {
    
        @Autowired
        private AmqpTemplate amqpTemplate;
    
        public void  create(){
            String message = "Hello RabbitMQ !";
            amqpTemplate.convertAndSend("hello", message);
            System.out.println("创建了一条消息 : " + message);
        }
    
    }

    编写测试类

    package com.example.demo;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {
        @Autowired
        Creater creater;
    
        @Test
        public void creatertest(){
            creater.create();
        }
    
    }

    控制台输出

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.0.9.RELEASE)
    
    2019-05-28 13:36:49.927  INFO 9052 --- [           main] com.example.demo.DemoApplicationTests    : Starting DemoApplicationTests on Yan with PID 9052 (started by Administrator in F:cloudRabbitMQ)
    2019-05-28 13:36:49.929  INFO 9052 --- [           main] com.example.demo.DemoApplicationTests    : No active profile set, falling back to default profiles: default
    2019-05-28 13:36:49.952  INFO 9052 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2ece4966: startup date [Tue May 28 13:36:49 CST 2019]; root of context hierarchy
    2019-05-28 13:36:50.507  INFO 9052 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$fd413eb4] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2019-05-28 13:36:51.141  INFO 9052 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
    2019-05-28 13:36:51.151  INFO 9052 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [127.0.0.1:5672]
    2019-05-28 13:36:51.231  INFO 9052 --- [ntContainer#0-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#f4c0e4e:0/SimpleConnection@64fbb393 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 49490]
    2019-05-28 13:36:51.318  INFO 9052 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 1.785 seconds (JVM running for 2.843)
    创建了一条消息 : Hello RabbitMQ !
    2019-05-28 13:36:51.424  INFO 9052 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2ece4966: startup date [Tue May 28 13:36:49 CST 2019]; root of context hierarchy
    2019-05-28 13:36:51.426  INFO 9052 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
    消费了一条消息 : Hello RabbitMQ !
    2019-05-28 13:36:51.466  INFO 9052 --- [       Thread-2] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2019-05-28 13:36:51.469  INFO 9052 --- [       Thread-2] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2019-05-28 13:36:51.469  INFO 9052 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
    2019-05-28 13:36:51.472  INFO 9052 --- [       Thread-2] o.s.a.r.l.SimpleMessageListenerContainer : Shutdown ignored - container is not active already
    Disconnected from the target VM, address: '127.0.0.1:49475', transport: 'socket'
    
    Process finished with exit code 0
  • 相关阅读:
    深度学习(十一) 残差网络
    深度学习(十) GoogleNet
    深度学习(九) 深度学习最全优化方法总结比较(SGD,Momentum,Nesterov Momentum,Adagrad,Adadelta,RMSprop,Adam)
    联合概率
    深度学习(八) Batch Normalization
    概率论
    设计者模式(八) 装饰者模式
    使用Adobe Photoshop CC 2015批量修改图片尺寸
    Turn.js 实现翻书效果的学习与总结
    TweenMax动画库学习(六)
  • 原文地址:https://www.cnblogs.com/devan/p/10936715.html
Copyright © 2011-2022 走看看