RabbitMQ生产者消费者模型构建(三)

ConnectionFactory:获取连接(地址,端口号,用户名,密码,虚拟主机等)

Connection:一个连接

Channel:数据通信信道,可发送、接收消息

Queue:具体的消息存储队列

Producer&Consumer:生产和消费者

//生产端代码

//创建连接工厂,进行设置
 ConnectionFactory connectionFactory = new ConnectionFactory();
 connectionFactory.setHost("127.0.0.1");
 connectionFactory.setPort(5672);
 connectionFactory.setVirtualHost("/");
 connectionFactory.setUsername("guest");
 connectionFactory.setPassword("guest");

 //创建连接
 Connection connection = connectionFactory.newConnection();

 //创建通道
 Channel channel = connection.createChannel();

 //发送数据
 String str = "hello world";
 channel.basicPublish("", "test001", null, str.getBytes());

 //关闭连接
 channel.close();
 connection.close();
//消费者端代码

//1 创建一个ConnectionFactory, 并进行配置
 ConnectionFactory connectionFactory = new     ConnectionFactory();
 connectionFactory.setHost("127.0.0.1");
 connectionFactory.setPort(5672);
 connectionFactory.setVirtualHost("/");
        
 //2 通过连接工厂创建连接
 Connection connection = connectionFactory.newConnection();
        
 //3 通过connection创建一个Channel
 Channel channel = connection.createChannel();
        
 //4 声明(创建)一个队列
 String queueName = "test001";
 //String queue, boolean durable, boolean exclusive, boolean  autoDelete, Map<String, Object> arguments
 //队列名称 (true持久化,队列不会消失), 独占(顺序队列,只有一个队列可以连接)
 channel.queueDeclare(queueName, true, false, false, null);
        
 //5 创建消费者
 QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        
 //6 设置Channel
 channel.basicConsume(queueName, true, queueingConsumer);
        
 while(true){
    //7 获取消息
    Delivery delivery = queueingConsumer.nextDelivery();
    String msg = new String(delivery.getBody());
    System.err.println("消费端: " + msg);
    Map<String, Object> headers = delivery.getProperties().getHeaders();
    System.err.println("headers get my1 value: " + headers.get("my1"));
            
    //Envelope envelope = delivery.getEnvelope();
 }

 

//pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--     <dependency>
               <groupId>com.rabbitmq</groupId>
               <artifactId>amqp-client</artifactId>
               <version>3.6.5</version>
             </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

 

posted @ 2019-07-09 11:20  鹿小框  阅读(503)  评论(0编辑  收藏  举报