springboot中使用rabbitmq的几种模式

简单模式

设置启动类

//消息的简单模式
@SpringBootApplication
public class Main1 {
    public static void main(String[] args) {
        SpringApplication.run(Main1.class,args);
    }
    @Bean
    public Queue helloworld(){
        return new Queue("helloworld",false,false,false);
    }
    @Autowired
    private Producer p;

    /**spring的执行流程
     * 自动扫描创建实例 --> 完成依赖注入--> @PostConstruct-->后续步骤*/
    @PostConstruct
    public void test(){
        //在新的线程中执行自己的运算,不阻塞spring主线程执行
        new Thread(()->{
            try {
                Thread.sleep(3000L);//等待helloworld队列创建
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            p.send();
        }).start();
    }
}

生产者

@Component
public class Producer {
    //发送消息的封装工具
    @Autowired
    private AmqpTemplate t;

    public void send(){
        //队列的参数在启动类中设置
        t.convertAndSend("helloworld","helloworld");
    }

}

消费者

//@RabbitListener(queues = "helloworld")//也可以注解到方法上,就不需要RabbitHandler
@Component
public class Consumer {
    @RabbitListener(queues = "helloworld")
    public void receive(String msg){
        System.out.println("收到"+msg);
    }

}

工作模式

启动类

//消息的工作模式
@SpringBootApplication
public class Main2 {
    public static void main(String[] args) {
        SpringApplication.run(Main2.class,args);
    }

    @Bean
    public Queue TaskQueue(){
        return new Queue("task_queue",true,false,false);
    }
    @Autowired
    private Producer p;

    /**spring的执行流程
     * 自动扫描创建实例 --> 完成依赖注入--> @PostConstruct-->后续步骤*/
    @PostConstruct
    public void test(){
        //在新的线程中执行自己的运算,不阻塞spring主线程执行
        new Thread(() -> {
            while (true){
            System.out.println("输入消息:");
            String s = new Scanner(System.in).nextLine();
            p.send(s);}
        }).start();

    }
}

生产者

@Component
public class Producer {
    //发送消息的封装工具
    @Autowired
    private AmqpTemplate t;

    public void send(String s){
        //队列的参数在启动类中设置
        t.convertAndSend("task_queue",s);
        /**
         * t.convertAndSend("task_queue",s,消息预处理对象);
         * 在预处理对象中,可以对消息的参数进行调整
         *
         * */
    }
}

消费者

@Component
public class Consumer {
    @RabbitListener(queues = "task_queue")
    public void receive1(String msg){
        System.out.println("消费者1收到"+msg);
    }
    @RabbitListener(queues = "task_queue")
    public void receive2(String msg){
        System.out.println("消费者2收到"+msg);
    }

}

在这里插入图片描述

发布和订阅模式

启动类中配置

  @Bean
    public FanoutExchange logs(){
        return new FanoutExchange("logs",false,false);
    }

生产者

@Component
public class Producer {
    //发送消息的封装工具
    @Autowired
    private AmqpTemplate t;

    public void send(String s){
        //队列的参数在启动类中设置
        t.convertAndSend("logs","",s);//第二个参数(路由键)可以不写,没用
    }

}

消费者

@Component
public class Consumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//队列,默认,随机命名,false,true,true
            //declare="false"表示不创建,使用已存在的交换机
            exchange = @Exchange(value = "logs",declare = "false")//交换机
    ))
    public void receive1(String msg){
        System.out.println("消费者1收到"+msg);
    }
    
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//队列,默认,随机命名,false,true,true
            exchange = @Exchange(value = "logs",declare = "false")//交换机
    ))
    public void receive2(String msg){
        System.out.println("消费者2收到"+msg);
    }
}

在这里插入图片描述

路由模式

启动类

@Bean
    public DirectExchange logs(){
        return new DirectExchange("direct_logs",false,false);
    }

生产者

@Component
public class Producer {
    //发送消息的封装工具
    @Autowired
    private AmqpTemplate t;

    public void send(String k, String s){
        //队列的参数在启动类中设置
        t.convertAndSend("direct_logs",k,s);
    }
}

消费者
多加个key键

@Component
public class Consumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//队列,默认,随机命名,false,true,true
            //declare="false"表示不创建,使用已存在的交换机
            exchange = @Exchange(value = "direct_logs",declare = "false"),//交换机
            key = {"error"}
    ))
    public void receive1(String msg){
        System.out.println("消费者1收到"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//队列,默认,随机命名,false,true,true
            exchange = @Exchange(value = "direct_logs",declare = "false"),//交换机
            key = {"info","error","warning"}
    ))
    public void receive2(String msg){
        System.out.println("消费者2收到"+msg);
    }
}

在这里插入图片描述

主题模式

启动类修改

@Bean
    public TopicExchange logs(){
        return new TopicExchange("topic_logs",false,false);
    }

生产者修改

public void send(String k, String s){
        //队列的参数在启动类中设置
        t.convertAndSend("topic_logs",k,s);
    }

消费者修改

@Component
public class Consumer {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//队列,默认,随机命名,false,true,true
            //declare="false"表示不创建,使用已存在的交换机
            exchange = @Exchange(value = "topic_logs",declare = "false"),//交换机
            key = {"*.orange.*"}
    ))
    public void receive1(String msg){
        System.out.println("消费者1收到"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//队列,默认,随机命名,false,true,true
            exchange = @Exchange(value = "topic_logs",declare = "false"),//交换机
            key = {"*.*.rabbit","lazy.#"}
    ))
    public void receive2(String msg){
        System.out.println("消费者2收到"+msg);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值