一、概念和模型
二、例子:消费者1只接收routing-key为error的信息,消费者接收routing-key为error和warning的信息
public class RabbitmqUntil {
//获取连接
public static Connection getRabbitmqConnection() throws Exception{
//定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost("127.0.0.1");
//设置端口(这里的端口号指定是AMQP协议所用的端口号)
factory.setPort(5672);
//设置数据库
factory.setVirtualHost("/test");
//设置用户名
factory.setUsername("test");
//设置密码
factory.setPassword("test");
return factory.newConnection();
}
}
public class RoutingSender {
//路由名称
public static final String ROUTING_NAME="routing mode";
public static void sendMessage()throws Exception{
//1、获取连接
Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
//2、创建通道
Channel channel = rabbitmqConnection.createChannel();
//3、创建路由(参数1:路由名称;参数2:模式名称,direct表示路由模式,路由模式发布消息需要绑定routing-key)
channel.exchangeDeclare(ROUTING_NAME,"direct");
//4、设置每次发送信息不超过1条
channel.basicQos(1);
String message="hello ,I am error";
String routingKey ="error";
//5、发布消息(这里需要绑定routingKey)
channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
message="hello ,I am waring";
routingKey="waring";
channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
}
public static void main(String[] args)throws Exception {
RoutingSender.sendMessage();
}
}
注:channel.exchangeDeclare(ROUTING_NAME,"direct"),direct表示路由模式,发布是需要绑定routing-key,channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
public class RoutingReceiver1 {
//路由名称
public static final String ROUTING_NAME="routing mode";
//队列名称
public static final String ROUTING_QUEUE="routing queue1";
public static void receiveMessage() throws Exception{
//创建连接
Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
//获取通道
final Channel channel = rabbitmqConnection.createChannel();
//申明队列
channel.queueDeclare(ROUTING_QUEUE,true,false,false,null);
//申明每次处理一条数据
channel.basicQos(1);
//绑定路由(并且指定routing-key为error、warning)
channel.queueBind(ROUTING_QUEUE,ROUTING_NAME,"error");
channel.queueBind(ROUTING_QUEUE,ROUTING_NAME,"warning");
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try{
System.out.println("路由模式消费者1:"+new String(body,"utf-8"));
}catch (Exception e){
e.printStackTrace();
}finally {
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
channel.basicConsume(ROUTING_QUEUE,false,consumer);
}
public static void main(String[] args) throws Exception{
RoutingReceiver1.receiveMessage();
}
}
注:绑定多个routing-key,只要对队列进行多次绑定即可channel.queueBind()
public class RoutingReceiver2 {
//路由名称
public static final String ROUTING_NAME="routing mode";
//队列名称
public static final String ROUTING_QUEUE="routing queue2";
public static void receiveMessage() throws Exception{
//创建连接
Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
//获取通道
final Channel channel = rabbitmqConnection.createChannel();
//申明队列
channel.queueDeclare(ROUTING_QUEUE,true,false,false,null);
//申明每次处理一条数据
channel.basicQos(1);
//绑定路由(并且指定routing-key为error)
channel.queueBind(ROUTING_QUEUE,ROUTING_NAME,"error");
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try{
System.out.println("路由模式消费者2:"+new String(body,"utf-8"));
}catch (Exception e){
e.printStackTrace();
}finally {
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
channel.basicConsume(ROUTING_QUEUE,false,consumer);
}
public static void main(String[] args) throws Exception{
RoutingReceiver2.receiveMessage();
}
}