SpringBoot single project integrates RabbitMQ to send messages

  • Introduce dependencies
 <!--RabbitMQ依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
  • yml file configuration MQ related configuration
server:
  port: 80
#===mq start===
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: xxx
    password: xxxxxx
    addresses: 127.0.0.1
    listener:
      direct:
        acknowledge-mode: manual #手动签收
    template:
      receive-timeout: 30000
      reply-timeout: 30000
    virtual-host: /
#===mq end===
  • Prepare a constant class to store the constants used (also can be written in the configuration file to read... etc.)
package cn.wyj.mq;

/**
 * @author WangYejian
 * @description: RabbitMQConstans
 * @date: 2020/11/24 17:15
 */
public class RabbitMQConstant {
    
    
    /**短信队列*/
    public final static String QUEUE_SMS="queue_sms";
    /**邮件队列*/
    public final static String QUEUE_EMAIL="queue_email";
    /**系统消息队列*/
    public final static String QUEUE_SYS="queue_sys";

    /**通道*/
    public final static String EXCHANGE_TOPIC="exchange_topic";

    /**key*/
    public final static String KEY_SMS="send_sms";
    /**key*/
    public final static String KEY_EMAIL="send_email";
    /**key*/
    public final static String KEY_SYS="send_sys";
}

  • Create a configuration class, declare queues and switches to bind switches and queues
package cn.wyj.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author WangYejian
 * @description: RabbitMQConfig 声明队列和交换机  将交换机和队列绑定
 * @date: 2020/11/23 16:46
 */
@Configuration
public class RabbitMQConfig {
    
    


    /**
     * 定义交换机
     */
    @Bean(RabbitMQConstant.EXCHANGE_TOPIC)
    public Exchange exchange(){
    
    
        return ExchangeBuilder.topicExchange(RabbitMQConstant.EXCHANGE_TOPIC).durable(true).build();
    }

    /**
     * 定义短信队列
     */
    @Bean(RabbitMQConstant.QUEUE_SMS)
    public Queue queueSms(){
    
    
        return new Queue(RabbitMQConstant.QUEUE_SMS,true);
    }

    /**
     * 将短信队列绑定到交换机
     */
    @Bean
    public Binding bindingSms(@Qualifier(RabbitMQConstant.QUEUE_SMS) Queue smsQueue, @Qualifier(RabbitMQConstant.EXCHANGE_TOPIC) Exchange exchange){
    
    
        return BindingBuilder.bind(smsQueue).to(exchange).with(RabbitMQConstant.KEY_SMS).noargs();
    }

    /**
     * 定义邮箱队列
     */
    @Bean(RabbitMQConstant.QUEUE_EMAIL)
    public Queue queueEmail(){
    
    
        return new Queue(RabbitMQConstant.QUEUE_EMAIL,true);
    }

    /**
     * 将邮箱队列和交换机绑定
     */
    @Bean
    public Binding bindingEmail(@Qualifier(RabbitMQConstant.QUEUE_EMAIL) Queue emailQueue, @Qualifier(RabbitMQConstant.EXCHANGE_TOPIC) Exchange exchange){
    
    
        return BindingBuilder.bind(emailQueue).to(exchange).with(RabbitMQConstant.KEY_EMAIL).noargs();
    }

    /**
     * 定义系统通知队列
     */
    @Bean(RabbitMQConstant.QUEUE_SYS)
    public Queue queueSys(){
    
    
        return new Queue(RabbitMQConstant.QUEUE_SYS,true);
    }

    /**
     * 将系统队列和交换机绑定
     */
    @Bean
    public Binding bindingSys(@Qualifier(RabbitMQConstant.QUEUE_SYS) Queue sysQueue, @Qualifier(RabbitMQConstant.EXCHANGE_TOPIC) Exchange exchange){
    
    
        return BindingBuilder.bind(sysQueue).to(exchange).with(RabbitMQConstant.KEY_SYS).noargs();
    }
}

  • send messages
package cn.wyj.mq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WangYejian
 * @description: MQControoller
 * @date: 2020/11/24 17:41
 */
@RestController
public class MQControoller {
    
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 发送消息
     * @return
     */
    @GetMapping("/mqSend")
    public String send(){
    
    
        //邮件发送消息
        rabbitTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_TOPIC,RabbitMQConstant.KEY_EMAIL,"email");
        //短信发送消息
        rabbitTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_TOPIC,RabbitMQConstant.KEY_SMS,"sms");
        //系统发送消息
        rabbitTemplate.convertAndSend(RabbitMQConstant.EXCHANGE_TOPIC,RabbitMQConstant.KEY_SYS,"sys");
        return "发送成功";
    }
}

-Create a listener class to listen for messages, get messages and process them.

package cn.wyj.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

/**
 * @author WangYejian
 * @description: RabbitMQHandle 消息监听
 * @date: 2020/11/24 17:07
 */
@Configuration
public class RabbitMQHandle {
    
    
    /**
     *
     * @param msg 接受的消息
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMQConstant.QUEUE_SMS)
    public void smsMessage(String msg, Message message, Channel channel){
    
    
        System.out.println("发送短信:"+msg);
    }
    /**
     *
     * @param msg 接受的消息
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMQConstant.QUEUE_EMAIL)
    public void emailMessage(String msg, Message message, Channel channel){
    
    
        System.out.println("发送邮件:"+msg);
    }
    /**
     *
     * @param msg 接受的消息
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMQConstant.QUEUE_SYS)
    public void sysMessage(String msg, Message message, Channel channel){
    
    
        System.out.println("发送系统消息:"+msg);
    }


}

  • The above is the whole process. Once you do this, you can send messages.

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/110089427