RabbitMQ通讯方式第一讲:Hello World

一:在官网里面,为了便于理解,在介绍RabbitMQ时,列举了生动的例子:

 RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that the letter carrier will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office, and a letter carrier.

RabbitMQ 是一个消息代理: 它接受和转发消息。你可以把它想象成一个邮局: 当你把你想要投递的邮件放在一个邮箱里时,你可以确定信件的承运人最终会把邮件投递给你的收件人。在这个类比中,RabbitMQ 是一个邮箱、一个邮局和一个信件载体

二:入门的通讯方式Hello World:

       我们将使用Java语言编写两个程序,一个是发送单个消息的生产者,一个是接受消息的接收者。

在官网里面,这是hello world 的流程图片,但是需要注意的是,虽然官网没有给我们画exchange,但是并不代表没有使用,这里面使用的是默认的exchange。

我在这里为大家画了更加直观的图

三:下面为大家提供代码:

其实官网中为我们提供了教程:

这是官网地址:RabbitMQ tutorial - "Hello World!" | RabbitMQ  学习是可以查看官网

将官网提供的教程的图片部分展示一下,如果感觉看官网更好的话,可以去官网直接学习

3.1提供者的代码:

小编将获取代码的方式包装成了工具类代码如下:

public class RabbitMQConnectionUtil {
    //这个是linux的IP
    private static final String RABBITMQ_HOST ="*.*.*.*";
    //你设置的用户名
    private static final String RABBITMQ_USERNAME="******";
    //自己的密码
    private static final String RABBITMQ_PASSWORD="Handongkun123!";
    //默认是“/”
    private static final String RABBITMQ_VIRTUAL_HOST="/";
    //端口号默认是5672
    private static final int RABBITMQ_PORT=5672;
    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPassword(RABBITMQ_PASSWORD);
        factory.setUsername(RABBITMQ_USERNAME);
        factory.setVirtualHost(RABBITMQ_VIRTUAL_HOST);
        factory.setHost(RABBITMQ_HOST);
        factory.setPort(RABBITMQ_PORT);
        Connection connection = factory.newConnection();
        return connection;
    }
}
public static  final String QUEUE_NAME="hello";
    @Test
    public void P(){
        //获取连接
        try {
            Connection connection = RabbitMQConnectionUtil.getConnection();
            //获取通道
            Channel channel = connection.createChannel();
            //创建队列
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);

            //开始发布消息
            channel.basicPublish("",QUEUE_NAME,null,"第er次发送".getBytes());

            System.out.println("消息发送成功");

            //为了看的更加清楚  写一个System.in.read();  让程序卡在这
            int read = System.in.read();
            System.out.println(read);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

3.2消费者代码:

@Test
    public void getMsg() throws IOException, TimeoutException {
        //获取连接
        Connection connection = RabbitMQConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        DefaultConsumer  consumer=new DefaultConsumer(channel){
            //重写方法,处理数据
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body)
                    throws IOException
            {
                System.out.println("消费者获取到消息:"+new String(body,"utf-8"));
            }
        };
        channel.basicConsume(QUEUE_NAME,consumer);
        System.out.println("开始监听队列");
        System.in.read();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值