java-rabbitmq-官网实例05

来源:互联网 发布:已备案域名怎么办 编辑:程序博客网 时间:2024/05/16 12:38
java-rabbitmq-官网实例05


描述:
Direct 直连交换器的使用,接收方绑定多个路由键时,只有和发送方有相同路由键的消息才能被消费。


运行:
D5_EmitLogDirect.main();
D5_ReceiveLogsDirect.main();




package com.example.tutorials;import com.rabbitmq.client.BuiltinExchangeType;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import java.util.Scanner;/** * 接收方绑定多个路由键时,只有和发送方有相同路由键的消息才能被消费 * 发送路由键= error 的消息 * direct,直连交换器 * amqp-client 4.2.0 */public class D5_EmitLogDirect {    private static final String EXCHANGE_NAME = "direct_logs";    public static void main(String[] argv) throws Exception {        ConnectionFactory factory = new ConnectionFactory();        //设置登录账号        factory.setHost(ServerInfo.host);        factory.setPort(ServerInfo.port);        factory.setUsername(ServerInfo.uName);        factory.setPassword(ServerInfo.uPwd);        //链接服务器        Connection connection = factory.newConnection();        Channel channel = connection.createChannel();        //定义一个直连交换器        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);        //路由键        String routingKey = "error";        //发送消息        System.out.println("输入要发送的消息,退出输入 x ");        String message = "Run. Run. Or it will explode.";        do{            Scanner scanner = new Scanner(System.in);            message = scanner.next();            channel.basicPublish(EXCHANGE_NAME                    , routingKey                    , null                    , message.getBytes("UTF-8"));            System.out.println(" [x] Sent routingKey='" + routingKey + "',msg='" + message + "'");        }while(!"x".equals(message));        channel.close();        connection.close();    }}

package com.example.tutorials;import com.rabbitmq.client.*;import java.io.IOException;import java.util.concurrent.TimeoutException;/** * 接收方绑定多个路由键时,只有和发送方有相同路由键的消息才能被消费 * 只有绑定的路由键 = error 的接受者,才可以接收到消息 * direct,直连交换器 * @create 2017-08-29 * amqp-client 4.2.0 **/public class D5_ReceiveLogsDirect {    private static final String EXCHANGE_NAME = "direct_logs";    public static void main(String[] argv) throws Exception {        stratThread("consumer_01","info");//不能收到消息        stratThread("consumer_02","waring");//不能收到消息        stratThread("consumer_03","error");//可以收到消息        stratThread("consumer_04","error","debug");//可以收到消息    }    public static void stratThread(final String consumerName, final String ...routingKeys){        new Thread(new Runnable() {            @Override            public void run() {                try {                    startConsumer(consumerName,routingKeys);//启动消费者                } catch (IOException e) {                    e.printStackTrace();                } catch (TimeoutException e) {                    e.printStackTrace();                }            }        }).start();    }    public static void startConsumer(final String consumerName,String ...routingKeys) throws IOException, TimeoutException {        ConnectionFactory factory = new ConnectionFactory();        //设置登录账号        factory.setHost(ServerInfo.host);        factory.setPort(ServerInfo.port);        factory.setUsername(ServerInfo.uName);        factory.setPassword(ServerInfo.uPwd);        //链接服务器        Connection connection = factory.newConnection();        Channel channel = connection.createChannel();        //定义直连交换器        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);        //声明一个私有排他、自动应答的队列        String queueName = channel.queueDeclare().getQueue();        //为通道绑定一个或多个路由键        for(String routingKey : routingKeys){            channel.queueBind(queueName, EXCHANGE_NAME, routingKey);        }        System.out.println(" ["+consumerName+"] Waiting for messages. To exit press CTRL+C");        //定义一个消费者        Consumer consumer = new DefaultConsumer(channel) {            @Override            public void handleConsumeOk(String consumerTag) {                super.handleConsumeOk(consumerTag);                System.out.println(" ["+consumerName+"] 已经注册成功! ");            }            @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(" ["+consumerName+"] Received routingkey='" + envelope.getRoutingKey() + "',msg = '" + message + "'");            }        };        channel.basicConsume(queueName, true, consumer);    }}


原创粉丝点击