1声明队列:
1.1通过创建一个Queue对象,
把它放到ioc容器中就自动创建了一个队列
注意包名:import org.springframework.amqp.core.Queue;
创建方法 :new Queue (队列的名字的)
注意队列的bean的名字,和队列名别搞错了
例子:
//创建一个名为simple.queue的队列
//注意它的bean名字是simpleQueue
@Bean
public Queue simpleQueue(){
return new Queue("simple.queue");
}
1.2通过注解
在消费者上写 @RabbitListener(queuesToDeclare=@Queue( 写上队列的名字 ));
产生两个效果
- 直接会声明(创建)这个队列,如果已存在,就不创建 ,不管这个消费这是否执行,即程序一启动就创建好这个队列
- 自动把消费者和这个队列绑定
例如:
@RabbitListener( queuesToDeclare={@Queue(name="simple2.queue")})
public void consumer3(String msg) throws InterruptedException {
System.out.println("消费者3收到消息 "+ msg);
Thread.sleep(10);
}
- 利用@RabbitListener(bindings=),可以同时把队列,交换机,以及他们的绑定关系确定,后面提到再说
2声明交换机:
2.1通过创建一个交换机对象来声明
交换机用类声明都是类似的
-
首先保证这个交换机是个bean对象,要加上@Bean注解
-
格式一般是:
@Bean public 写交换机类型 参数名(){ return new 交换机类型("交换机名字"); }
下面给三个例子:
fanout交换机:
@Bean
public FanoutExchange fanoutExchange1(){
return new FanoutExchange("fanout.exchange1");
}
direct交换机:
@Bean
public DirectExchange directExchange2(){
return new DirectExchange("direct.exchange2");
}
topic交换机:
@Bean
public Queue topicQueue1(){
return new Queue("topic.queue1");
}
2.2通过注解来声明:
利用@RabbitListener(bindings=),后面说
3如何声明绑定关系:
3.1通过创建binding类对象
各种交换机与对列的绑定 其实 都是类似的
如果一个交换机要绑定多个队列,就要创建多个binding类对象
对于fanout交换机例子:
@Bean
public FanoutExchange fanoutExchange1(){
return new FanoutExchange("fanout.exchange1");
}
@Bean
public Queue fanoutQueue(){
return new Queue("fanout.queue1");
}
// 绑定上面声明的队列和交换机
@Bean
public Binding binding(FanoutExchange fanoutExchange1,Queue fanoutQueue){
return BindingBuilder.bind(fanoutQueue).to(fanoutExchange1);
}
对于dirct交换机例子:
如果一个dirct交换机与一个队列 要设置多个routingkey,就要创建多个Binding对象
例子:
@Bean
public DirectExchange directExchange2(){
return new DirectExchange("direct.exchange2");
}
@Bean
public Queue dirctQueue2(){
return new Queue("direct.queue2");
}
// 绑定上面声明的队列和交换机,且设置了两个routingkey
@Bean
public Binding binding3a(DirectExchange directExchange2,Queue dirctQueue2){
return BindingBuilder.bind(dirctQueue2).to(directExchange2).with("red");
}
@Bean
public Binding binding3b(DirectExchange directExchange2,Queue dirctQueue2){
return BindingBuilder.bind(dirctQueue2).to(directExchange2).with("yellow");
}
对于topic交换机例子:
@Bean
public TopicExchange topicExchange1(){
return new TopicExchange("topic.exchange1");
}
@Bean
public Queue topicQueue1(){
return new Queue("topic.queue1");
}
@Bean
public Binding binding4(TopicExchange topicExchange1,Queue topicQueue1){
return BindingBuilder.bind(topicQueue1).to(topicExchange1).with("China.*");
}
3.2通过注解:
利用@RabbitListener 注解,一般把它放在消费者的那个方法上
用@RabbitListener注解的属性**bindings,**它是 QueueBinding [] 类型,可以声明多个交换机,队列,以及他们的绑定关系
像这样:
@RabbitListener(bindings={
//一个 @QueueBinding(....)就是声明一个队列,一个交换机,以及他们的绑定关系
@QueueBinding(....),....
})
//那么@QueueBinding怎么填写呢?
QeueuBinding注解填写
首先看它结构:
@Target({})
@Retention(RetentionPolicy.RUNTIME)
public @interface QueueBinding {
Queue value();
Exchange exchange();
String[] key() default {};
String ignoreDeclarationExceptions() default "false";
Argument[] arguments() default {};
String declare() default "true";
String[] admins() default {};
}
需要填写三个属性
Value: 类型是注解Queue,用于填写声明的一个队列 格式:Value=@Queue(“队列名”)
Exchange: 类型是注解Exchange ,用于填写声明的一个交换机,格式Exchange=@Exchange(name=“交换机名字”,type=“交换机种类”)
type可以用类ExchangeTypes的成员来指定交换机类型,例如ExchangeTypes.FANOUT就是指定FANOUT交换机
keys: 如果是fanout交换机就直接省略,其它的direct,topic交换机一定要写上
值得注意:
- 这里Value,Exchange可以用已存在的交换机和队列
- 会自动把消费者与 你在Value属性哪里声明的队列会自动绑定
下面给出交换机类型分别是fanout,direct,topic的完整例子:
fanout交换机例子:
创建并绑定fanout交换机fanout.exchange2和队列fanout.queue3,并绑定:
//注解声明交换机
@RabbitListener(bindings={
@QueueBinding(
value = @Queue("fanout.queue3"),
exchange = @Exchange(name="fanout.exchange2",type= ExchangeTypes.FANOUT)
//这里以为交换机类型是fanout所以不用写属性key的值
)
})
public void consumer7(String msg) throws InterruptedException {
System.out.println("消费者7收到消息 "+ msg);
Thread.sleep(50);
}
direct交换机例子:
创建并绑定dirct交换机direct.exchange1和队列direct.queue1,并绑定:
//注解声明direct交换机
@RabbitListener(bindings={
@QueueBinding(
value = @Queue("direct.queue1"),
exchange = @Exchange(name="direct.exchange1",type= ExchangeTypes.DIRECT),
// 这里直接可以声明多个routingkey
key={"blue","red"}
)
})
public void consumer8(String msg) throws InterruptedException {
System.out.println("消费者8收到消息 "+ msg);
Thread.sleep(50);
}
topic交换机例子:
声明队列dirct.queue1 和交换机topic.exchange2 并绑定
@RabbitListener(bindings={
@QueueBinding(
value = @Queue("direct.queue1"),
exchange = @Exchange(name="topic.exchange2",type= ExchangeTypes.TOPIC),
key="China.*"
)
})
public void consumer13(String msg) throws InterruptedException {
System.out.println("消费者13收到消息 "+ msg);
Thread.sleep(50);
}