前提是消息队列和exchanges创建好了,如果没有创建好,我们程序重要怎么去创建这些呢,我们就可以使用一个组件,
叫AmqpAdmin,因为我们要使用程序临时的创建一些,绑定规则,或者创建一些消息队列,我们就来采用它,他来帮我们
创建和删除,这些Queue,这些消息队列,Exchange交换器,包括这些绑定规则,我们就来测试类里面来测试一下,
AmqpAdmin,我们只需要一个Autowired,
@Autowired
AmqpAdmin amqpAdmin;
RabbitAutoConfiguration,这个类里面给我们注入的组件,
@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
@ConditionalOnMissingBean(AmqpAdmin.class)
public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
这是一个系统管理组件,用它来创建消息队列等等,我们先来创建一个Exchange,对amqpAdmin里面,他有一些方法,declare
里面是创建一些组件,相反的有一些remove和delete,是删除一些组件,我先来创建一个exchange,我们在这创建exchange的
时候呢,我们要传一个exchange对象
org.springframework.amqp.core.Exchange
public interface Exchange extends Declarable {
这个接口下边有很多的实现,有一个抽象的Exchange,我们要创建哪种类型的Exchange,是要Direct类型,还是Fanout,还是Topic,
还是Header,那我们就以direct为例,那我们就可以写一个DirectExchange,我们在创建这个Exchange的时候呢,我们点进来,
org.springframework.amqp.core.DirectExchange
我们可以传入一些规则,比如我们Exchange的名字,他是不是持久化的,是否删除的,我们默认传一个名字就行了
public class DirectExchange extends AbstractExchange {
public static final DirectExchange DEFAULT = new DirectExchange("");
public DirectExchange(String name) {
super(name);
}
public DirectExchange(String name, boolean durable, boolean autoDelete) {
super(name, durable, autoDelete);
}
public DirectExchange(String name, boolean durable,
boolean autoDelete, Map arguments) {
super(name, durable, autoDelete, arguments);
}
@Override
public final String getType() {
return ExchangeTypes.DIRECT;
}
}
我们用amqpadmin工具创建的一个exchange,名字就叫他,这个创建完了我们在控制台输出一下,创建完成,默认是没有我们这个的
amqpadmin.exchange
package com.learn.springboot;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.learn.bean.Book;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootAmqpApplicationTests {
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange() {
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
System.out.println("创建完成");
}
}
我们在这创建一下,运行成功我们就发现这里已经有了
其他的创建都一样,比如我创建一个Exchange,我来创建一个队列,这里有declareQueue,这个创建不传名字的情况下,
它是默认随机给一个名字,这个是Queue队列,他们是一个类不是一个接口,那我们就直接来new,new一个Queue,队列的
详细信息,我们点进来,可以只传队列的名字,和是否持久化,很多种构造器,我们就选用两个的,我们就来叫amqpadmin.queue,
org.springframework.amqp.core.Queue.Queue(String)
/**
* The queue is durable, non-exclusive and non auto-delete.
*
* @param name the name of the queue.
*/
public Queue(String name) {
this(name, true, false, false);
}
/**
* Construct a new queue, given a name and durability flag. The queue is non-exclusive and non auto-delete.
*
* @param name the name of the queue.
* @param durable true if we are declaring a durable queue (the queue will survive a server restart)
*/
public Queue(String name, boolean durable) {
this(name, durable, false, false, null);
}
我们的队列的名字就叫他,然后它是否是持久化的,我们写一个true,这是一个队列,我们来执行一下,看队列能不能创建爱你出来,
amqp队列没问题
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange() {
// amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
// System.out.println("创建完成");
amqpAdmin.declareQueue(new Queue("amqpadmin.queue"));
}
队列创建出来以后呢,讲exchange和队列绑定起来,创建绑定规则,绑定我们就可以使用了,declareBinding,
Binding我们来搜一下,new一个Binding,而New的时候传哪些参数
org.springframework.amqp.core.Binding
public Binding(String destination, DestinationType destinationType, String exchange, String routingKey,
Map arguments) {
this.destination = destination;
this.destinationType = destinationType;
this.exchange = exchange;
this.routingKey = routingKey;
this.arguments = arguments;
}
第一个destination目的地,第二个是目的地类型,这目的地类型有两种,你是要绑定一个消息队列,还是绑定一个交换器
public enum DestinationType {
QUEUE, EXCHANGE;
}
然后我们在这里要写交换器的名字,包括有一些参数头,我们就直接来写我们的绑定,第一个destination,目的地就发布到这,
类型绑定的是一个队列,我们来写上一个队列,还有一个信息,第三个信息,是我们exchange的类型,我们来给哪个exhange来绑这个
队列,Exchange就是我们前面创建的Exchange,我们再来看第四个数据,routingKey,我们的路由件,我们来写一个路由件,我们是
根据路由件,来发布给谁,路由件我们就叫amqp.haha,我们还要传入参数,传入一个map的参数,没有我就直接写一个null,这个我们就
声明了一个绑定,我先不运行,不运行前我先看一下Exchange,它里面默认是没有什么绑定规则的
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange() {
// amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
// System.out.println("创建完成");
// amqpAdmin.declareQueue(new Queue("amqpadmin.queue"));
amqpAdmin.declareBinding(new
Binding("amqpadmin.queue",Binding.DestinationType.QUEUE,"amqpadmin.exchange",
"amqp.haha",null));
}
我们发现绑定也是成功了,我在这刷新一下,我们发现绑定规则已经显示在这儿了
我们这个exchange绑定了amqpqueue,路由key叫他,我们可以在这里解绑,相当于我们用amqp就可以操作这些东西,
当然这是绑定,delete只要写名字,大家来一一尝试