-
RabbitAdmin主要用于在Java代码中对理队和队列进行管理,用于创建、绑定、删除队列与交换机,发送消息等。
-
在applicationContext.xml中配置RabbitAdmin
<!-- RabbitAdmin用于创建、绑定、管理队列与交换机 --> <rabbit:admin id="rabbitAdmin" connection-factory="connectionFactory"/>
-
在代码中引入RabbitAdmin
@Resource(name = "rabbitAdmin") private RabbitAdmin rabbitAdmin;
-
RabbitAdmin创建交换机
//创建交换机,无则创建有则跳过 rabbitAdmin.declareExchange(new FanoutExchange("test.exchange.fanout", true, false)); rabbitAdmin.declareExchange(new DirectExchange("test.exchange.direct", true, false)); rabbitAdmin.declareExchange(new TopicExchange("test.exchange.topic", true, false));
-
RabbitAdmin创建队列
//创建队列 rabbitAdmin.declareQueue(new Queue("test.queue"));
-
RabbitAdmin绑定交换机与队列
//绑定队列 rabbitAdmin.declareBinding(new Binding("test.queue", Binding.DestinationType.QUEUE, "test.exchange.topic", "#", new HashMap<String, Object>()));
-
RabbitAdmin发送消息
@Resource private RabbitTemplate rabbitTemplate; //发送消息 rabbitTemplate.convertAndSend("test.exchange.topic", "hello", "abc123");
-
RabbitAdmin删除队列
rabbitAdmin.deleteQueue("test.queue");
-
RabbitAdmin删除交换机
rabbitAdmin.deleteExchange("test.exchange.fanout"); rabbitAdmin.deleteExchange("test.exchange.direct"); rabbitAdmin.deleteExchange("test.exchange.topic");
-
代码示例 RabbitAdmin使用代码 中的RabbitAdminTestor