SpringBoot集成RabbitMQ

1、导包
    <!--spirngboot集成rabbitmq-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
2、application.yml
server:
  port: 44000
spring:
  application:
    name: springboot-rabbitmq
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtualHost: / #以上配置在发送方
    listener:#配置在接收方
      simple:
        acknowledge-mode: manual #手动签收
3、RabbitMQ配置类

这个配置类可以不用写,可以在RabbitMq访问界面添加交换机、队列以及绑定。

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//rabbitmq配置类
@Configuration
public class RabbitmqConfig {
    //邮箱队列名
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    //短信队列名
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    //交换机名
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
    //创建交换机
    @Bean(EXCHANGE_TOPICS_INFORM)//Bean的名字
    public Exchange exchangeTopicInform() {
        //durable(true)持久化,消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }
    //声明队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue queueInformSms() {
        Queue queue = new Queue(QUEUE_INFORM_SMS,true);//持久化
        return queue;
    }

    //声明队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue queueInformEmail() {
        Queue queue = new Queue(QUEUE_INFORM_EMAIL,true);//持久化
        return queue;
    }
    //消费者把队列绑定交换机
    @Bean
    public Binding bindingQueueInformSms(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("sms.*").noargs();
    }
    @Bean
    public Binding bindingQueueInformEmail(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("email.*").noargs();
    }
}
4、主配置类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRabbitMq {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootRabbitMq .class,args);
    }
}
5、消息接收者
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import com.rabbitmq.client.Channel;
import java.io.IOException;

@Component
public class Rev {
    //监听email队列
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_EMAIL})
    public void receive_email(String msg, Message message, Channel channel) throws IOException {
        System.out.println("email"+msg);
        //触发手动签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //监听sms队列
    @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_SMS})
    public void receive_sms(String msg, Message message, Channel channel) throws IOException {
        System.out.println("sms"+msg);
        //触发手动签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
6、消息发送者
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootRabbitMq.class)
public class Send {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testSend(){
        String message = "test rabbitmq";
        for(int i=0;i<10;i++){
            if(i%2==0){
                rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM,"email.dfd",message);
            }else{
                rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM,"sms.dfd",message);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值