RabbitMQ笔记

准备工作

  • 使用页面方式添加用户,虚拟主机,配置用户可以访问的虚拟主机
    在这里插入图片描述

6种模型

HelloWorld模型–直连

  • 生产者
public class Provider {

    public static void main(String[] args) throws IOException, TimeoutException {
        /*//创建连接mq的连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接rbmq的主机
        connectionFactory.setHost("ip");
        //设置端口
        connectionFactory.setPort(5672);
        //设置连接那个虚拟机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("******");
        //获取连接对象
        Connection connection = connectionFactory.newConnection();*/

        //获取连接
        Connection connection = RbmqUtil.getConnection();

        //获取连接中通道
        Channel channel = connection.createChannel();
        //通道绑定对应的消息队列
        //参数1:队列名称,如果不存在就创建
        //参数2:定义队列特性是否要持久化,
        //参数3:exclusive 是否独占队列
        //参数4:autoDelete 是否在消费完自动删除队列
        //参数5: 额外附加参数
        channel.queueDeclare("hello",false, false,false,null);
        //发布消息
        //参数1:交换机
        //参数2:队列的名称
        //参数3:额外属性设置
        //参数3:具体内容
        channel.basicPublish("","hello",null,"hello".getBytes());

        /*//关闭通道
        channel.close();
        //关闭连接
        connection.close();*/

        RbmqUtil.closeChannelAndConnection(channel,connection);
    }

}
  • 消费者
public class Customer {

    public static void main(String[] args) throws IOException, TimeoutException {
        /*//创建连接mq的连接工厂对象
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接rbmq的主机
        connectionFactory.setHost("ip");
        //设置端口
        connectionFactory.setPort(5672);
        //设置连接那个虚拟机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("******");
        //获取连接对象
        Connection connection = connectionFactory.newConnection();*/

        //获取连接
        Connection connection = RbmqUtil.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();

        channel.queueDeclare("hello",false, false,false,null);

        channel.basicConsume("hello",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(LocalDateTime.now()+"--"+new String(body));
            }
        });

    }
}
  • 封装工具类
package com.wu.utils;

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

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

/**
 * @program: RabbitMq
 * @author: Mr-Jies
 * @create: 2020-06-07 14:36
 **/

public class RbmqUtil {

    private static  ConnectionFactory connectionFactory;

    static {
        //创建连接mq的连接工厂对象  -- ConnectionFactory重量级资源  类加载执行  只执行一次
        connectionFactory = new ConnectionFactory();
        //设置连接rbmq的主机
        connectionFactory.setHost("ip");
        //设置端口
        connectionFactory.setPort(5672);
        //设置连接那个虚拟机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("***");
    }

    //创建连接
    public static Connection getConnection() {
        Connection connection = null;
        try {
            //获取连接对象
            connection = connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //关闭连接和通道
    public static void closeChannelAndConnection(Channel channel, Connection connection) {

        try {
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

}

Work queues–任务队列

  • 平分消费
public class Provider {

    public static void main(String[] args) throws IOException {

        Connection connection = RbmqUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("work",true,false,false,null);

        for (int i = 0; i <100; i++) {
            channel.basicPublish("","work",null,(i+":发送了消息").getBytes());
        }

        RbmqUtil.closeChannelAndConnection(channel,connection);

    }
}

public class Customer01 {

    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("work",true,false,false,null);
        channel.basicConsume("work",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(LocalDateTime.now()+"----消费者:01:"+new String(body));
            }
        });
    }
}

public class Customer02 {

    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("work",true,false,false,null);
        channel.basicConsume("work",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(LocalDateTime.now()+"----消费者:02:"+new String(body));
            }
        });
    }
}
  • 消息确认
public class Customer01 {

    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();

        //每次只消费一个通知
        channel.basicQos(1);

        channel.queueDeclare("work",true,false,false,null);

//        channel.basicConsume("work",true,new DefaultConsumer(channel){
        channel.basicConsume("work",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(LocalDateTime.now()+"----消费者:01:"+new String(body));
                //确认消息
                //参数1: 确认队列中哪个具体消息  参数2: 是否开启多个消息的同时确认
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}


public class Customer02 {

    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();

        //每次只消费一个通知
        channel.basicQos(1);

        channel.queueDeclare("work",true,false,false,null);

        //参数1: 队列名称 参数2:消息自动确定 true --消费者自动向rabbitmq确认消息消费 :  false 不会自动确认消息
//        channel.basicConsume("work",true,new DefaultConsumer(channel){
        channel.basicConsume("work",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(LocalDateTime.now()+"----消费者:02:"+new String(body));
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //确认消息
                //参数1: 确认队列中哪个具体消息  参数2: 是否开启多个消息的同时确认
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}

Publish/Subscribe 发布订阅

  • fanout
public class Provider {
    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();
        //声明交换机
        //参数1:交换机的名称  参数2: fanout为广播
        channel.exchangeDeclare("logs","fanout");
        //发布消息
        channel.basicPublish("logs","",null,"这是一条广播信息!!!".getBytes());

