SpringBoot 注入RabbitMQ java初级使用

package com.cetcnav.config.rabbitmq;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: dell
 * @Date: 2024/5/8 13:21
 */
@Configuration
public class DirectRabbitConfig {

    //队列名:TestDirectQueue
    @Bean
    public Queue QXBDWarningMsgQueue() {
        // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
        // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("QXBD_WARNING_MSG_QUEUE", true);
    }

    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange QXBDWarningMsgExchange() {
        return new DirectExchange("QXBD_WARNING_MSG_QUEUE_EXCHANGE", true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(QXBDWarningMsgQueue()).to(QXBDWarningMsgExchange()).with("QXBD_WARNING_MSG_QUEUE_ROUTING");
    }
}


package com.cetcnav.rabbitmqListener;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cetcnav.cache.SkyMirrorDataCache;
import com.cetcnav.domain.DI.DIData;
import com.cetcnav.domain.skyMirrorData.SkyMirrorData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/**
 * @Description:
 * @Author: dell
 * @Date: 2024/5/8 9:24
 */
@Component
public class DirectReceiver {

    private static Logger log = LoggerFactory.getLogger(DirectReceiver.class);

    // 单条发送
    @Value("${skymirror.pushUrl}")
    private String PUSH_URL;
    // 批量发送
    @Value("${skymirror.pushBatchUrl}")
    private String PUSH_BATCH_URL;

    @RabbitListener(queues = "QXBD_WARNING_MSG_QUEUE")
    public void receiveMessage(String message) throws ParseException {
        log.info("queue.consumer 收到1条message:{}", message);
        try {
            // 处理RabbitMQ接收的状态告警信息
            dealMessage(message);
        } catch (Exception e) {
            log.error(ExceptionUtil.getMessage(e), e);
        }
    }


    /**
     * 处理RabbitMQ接收的状态告警信息
     */
    private void dealMessage(String message) {
        SkyMirrorData skyMirrorData = JSON.parseObject(message, SkyMirrorData.class);
        SkyMirrorDataCache.addSkyMirrorDataCache(skyMirrorData);
    }
}
package com.cetcnav.rabbitmqService;

import com.alibaba.fastjson.JSONObject;
import com.cetcnav.domain.skyMirrorData.SkyMirrorData;
import com.cetcnav.domain.skyMirrorData.SkyMirrorData_old;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: dell
 * @Date: 2024/5/8 13:52
 */
@Component
public class RabbitmqService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 推送消息
    public boolean sendDirectMsg(SkyMirrorData skyMirrorData) {
        rabbitTemplate.convertAndSend("QXBD_WARNING_MSG_QUEUE_EXCHANGE", "QXBD_WARNING_MSG_QUEUE_ROUTING",
                JSONObject.toJSONString(skyMirrorData));
        return true;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值