消费者C1的RoutingKey 规则按照info warn 两种RoutingKey匹配 绑定队列console
package com. esint. rabbitmq. work03 ;
import com. esint. rabbitmq. RabbitMQUtils ;
import com. rabbitmq. client. Channel ;
import com. rabbitmq. client. DeliverCallback ;
public class ReceiveLog01 {
public static final String EXCHANGE_NAME = "logs" ;
public static void main ( String [ ] args) throws Exception {
Channel channel = RabbitMQUtils . getChannel ( ) ;
channel. exchangeDeclare ( EXCHANGE_NAME , "fanout" ) ;
String queue = channel. queueDeclare ( ) . getQueue ( ) ;
channel. queueBind ( queue, EXCHANGE_NAME , "" ) ;
System . out. println ( "wait print message..." ) ;
DeliverCallback deliverCallback = ( var , var2) -> {
System . out. println ( "01:" + new String ( var2. getBody ( ) ) ) ;
} ;
channel. basicConsume ( queue, true , deliverCallback, var -> { } ) ;
}
}
消费者C2的RoutingKey 规则按照error RoutingKey匹配 绑定队列disk
package com. esint. rabbitmq. work03 ;
import com. esint. rabbitmq. RabbitMQUtils ;
import com. rabbitmq. client. Channel ;
import com. rabbitmq. client. DeliverCallback ;
public class ReceiveLog02 {
public static final String EXCHANGE_NAME = "logs" ;
public static void main ( String [ ] args) throws Exception {
Channel channel = RabbitMQUtils . getChannel ( ) ;
channel. exchangeDeclare ( EXCHANGE_NAME , "fanout" ) ;
String queue = channel. queueDeclare ( ) . getQueue ( ) ;
channel. queueBind ( queue, EXCHANGE_NAME , "" ) ;
System . out. println ( "wait print message..." ) ;
DeliverCallback deliverCallback = ( var , var2) -> {
System . out. println ( "02:" + new String ( var2. getBody ( ) , "UTF-8" ) ) ;
} ;
channel. basicConsume ( queue, true , deliverCallback, var -> { } ) ;
}
}
生产者发送 基于下面调试程序改变参数RoutingKey 切换info warn error 可以观察两个生产者的不同数据接收
package com. esint. rabbitmq. work03 ;
import com. esint. rabbitmq. RabbitMQUtils ;
import com. rabbitmq. client. Channel ;
import java. util. Scanner ;
public class EmitLog {
public static final String EXCHANGE_NAME = "logs" ;
public static void main ( String [ ] args) throws Exception {
Channel channel = RabbitMQUtils . getChannel ( ) ;
channel. exchangeDeclare ( EXCHANGE_NAME , "fanout" ) ;
Scanner scanner = new Scanner ( System . in) ;
while ( scanner. hasNext ( ) ) {
String mes = scanner. next ( ) ;
channel. basicPublish ( EXCHANGE_NAME , "" , null , mes. getBytes ( "UTF-8" ) ) ;
System . out. println ( "生产者发出消息:" + mes. toString ( ) ) ;
}
}
}