java 动态队列_RabbitMq之动态修改队列参数

本文介绍如何在Java中动态修改RabbitMQ队列参数,由于RabbitMQ不支持直接修改,故采取先删除再重建的方式实现。通过`DynamicDelete`类的`declearQueue`方法展示了如何删除并重新创建队列来达到修改参数的目的。
摘要由CSDN通过智能技术生成

RabbitMq不支持修改已经存在的队列和交换机参数,可以通过创建失败,捕获异常信息,然后删除原队列或交换机,然后重新创建,以达到修改参数的功能。

package com.sky.study.delete;

import java.io.IOException;

import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP;

import com.rabbitmq.client.AMQP.Queue.DeclareOk;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.Connection;

import com.sky.study.ConnectionUtil;

public class DynamicDelete {

private final static String queueName="ceshi_queue";

public static void main(String[] args) throws Exception {

Connection connection = ConnectionUtil.getConnection();

Channel channel = connection.createChannel();

DeclareOk ss = declearQueue(channel,queueName);

System.out.println(ss);

channel.close();

connection.close();

}

public static DeclareOk declearQueue(Channel channel, String queueName) throws Exception {

Connection connection1 = ConnectionUtil.getConnection();

Channel channel1 = connection1.createChannel();

try {

channel1.queueDeclare(queueName,true, false, false, null);

} catch (IOException e) {

try {

channel1.close();

} catch (Exception ex) {

}

try {

connection1.close();

} catch (Exception ex) {

}

connection1 = ConnectionUtil.getConnection();

channel1 = connection1.createChannel();

channel1.queueDelete(queueName);

try {

channel1.close();

} catch (Exception ex) {

}

try {

connection1.close();

} catch (Exception ex) {

}

} finally {

try {

channel1.close();

} catch (Exception ex) {

}

try {

connection1.close();

} catch (Exception ex) {

}

}

DeclareOk declareOk = channel.queueDeclare(queueName, true, false, false, null);

return declareOk;

}

public static AMQP.Exchange.DeclareOk declearExchange(Channel channel, String exchangeName, String exchangeMode) throws IOException, TimeoutException {

//exchangeDeclare 交换机名字,类型,是否持久化,不使用时是否自动删除,是否是内部的(不能被客户端使用),其他参数

Connection connection1 = ConnectionUtil.getConnection();

Channel channel1 = connection1.createChannel();

AMQP.Exchange.DeclareOk declareOk;

try {

channel1.exchangeDeclare(exchangeName, exchangeMode, true, false, false, null);

} catch (IOException e) {

try {

channel1.close();

} catch (Exception ex) {

}

try {

connection1.close();

} catch (Exception ex) {

}

connection1 = ConnectionUtil.getConnection();

channel1 = connection1.createChannel();

channel1.exchangeDelete(exchangeName);

try {

channel1.close();

} catch (Exception ex) {

}

try {

connection1.close();

} catch (Exception ex) {

}

} finally {

try {

channel1.close();

} catch (Exception ex) {

}

try {

connection1.close();

} catch (Exception ex) {

}

}

declareOk = channel.exchangeDeclare(exchangeName, exchangeMode,true, false, false, null);

return declareOk;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值