RabbitMQ之业务场景:动态创建,删除队列工具类(二)

RabbitMqConfig配置类

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.ivg.framework.comp.rabbitmq.service.RabbitMqSend;

@Configuration
public class RabbitMqConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public RabbitMqSend rabbitMqSend() {
        return new RabbitMqSend(rabbitTemplate);
    }
}

工具类RabbitMqSend

import java.util.UUID;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RabbitMqSend implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    private RabbitTemplate rabbitTemplate;

    /************ Send_exchanges ***********************/
    private static final String topic_exchange = "fitting_invengo_exchange";
    private static final String cashierExchange = "cashier_invengo_exchange";

    public RabbitMqSend(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setConfirmCallback(this);// 最后设置内容
    }

    public RabbitAdmin createAdmin() {
        return new RabbitAdmin(this.rabbitTemplate.getConnectionFactory());
    }

    public void bindQueue(String queueName, String routingKey) {
        RabbitAdmin admin = createAdmin();
        Queue queue = new Queue(queueName);
        admin.declareQueue(queue);
        TopicExchange topicExchange = new TopicExchange(topic_exchange);
        admin.declareExchange(topicExchange);
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(routingKey);
        admin.declareBinding(binding);
        log.info("创建队列名:{},创建routingKey:{}", queueName, routingKey);
        // admin.setAutoStartup(true);
    }

    public boolean deleteQueue(String queueName) {
        RabbitAdmin admin = createAdmin();
        boolean result = admin.deleteQueue(queueName);
        if (result) {
            log.info("删除队列{}成功", queueName);
        } else {
            log.info("删除队列{}失败", queueName);
        }
        return result;
    }

    public void removeBinding(String queueName, String routingKey) {
        RabbitAdmin admin = createAdmin();
        Queue queue = new Queue(queueName);
        TopicExchange topicExchange = new TopicExchange(topic_exchange);
        Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(routingKey);
        admin.removeBinding(binding);
    }

    public void sendMsg(String routingKey, Object msg) {
        this.sendMsgTemplate(topic_exchange, routingKey, msg);
    }

    public void sendMsgTemplate(String exchange, String routingKey, Object msg) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, new CorrelationData(UUID.randomUUID().toString()));
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (correlationData == null) {
            return;
        }
        log.debug("send id:" + correlationData.getId());
        if (ack) {// 调用成功
            log.warn(correlationData.getId() + ":" + "发送成功.");
        } else {
            log.warn(correlationData.getId() + ":" + "发送失败.");
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.info("send message failed:" + "{message:" + message + ",replyCode:" + replyCode + ",replyText:" + replyText
                + ",exchange" + exchange + ",routingKey" + routingKey + "}");
    }

	public void sendCashierMsg(String routingKey,String msg) {
		sendMsgTemplate(cashierExchange, routingKey, msg);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值