RabbitMQ发送接收消息(队列式+主题式)(springboot)

RabbitMQ发送接收消息(队列式+主题式)(springboot)

maven依赖与配置


    org.springframework.boot
    spring-boot-starter-amqp

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

 

1.队列式

1.1创建队列 


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * 创建队列
 */
@SpringBootConfiguration
public class CreateQueue {
    
    @Bean
    public Queue que(){
        return new Queue("myque");
    }

      
    //exchange 和 bingding可以没有
    @Bean
    public DirectExchange exc(){
        return new DirectExchange("myexc");
    }
    @Bean
    public Binding bind(){
        return BindingBuilder.bind(que()).to(exc()).with("mykey");
    }
      
      
      //创建队列 myque2和auqe,测试接收消息并回馈
      //@Bean
      public Queue que2(){
          return new Queue("myque2");
      }
      //@Bean
      public Queue que3(){
          return new Queue("aque");
      }

}

1.2发送消息


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

/**
 * 发送消息
 */
@Component
public class Sender  {
    
    @Autowired
    RabbitTemplate rt;
    
    public void send(){
        
        /*
         * convertAndSend(String routingKey, final Object object)
         * 方法参数为routingKey,但是参数只有我queueName时才能成功
         */
        //!!!队列名称,消息内容
        rt.convertAndSend("myque", " hello World  myque"); //成功
        rt.convertAndSend("myexc", " hello World  myexc"); //失败
        rt.convertAndSend("mykey", " hello World  mykey"); // 失败
        
        rt.convertAndSend("myque2", " hello World 2"); 
    }


}

1.3接收消息


import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

/**
 * 接收消息
 */
@Component
public class Receiver {

    @RabbitListener(queues = "myque")
    public void rece(String message){
        
        System.out.println("接收:"+message);
        
    }
    
    


    /**
     * 接收消息并返回内容
     */
    //@RabbitListener(queues = "myque2")
    //@SendTo("aque") //返回信息 
    public String receAndReturn(String message){
        
        System.out.println("接收2:"+message);
        
        return "aaaaaa";
    }
    
    /**
     * 接收回馈的信息
     */
    @RabbitListener(queues = "aque")
    public void getBack(String message){
        System.out.println("返回的消息:"+message);
    }

    
}

2.主题式

2.1创建队列


/**
 * 路由与队列绑定关系枚举
 */
public enum STEQEnum {
    
    /** 订阅者 1 */
    CONSUMER1("topic_exchange","topic_queue1","tp.#"),
    /** 订阅者 2 */
    CONSUMER2("topic_exchange","topic_queue2","tp.a.*");
    
    
    /** 路由名称 */
    private String exchange;
    /** 队列 */
    private String queue;
    /** 路由键 */
    private String routingKey;

    STEQEnum(String exchange, String queue, String routingKey){
        this.exchange = exchange;
        this.queue = queue;
        this.routingKey = routingKey;
    }

    public String getExchange() {
        return exchange;
    }
    public void setExchange(String exchange) {
        this.exchange = exchange;
    }
    public String getQueue() {
        return queue;
    }
    public void setQueue(String queue) {
        this.queue = queue;
    }
    public String getRoutingKey() {
        return routingKey;
    }
    public void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }
    
    
}

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

/**
 * 创建路由、队列,并绑定
 */
@SpringBootConfiguration
public class TopicConfig {

    @Bean
    public TopicExchange exc(){
        return new TopicExchange(STEQEnum.CONSUMER1.getExchange());
    }
    
    @Bean
    public Queue queue1(){
        return new Queue(STEQEnum.CONSUMER1.getQueue());
    }

    @Bean
    public Queue queue2(){
        return new Queue(STEQEnum.CONSUMER2.getQueue());
    }
    
    @Bean
    public Binding bind1(){
        return  BindingBuilder.bind(queue1()).to(exc()).with(STEQEnum.CONSUMER1.getRoutingKey());
    }

    @Bean
    public Binding bind2(){
        return  BindingBuilder.bind(queue2()).to(exc()).with(STEQEnum.CONSUMER2.getRoutingKey());
    }
    
    
    
}

2.2 发布消息


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

/**
 * 发布消息
 */
@Component
public class Publisher {

    @Autowired
    RabbitTemplate rt;
    
    public void doPub(){
        
        String exchange = STEQEnum.CONSUMER1.getExchange();
         
        rt.convertAndSend(exchange, "tp.a.b", " 公众号消息 ");
        
    }
}

2.3 订阅者接收消息


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

/**
 * 订阅者
 */
@Component
public class Subscribe {
    
    //订阅者 1
    @RabbitListener(queues = "topic_queue1")
    public void  receiver( String message){
        System.out.println("1:"+message);
    }
    
    //订阅者 1
    @RabbitListener(queues = "topic_queue2")
    public void  receiver2( String message){
        System.out.println("2:"+message);
    }
    

}

 

你可能感兴趣的:(RabbitMQ)