1.使用直接交换机(Direct),消费在发送消息时绑定一个routingKey值,消费者在绑定通道的时候指定交换机和routingKey值,就可以实现对消息的路由。

消费者代码:

public class Consumer {
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        //创建连接和通道
       
ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        final Connection connection = factory.newConnection();
        ExecutorService service = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 4; i++) {
            final int cur = i;
            service.submit(new Runnable() {
                Channel channel = connection.createChannel();
                String queryname = channel.queueDeclare().getQueue();

                public void run() {
                    //创建队列消费者
                   
QueueingConsumer consumer = new QueueingConsumer(channel);
                    try {
                        //第四个线程绑定多个routkey
                       
if (cur == 3) {
                            channel.queueBind(queryname, "direct", "0");
                            channel.queueBind(queryname, "direct", "1");
                            channel.queueBind(queryname, "direct", "2");
                        } else {
                            //第三个参数是routingkey
                           
channel.queueBind(queryname, "direct", cur + "");
                        }
                        channel.basicConsume(queryname, consumer);
                        while (true) {
                            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                            String message = new String(delivery.getBody());
                            System.out.println("线程 " + cur + " 获取到消息 " + message);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        service.shutdown();
    }
}

生产者代码:

public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException {
        //配置rabbitmq服务器地址
       
ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("starktan");
        factory.setPassword("starktan");
        factory.setVirtualHost("/");
        //建立连接和通道
       
Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //声明一个直接交换机
       
channel.exchangeDeclare("direct", BuiltinExchangeType.DIRECT);
        System.out.println("发送信息!");
        String message = "WorkQueue Message number " ;
        //第二个参数是routingkey
       
channel.basicPublish("direct", "0", true, null, (message+"one").getBytes());
        channel.basicPublish("direct", "1", true, null, (message+"two").getBytes());
        channel.basicPublish("direct", "2", true, null,(message+"three").getBytes());
        channel.close();
        connection.close();
    }
}
posted on 2017-07-15 23:28  Stark_Tan  阅读(230)  评论(0编辑  收藏  举报