创建交换机、队列以及绑定关系

1、网页界面创建

2、AmqpAdmin创建

package com.itheima;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

@SpringBootTest
class Springboot08SsmpApplicationTests {

    @Autowired
    private AmqpAdmin amqpAdmin;
    @Test
    void contextLoads() {
        Queue fanoutSmsQueue = new Queue("9999", true, false, false);
        amqpAdmin.declareQueue(fanoutSmsQueue);
    }
}

3、创建bean,工程自动创建

package com.itheima.config;

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


/**
 * @author :Rk.
 * @date : 2022/10/8
 * 定义完成后 rabbitmq服务器会自动创建交换机和队列 以及绑定关系
 */
@Configuration
public class RabbitMQConfig {


    /**
     * 定义交换机名称
     */
    private String EXCHANGE_NAME="springboot_falout_exchange";

    /**
     * 短信队列
     */
    private String FANOUT_SMS_QUEUE = "fanout_sms_queuerk";

    /**
     * 邮件队列
     */
    private String FANOUT_EMAIL_QUEUE = "fanout_email_queuerk";


    /**
     * 定义 扇形交换机
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        //参数 交换机名称
        return new FanoutExchange(EXCHANGE_NAME);
    }

    /**
     *参数一:队列名称
     *参数二durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
     *参数三exclusive:默认也是false,是否独占队列
     *参数四autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
     */
    @Bean
    public Queue smsQueue(){
        return new Queue(FANOUT_SMS_QUEUE,true, false,false);
    }

    /**
     *参数一:队列名称
     *参数二:durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
     *参数三:exclusive:默认也是false,是否独占队列
     *参数四:autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
     */
    @Bean
    public Queue emailQueue(){
        return new Queue(FANOUT_EMAIL_QUEUE,true,false,false);
    }

    /**
     * 短信队列绑定交换机
     * @param smsQueue   短信队列注入到容器的id,也就是方法名 sumQueue
     * @param fanoutExchange   交换机注入到容器的id,也就是方法名 fanoutExchange
     * @return
     */
    @Bean
    public Binding bindingSmsFanoutExchange(Queue smsQueue,FanoutExchange fanoutExchange){
        return  BindingBuilder.bind(smsQueue).to(fanoutExchange);
    }

    /**
     * 邮件队列队列绑定交换机
     * @param emailQueue   短信队列注入到容器的id,也就是方法名 emailQueue
     * @param fanoutExchange   交换机注入到容器的id,也就是方法名 fanoutExchange
     * @return
     */
    @Bean
    public Binding bindingEmailFanoutExchange(Queue emailQueue,FanoutExchange fanoutExchange){
        return  BindingBuilder.bind(emailQueue).to(fanoutExchange);
    }

}

4、@RabbitListener

加在方法上:

/**
 * 队列不存在时,需要创建一个队列,并且与exchange绑定
 */
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "topic.n1", durable = "false", autoDelete = "true"),
        exchange = @Exchange(value = "topic.e", type = ExchangeTypes.TOPIC),
        key = "r"))
public void consumerNoQueue(String data) {
    System.out.println("consumerNoQueue: " + data);
}

加在类上需要@RabbitHandler

package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Book;
import com.itheima.mapper.BookMapper;
import com.itheima.service.BookService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "topic.n1", durable = "true", autoDelete = "true"),
        exchange = @Exchange(value = "topic.e", type = ExchangeTypes.TOPIC),
        key = "r"))
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;
    @Override
    @RabbitHandler
    public Book queryById(Integer id) {
        Book book = bookMapper.selectById(id);
        return book;
    }
}

你可能感兴趣的:(消息中间件,rabbitmq)