spring boot集成rabbitMQ:系列三

6 篇文章 0 订阅
5 篇文章 0 订阅

目录

 

RabbitMq实操Topic方式

项目准备和配置相关见系列二

一、RabbitMq的config代码实现:

二、生产者代码实现

三、消费者代码实现

四、测试代码实现


RabbitMq实操Topic方式

项目准备和配置相关见系列二

一、RabbitMq的config代码实现:

package org.maggiezhang.springbootdemo.common.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@Slf4j
public class RabbitConfigTopic {

    public final static String topic_message_A = "topic.topic_message_A";
    public final static String topic_message_AMany ="topic.topic_message_A.many";

    public final static String topic_exchange="topic_exchange";


    @Bean
    public Queue queueMessageTopicA(){
        return new Queue(RabbitConfigTopic.topic_message_A, true);
    }

    /*@Bean
    public Queue queueMessageAMany(){
        return new Queue(RabbitConfigTopic.topic_message_AMany, true);
    }*/

    @Bean
    TopicExchange exchange(){
        return new TopicExchange(topic_exchange);
    }

    @Bean
    Binding bindingExchangeMessageA(Queue queueMessageTopicA, TopicExchange exchange){
        return BindingBuilder.bind(queueMessageTopicA).to(exchange).with("topic.topic_message_A");
    }

    /**
     * 只要是topic开头的message都会绑到AMany的queue中(会收到更多的message)
     * @param queueMessageTopicAMany
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessageAMany(Queue queueMessageTopicAMany, TopicExchange exchange){
        return BindingBuilder.bind(queueMessageTopicAMany).to(exchange).with("topic.#");
    }


}

二、生产者代码实现

package org.maggiezhang.springbootdemo.common.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class MsgProducerTopic {

    @Autowired
    public RabbitTemplate rabbitTemplate;

    public void send1(String msg){
        //String context = "这是从topic_message_A发出来的:"+ new Date();
        log.info(msg);
        rabbitTemplate.convertAndSend(RabbitConfigTopic.topic_exchange, RabbitConfigTopic.topic_message_A, msg);
    }

    public void send2(String msg){
        //String context = "这是从topic_message_AMany发出来的:"+ new Date();
        log.info(msg);
        rabbitTemplate.convertAndSend(RabbitConfigTopic.topic_exchange, RabbitConfigTopic.topic_message_AMany, msg);
    }
}

三、消费者代码实现

package org.maggiezhang.springbootdemo.common.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = RabbitConfigTopic.topic_message_A)
@Slf4j
public class MsgReceiverTopicQueueA {
    @RabbitHandler
    public void process(String message) {
        log.info("接收队列A的信息:" + message);
    }


}

四、测试代码实现

package org.maggiezhang.springbootdemo.common.mq;

import lombok.extern.slf4j.Slf4j;
import org.maggiezhang.springbootdemo.CaseBase;
import org.maggiezhang.springbootdemo.common.mq.MsgProducerTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;

import java.util.Date;

@Slf4j
public class TestMsgProducerTopic extends CaseBase{
    @Autowired
    MsgProducerTopic msgProducerTopic;

    @Test
    public void testSendMsgTopicA(){
        msgProducerTopic.send1("这是从topic_message_A发出来的:"+ new Date());
        log.info("test: send message A successfully");

    }

    @Test
    public void testSendMsgTopicAMany(){
        msgProducerTopic.send2("这是从topic_message_AMany发出来的:"+ new Date());
        log.info("test: send message Many successfully");

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值