        RbmqUtil.closeChannelAndConnection(channel,connection);

    }
}



public class Customer01 {

    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();
        //绑定交换机
        channel.exchangeDeclare("logs","fanout");
        //创建临时队列
        String queue = channel.queueDeclare().getQueue();
        //将临时队列绑定exchange
        channel.queueBind(queue,"logs","");
        //处理消息
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者01---"+new String(body));
            }
        });
    }
}

路由 Routing

  • direct
public class Provider {

    public static void main(String[] args) throws IOException {

        Connection connection = RbmqUtil.getConnection();

        Channel channel = connection.createChannel();
        channel.exchangeDeclare("logs_direct","direct");
        String routKey = "news";
        channel.basicPublish("logs_direct",routKey,null,"这是一条日志信息".getBytes());

        RbmqUtil.closeChannelAndConnection(channel,connection);

    }
}

public class Customer01 {
    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();

        String exchange = "logs_direct";
        String routKey = "log";
        channel.exchangeDeclare(exchange,"direct");
        //创建临时队列
        String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue,exchange,routKey);

        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者--1--"+new String(body));
            }
        });

    }
}

动态路由 Topics

  • topic
public class Provider {
    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("topics","topic");
        channel.basicPublish("topics","user.save.2",null,"这是一条tops信息".getBytes());
        RbmqUtil.closeChannelAndConnection(channel,connection);
    }
}


public class Customer01 {
    public static void main(String[] args) throws IOException {
        Connection connection = RbmqUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("topics", "topic");
        //创建临时队列
        String queue = channel.queueDeclare().getQueue();
        //    *  匹配一个单词
        channel.queueBind(queue, "topics", "user.*");
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("-----" + new String(body));
            }
        });
    }
}

Springboot整合

spring:
  application:
    name: rabbit_mq
  rabbitmq:
    host: ***.**
    password: ...
    port: 5672
    username: ems
    virtual-host: /ems

hello

@Test  //hello
void contextLoads() {
    rabbitTemplate.convertAndSend("hello","hello world!!!!");
}

@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloController {

    @RabbitHandler
    public void receivel(String body){
        System.out.println("hello消费--"+body);
    }

}

广播

@Test //fanout
void testFanout(){
    rabbitTemplate.convertAndSend("logs","","测试广播!!!");
}


@Component
public class FanoutController {

    @RabbitListener(bindings={
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout") //绑定交换机
            )
    })
    public void hello(String msg){
        System.out.println("hello----"+msg);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "logs",type = "fanout")
            )
    })
    public void work(String msg){
        System.out.println("work----"+msg);
    }
}

路由

@Test //direct
void testDirect(){
    rabbitTemplate.convertAndSend("pros","log","路由!!");
}

@Component
public class RountController {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "pros",type = "direct"),
                    key = {"pro","log"}
            )
    })
    public void msg01(String msg){
        System.out.println("msg01------"+msg);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "pros",type = "direct"),
                    key = {"log"}
            )
    })
    public void msg02(String msg){
        System.out.println("msg02------"+msg);
    }

}

动态路由

@Test  //topic
void testTopic(){
    rabbitTemplate.convertAndSend("top","user.name.66","小米");
}

@Component
public class TopController {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "top",type = "topic"),
                    key = {"user.#"}
            )
    })
    public void msg01(String msg){
        System.out.println("msg01"+msg);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(name = "top",type = "topic"),
                    key = {"user.*"}
            )
    })

    public void msg02(String msg){
        System.out.println("msg02"+msg);
    }
}

RabbitMQ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于C++&OPENCV 的全景图像拼接 C++是一种广泛使用的编程语言,它是由Bjarne Stroustrup于1979年在新泽西州美利山贝尔实验室开始设计开发的。C++是C语言的扩展,旨在提供更强大的编程能力,包括面向对象编程和泛型编程的支持。C++支持数据封装、继承和多态等面向对象编程的特性和泛型编程的模板,以及丰富的标准库,提供了大量的数据结构和算法,极大地提高了开发效率。12 C++是一种静态类型的、编译式的、通用的、大小写敏感的编程语言,它综合了高级语言和低级语言的特点。C++的语法与C语言非常相似,但增加了许多面向对象编程的特性,如类、对象、封装、继承和多态等。这使得C++既保持了C语言的低级特性,如直接访问硬件的能力,又提供了高级语言的特性,如数据封装和代码重用。13 C++的应用领域非常广泛,包括但不限于教育、系统开发、游戏开发、嵌入式系统、工业和商业应用、科研和高性能计算等领域。在教育领域,C++因其结构化和面向对象的特性,常被选为计算机科学和工程专业的入门编程语言。在系统开发领域,C++因其高效性和灵活性,经常被作为开发语言。游戏开发领域中,C++由于其高效性和广泛应用,在开发高性能游戏和游戏引擎中扮演着重要角色。在嵌入式系统领域,C++的高效和灵活性使其成为理想选择。此外,C++还广泛应用于桌面应用、Web浏览器、操作系统、编译器、媒体应用程序、数据库引擎、医疗工程和机器人等领域。16 学习C++的关键是理解其核心概念和编程风格,而不是过于深入技术细节。C++支持多种编程风格,每种风格都能有效地保证运行时间效率和空间效率。因此,无论是初学者还是经验丰富的程序员,都可以通过C++来设计和实现新系统或维护旧系统。3

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值