image-20190810190723831

image-20190810190822672



配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@Configuration
public class RabbitMqConfig3 {

//队列one
@Bean
public Queue queueOne(){

return new Queue("queueOne");
}


//队列Two
@Bean
public Queue queueTwo(){

return new Queue("queueTwo");
}



//交换机。类型Direct(⭐️⭐️⭐️⭐️)
@Bean
public DirectExchange directExchange(){

return new DirectExchange("directExchange");
}

//绑定 队列ONE和交换机directExchange 使用路由key-one (⭐️⭐️⭐️⭐️⭐️⭐️⭐️)
@Bean
public Binding queueOneBinding(){

return BindingBuilder.bind(queueOne()).to(directExchange()).with("one");
}



//绑定 队列TWO 和交换机directExchange 使用路由key-two (⭐️⭐️⭐️⭐️⭐️⭐️⭐️)
@Bean
public Binding queueTwoBinding(){

return BindingBuilder.bind(queueTwo()).to(directExchange()).with("two");
}


//绑定 队列TWO 和交换机directExchange 使用路由key-thress(⭐️⭐️⭐️⭐️⭐️⭐️⭐️)
@Bean
public Binding queueThreeBinding(){

return BindingBuilder.bind(queueTwo()).to(directExchange()).with("three");
}
}

发送消息 - 生产者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

@Service
public class RabbitMqService3 {

@Autowired
RabbitTemplate rabbitTemplate;

public void send(String routingKey){

//指定交换机的名字
//指定路由key
//message
rabbitTemplate.convertAndSend("directExchange",routingKey,"value:" + routingKey);
}
}

接收消息 - 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

@Component
public class RabbitMqComponent3 {

// @RabbitListener 只指定队列名字
@RabbitListener(queues = "queueOne")
public void listerQueueOne(String message){
System.out.print("queueOne" + message);
}



@RabbitListener(queues = "queueTwo")
public void listerQueueTwo(String message){
System.out.print("queueTwo" + message);
}
}


image-20190810192847521

image-20190810192858783

image-20190810193431926