六、rabbitMQ Routing

来源:互联网 发布:海量 top k算法 编辑:程序博客网 时间:2024/06/05 12:46

在上一个实例中我们学习了发布/订阅模型。这种情况有时后可能不满足我们的要求。比如:在日志系统中我们要求error warn info 级别的日志打印在屏幕上,而error 几级别的日志还要打印在磁盘文件中。



我们在创建exchange的时候就不能再使用fanout 这种类型的exchange。而要使用  direct。   channel.exchangeDeclare(EXCHANGE_NAME, "direct");

在exchange和队列的绑定过程中 我们以前使用的是 channel.queueBind(queueName, EXCHANGE_NAME, "");  其实最后一位就是routing key,我们可以使用routing key 过滤exchange到队列的消息。如果exchange为fanout类型是会忽略该参数。


实例:

下面的实例上图不是完全相同,由于我不小心把 消费者2中的channel.queueBind(queueName, EXCHANGE_NAME, "error")给注释掉了。

消费者1、

package test1;import com.rabbitmq.client.*;import java.io.IOException;public class consumer {    private final static String EXCHANGE_NAME = "logs_exchange";    public static void main(String[] args) {        ConnectionFactory factory = new ConnectionFactory();        factory.setUsername("guest");        factory.setPassword("guest");        factory.setHost("127.0.0.1");        factory.setVirtualHost("/");        factory.setPort(5672);        try {            final Connection connection = factory.newConnection();            final Channel channel = connection.createChannel();            channel.exchangeDeclare(EXCHANGE_NAME, "direct");//不存在时,创建exchange            String queueName = channel.queueDeclare().getQueue();//创建队列            channel.queueBind(queueName, EXCHANGE_NAME, "error");//队列绑定到exchange//            channel.queueBind(queueName, EXCHANGE_NAME, "warn");//队列绑定到exchange//            channel.queueBind(queueName, EXCHANGE_NAME, "info");//队列绑定到exchange            Consumer consumer = new DefaultConsumer(channel) {                @Override                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)                        throws IOException {                    String message = new String(body, "UTF-8");                    System.out.println(message);                }            };            boolean autoAck = true;//自动返回设置为false            channel.basicConsume(queueName, autoAck, consumer);            Thread.sleep(600 * 1000);            channel.close();            connection.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

消费者2、

package test1;import com.rabbitmq.client.*;import java.io.IOException;public class consumer {    private final static String EXCHANGE_NAME = "logs_exchange";    public static void main(String[] args) {        ConnectionFactory factory = new ConnectionFactory();        factory.setUsername("guest");        factory.setPassword("guest");        factory.setHost("127.0.0.1");        factory.setVirtualHost("/");        factory.setPort(5672);        try {            final Connection connection = factory.newConnection();            final Channel channel = connection.createChannel();            channel.exchangeDeclare(EXCHANGE_NAME, "direct");//不存在时,创建exchange            String queueName = channel.queueDeclare().getQueue();//创建队列//            channel.queueBind(queueName, EXCHANGE_NAME, "error");//队列绑定到exchange            channel.queueBind(queueName, EXCHANGE_NAME, "warn");//队列绑定到exchange            channel.queueBind(queueName, EXCHANGE_NAME, "info");//队列绑定到exchange            Consumer consumer = new DefaultConsumer(channel) {                @Override                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)                        throws IOException {                    String message = new String(body, "UTF-8");                    System.out.println(message);                }            };            boolean autoAck = true;//自动返回设置为false            channel.basicConsume(queueName, autoAck, consumer);            Thread.sleep(600 * 1000);            channel.close();            connection.close();        } catch (Exception e) {            e.printStackTrace();        }    }}

生产者:

package test1;import com.rabbitmq.client.*;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class producter {    private final static String EXCHANGE_NAME = "logs_exchange";    public static void main(String[] args) {        ConnectionFactory factory = new ConnectionFactory();        factory.setUsername("guest");        factory.setPassword("guest");        factory.setHost("127.0.0.1");        factory.setVirtualHost("/");        factory.setPort(5672);        Map<String, String> infoGrade = new HashMap<String, String>();        infoGrade.put("error","this is error message");        infoGrade.put("warn", "this is warn message");        infoGrade.put("info", "this is info message");        try {            Connection connection = factory.newConnection();            Channel channel = connection.createChannel();            channel.exchangeDeclare(EXCHANGE_NAME, "direct");//不存在时,创建exchange            Iterator<Map.Entry<String, String>> entrySetIterator = infoGrade.entrySet().iterator();            while (entrySetIterator.hasNext()) {                Map.Entry<String, String> stringEntry = entrySetIterator.next();                channel.basicPublish(EXCHANGE_NAME, stringEntry.getKey(), null, stringEntry.getValue().getBytes());//消息发布到exchange            }            channel.close();            connection.close();        } catch (Exception e) {            e.printStackTrace();        }    }    }


运行时截图:






原创粉丝点击