RabbitMQ学习笔记(2)----RabbitMQ简单队列(Hello World)的使用

1. 简单队列结构图

  

2. 引入依赖

  pom.xml文件

 <dependency>
      <groupId>com.rabbitmq</groupId>
      <artifactId>amqp-client</artifactId>
      <version>5.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.8.0-beta2</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.8.0-beta2</version>
    </dependency>

3. 生产者生产消息

  Producer如下:

package com.wangx.rabbitmq.helloworld;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    /**
     * 队列名字
     */
    private static final String QUEUE_NAME = "MyQueue";
    public static void main(String[] args) throws IOException, TimeoutException {

        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置服务器主机
        factory.setHost("localhost");
        //设置用户名
        factory.setUsername("wangx");
        //设置密码
        factory.setPassword("wangx");
        //设置VirtualHost
        factory.setVirtualHost("/wangx");
        Connection connection = null;
        Channel channel = null;
        try {

            //创建连接
            connection = factory.newConnection();
            //创建消息通道
            channel = connection.createChannel();
            //声明队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            //发送消息
            for (int i = 0; i < 10; i++) {
                //发送消息
                channel.basicPublish("", QUEUE_NAME, null, (message + i).getBytes());
                System.out.println(" [x] Sent '" + message + i + "'");
            }
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            channel.close();
            connection.close();
        }
    }
}

  运行发送消息,然后查看控制台:

  

  可以看到已经存在了刚刚的十条消息了。

4. 消费者消费消息

package com.wangx.rabbitmq.helloworld;

import com.rabbitmq.client.*;
import com.rabbitmq.client.impl.recovery.QueueRecoveryListener;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MyConsumer {
    /**
     * 队列名字
     */
    private static final String QUEUE_NAME = "MyQueue";
    public static void main(String[] args) throws IOException, TimeoutException {

        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置服务器主机
        factory.setHost("localhost");
        //设置用户
        factory.setUsername("wangx");
        //设置密码
        factory.setPassword("wangx");
        //设置VirtualHost
        factory.setVirtualHost("/wangx");
        Connection connection = null;
        Channel channel = null;
        try {

            //创建连接
            connection = factory.newConnection();
            //创建消息通道
            channel = connection.createChannel();
            //声明队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            Consumer consumer = new DefaultConsumer(channel){
                //重写DefaultConsumer中handleDelivery方法,在方法中获取消息
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope,
                                           AMQP.BasicProperties properties, byte[] body) throws IOException{
                    String message = new String(body, "UTF-8");
                    System.out.println("收到消息 '" + message + "'");
                }
            };
            //监听消息
            channel.basicConsume(QUEUE_NAME, true,consumer);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
        }
    }
}

  启动消费者,可以看到将会成功消费刚刚生产的十条消息。

  控制台打印如下:

收到消息 'Hello World!0'
收到消息 'Hello World!1'
收到消息 'Hello World!2'
收到消息 'Hello World!3'
收到消息 'Hello World!4'
收到消息 'Hello World!5'
收到消息 'Hello World!6'
收到消息 'Hello World!7'
收到消息 'Hello World!8'
收到消息 'Hello World!9'

  查看rabbitmq:

  

 

  刚刚生产者生产的消息已经不存在了,表示已经被消费了。

 

转载于:https://www.cnblogs.com/Eternally-dream/p/9968542.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值