RabbitMQ之交换器direct

消费者Demo

ErrorReceiver.java

package com.example.rabbitmq.util;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 *
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "${mq.config.queue.error}",autoDelete = "true"),
        exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.DIRECT),
        key = "${mq.config.queue.error.routing.key}"
))
public class ErrorReceiver {
    @RabbitHandler
    public void rabbitmqListener(String msg){
        System.out.printf("接收............Error:" + msg);
    }
}

InfoReceiver.java

package com.example.rabbitmq.util;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 *
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "${mq.config.queue.info}",autoDelete = "true"),
        exchange = @Exchange(value = "${mq.config.exchange}",type = ExchangeTypes.DIRECT),
        key = "${mq.config.queue.info.routing.key}"
))
public class InfoReceiver {
    @RabbitHandler
    public void rabbitmqListener(String msg){
        System.out.printf("接收............Info:" + msg);
    }
}

application.properties

spring.application.name=springcloud-mq

spring.rabbitmq.host=10.250.***.**
spring.rabbitmq.port=5672
spring.rabbitmq.username=*****
spring.rabbitmq.password=*****


#交换器类型
mq.config.exchange=log.direct

#路由键名称
mq.config.queue.info.routing.key=log.info.routing.key

#队列名称
mq.config.queue.info=log.info

#路由键名称
mq.config.queue.error.routing.key=log.error.routing.key

#队列名称
mq.config.queue.error=log.error

生产者Demo

SendMsg.java

package com.example.rabbitmq.util;

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

@Component
public class SendMsg {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Value("${mq.config.exchange}")
    private String exchange;
    @Value("${mq.config.queue.info.routing.key}")
    private String routingKeyInfo;
    @Value("${mq.config.queue.error.routing.key}")
    private String routingKeyError;

    public void sendMsg(String msg) {
        amqpTemplate.convertAndSend(exchange,routingKeyError,msg);
    }
}

application.properties

spring.application.name=springcloud-mq

spring.rabbitmq.host=10.250.***.**
spring.rabbitmq.port=5672
spring.rabbitmq.username=******
spring.rabbitmq.password=*****

#交换器类型
mq.config.exchange=log.direct

#路由键名称
mq.config.queue.info.routing.key=log.info.routing.key

#路由键名称
mq.config.queue.error.routing.key=log.error.routing.key

RabbitmqApplicationTests.java

package com.example.rabbitmq;

import com.example.rabbitmq.util.SendMsg;
import org.junit.Test;
import org.junit.runner.RunWith;
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 = RabbitmqApplication.class)
public class RabbitmqApplicationTests {

    @Autowired
    SendMsg sendMsg;
    @Test
    public void contextLoads() throws InterruptedException {
        while (true){
            Thread.sleep(1000);
            sendMsg.sendMsg("hello world");
        }
    }

}

 

posted @ 2019-06-21 16:39  情似雨餘黏地絮  阅读(199)  评论(0编辑  收藏  举报