rabbitmq详解第四天(消息类型一direct 路由模式)

 producer发送消息的时候需要设置一个消息类型,然后consumer订阅的时候只订阅自己需要的消息类型即可



 一对一

配置
@Configuration
public class OneConfig {
    /* one2one */
    final static String ONE2ONE = "one2one";

    @Bean
    public Queue queueOne2One() {
        return new Queue(ONE2ONE);
    }
}

生产者 
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 一个生产者对应一个消费者:生产者
 */
@Component
public class OneProducer {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String sendMsg = "hello one2one " + new Date();
        System.out.println("one2one 生产者 sendMessage : " + sendMsg);
        this.rabbitTemplate.convertAndSend("one2one", sendMsg);
    }
}

消费者 
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 一个生产者对应一个消费者:消费者
 */
@Component
@RabbitListener(queues = "one2one")
public class OneCustomer {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("one2one 消息消费者消费消息  : " + hello);
    }
}

测试类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * 一个生产者 对应 一个消费者
 */
@RestController
public class One2OneController {
    @Autowired
    private OneProducer oneProducer;

    @RequestMapping("/one2one")
    public String one2OneSend(){
        oneProducer.send();
        return "ok";
    }

}

一对多

配置

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class One2manyConfig {

    /* one2many */
    final static String ONE2MANY =  "one2many";

    @Bean
    public Queue queueOne2Many(){
        return new Queue(ONE2MANY);
    }
}


生产者
 
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 一对多模式:单个消息生产者
 */
@Component
public class One2ManyProducer {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        String sendMsg = msg + new Date();
        System.out.println("one2many 生产者产生的消息 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("one2many", sendMsg);
    }
}

消费者

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 一对多模式:多个消费者
 */
@Component
@RabbitListener(queues = "one2many")
public class One2ManyCustomer1 {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("one2may 消费者1号  : " + msg);
    }
}

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 一对多模式:多个消费者
 */
@Component
@RabbitListener(queues = "one2many")
public class One2ManyCustomer2 {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("one2may 消费者2号  : " + msg);
    }
}


测试方法 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class One2manyController {

    @Autowired
    private One2ManyProducer one2ManyProducer;

    @RequestMapping("/one2many")
    public String one2Many() {
        for (int i = 0; i < 10; i++) {
            one2ManyProducer.send("one2many 消费者你好 ,----" + i);
        }
        return "ok";
    }
}

多对多

配置
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class Many2manyConfig {


    /* many2many */
    final static String MANY2MANY = "many2many";

    @Bean
    public Queue queueMany2Many(){
        return new Queue(MANY2MANY,true);
    }
}


生产者

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 一对多模式:单个消息生产者
 */
@Component
public class Mnay2ManyProducer1 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        String sendMsg = msg + new Date();
        System.out.println("one2many 一号生产者产生的消息 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("many2many", sendMsg);
    }
}

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 一对多模式:单个消息生产者
 */
@Component
public class Mnay2ManyProducer2 {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        String sendMsg = msg + new Date();
        System.out.println("one2many 二号生产者产生的消息 : " + sendMsg);
        this.rabbitTemplate.convertAndSend("many2many", sendMsg);
    }
}

消费者


import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 一对多模式:多个消费者
 */
@Component
@RabbitListener(queues = "many2many")
public class Mnay2ManyCustomer1 {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("many2many 消费者1号  : " + msg);
    }
}

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 一对多模式:多个消费者
 */
@Component
@RabbitListener(queues = "many2many")
public class Mnay2ManyCustomer2 {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("many2many 消费者2号  : " + msg);
    }
}


测试方法

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class Many2manyController {

    @Autowired
    private Mnay2ManyProducer1 mnay2ManyProducer1;
    @Autowired
    private Mnay2ManyProducer2 mnay2ManyProducer2;

    @RequestMapping("/many2many")
    public String many2many() {
        for (int i = 0; i < 10; i++) {
            mnay2ManyProducer1.send("many2many 消费者你好(一号生产者说) ,----" + i);
            mnay2ManyProducer2.send("many2many 消费者你好(二号生产者说) ,----" + i);
        }
        return "ok";
    }
}

发送对象

对象

public class User {
    private  int id;
    private  String name;
    private  String age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}


配置 
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObjConfig {


    final static String OBJ = "obj";

    @Bean
    public Queue queue(){
        return new Queue(OBJ);
    }

}


生产者

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 对象传输,生产者
 */
@Component
public class ObjProducter {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        User us = new User();
        us.setAge("测试");
        us.setName("aaa");
        us.setId(123);
        rabbitTemplate.convertAndSend("obj", us);
        System.out.println("发送对象数据: " );
    }
}

消费者

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 对象的传输,消费者
 */
@Component
@RabbitListener(queues = "obj")
public class ObjCustomer {

    @RabbitHandler
    public void process(User user) {
        System.out.println("消费者  : " + user.toString());
    }
}

测试方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * 发送对象
 */
@RestController
public class ObjController {


    @Autowired
    private ObjProducter objProducter;

    @RequestMapping("/sendobj")
    public String sendobj() {
        objProducter.send();
        return "ok";
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄泉路好走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值