RabbitMQ快速入门之交换机

极速入门-消息生产与消费

ConnectionFactory:获取连接工厂
Connection:一个链接
Channel:数据通信通道,课发送和接收消息
Queue:具体的消息存储队列
Producer & Consumer:生产和消费者

1.创建一个springboot项目,勾选上我们的RibbitMQ,再导入pom依赖

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.6.5</version>
</dependency>

2.消费端

package com.pyc.rabbitmqapi.quickstart;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
 * @author椿
 * @create 2020-03-01 7:25
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
//        链接MQ服务器
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.0.101");
//        5672是MQ服务器的服务端口,15672是管控台端口
        factory.setPort(5672);
        factory.setVirtualHost("/");

//        创建connection
        Connection connection = factory.newConnection();

//        创建会话
        Channel channel = connection.createChannel();

//        绑定队列Queue(因为这里存放着将要消费的消息)
        String queueName = "test01";
//        1.队列的名称  2.是否持久化 3.是否独占 4.无消息是否自动删除 5.消息信息参数
//        queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
        channel.queueDeclare(queueName,true,false,false,null);

//        指定消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);

//        指定队列中的消息的签收方式
//        是否自动签收 true
        channel.basicConsume(queueName,true,consumer);

//        接受消费者
        while (true){
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("从MQ服务器队列中接受到的消息:" + msg);
        }
    }

}

3.生产端

package com.pyc.rabbitmqapi.quickstart;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
 * @author椿
 * @create 2020-03-01 7:29
 */
public class Procuder {
    public static void main(String[] args) throws Exception {
        //1 创建一个ConnectionFactory, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.0.101");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        //4 通过Channel发送数据
        for(int i=0; i < 5; i++){
            String msg = "Hello RabbitMQ!";
            //1 exchange   2 routingKey
            channel.basicPublish("", "test01", null, msg.g
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值