rabbitmq之direct交换器以及rabbitmq java发布订阅 消息实现

由于本人系统地学习和使用过rabbitmq,所以直接上代码!

topic模式代码直接联系本人

public class Constants {


    public static String direct_exchange = "dy-exchange";
    public  static String queue_direct = "dy-queue";

    public static  String fanout_exchange = "dy-exchange-fanout";
    public static  String queue_fanout_1 = "dy-queue-fanout-1";
    public static  String queue_fanout_2 = "dy-queue-fanout-2";


    public static String topic_exchange = "dy-exchange-topic";
    public static String queue_topic_1 = "dy-queue-topic-1";
    public static String queue_topic_2 = "dy-queue-topic-2";
    public static String queue_topic_3 = "dy-queue-topic-3";

}
第一个消费者
package com.dy.direct;

import com.dy.Constants;
import com.rabbitmq.client.*;

/**
 * Created by dy on 16-4-28.
 */
public class DirectConsumer_1 {
    public String host ="127.0.0.1";

    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setUsername("rpc_user");
//        factory.setPassword("rpcme");
        factory.setHost(host);

        //获取链接
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        /**
         * 声明队列
         * durable是否持久化,exclusive是否私有,autoDelete当最后一个消费者取消订阅的时候,对列是否自动移出
         * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
         */
        channel.queueDeclare(Constants.queue_direct, false, false, false, null);

        //绑定队列,交换器,路由键(交换器根据路由规则把消息放入匹配的对队列)
        //不同消费者,绑定相同队列到相同交换器,这时候会均衡消费队列中的消息,不管路由规则
        //不同消费者,绑定不同队列到相同交换器,这时候(交换器根据路由规则把消息放入匹配的对队列)
        channel.queueBind(Constants.queue_direct+"2", Constants.direct_exchange, "test");

        /**
         * 订阅消息
         * autoAck是否消息订阅到队列就确认
         * basicConsume(String queue, boolean autoAck, String consumerTag, TopicConsumer_1 callback)
         * basicConsume(String queue, boolean autoAck, TopicConsumer_1 callback)
         */
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(Constants.queue_direct, false,consumer);

        while (true) {
            try {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
//                AMQP.BasicProperties props = delivery.getProperties();
                String msg = new String(delivery.getBody(), "UTF-8");

                System.out.println("我接收到的消息是:"+msg);
                System.out.println("消息的唯一ID:"+delivery.getEnvelope().getDeliveryTag());

                // 返回接收到消息的确认信息
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    }

    public static void main(String[] args) {
        try {
            new DirectConsumer_1().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
第二个消费者
package com.dy.direct;

import com.dy.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

/**
 * Created by dy on 16-4-28.
 */
public class DirectConsumer_2 {
    public String host ="127.0.0.1";


    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setUsername("rpc_user");
//        factory.setPassword("rpcme");
        factory.setHost(host);

        //获取链接
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        /**
         * 声明队列
         * durable是否持久化,exclusive是否私有,autoDelete当最后一个消费者取消订阅的时候,对列是否自动移出
         * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
         */
        channel.queueDeclare(Constants.queue_direct, false, false, false, null);

        //绑定队列,交换器,路由键(交换器根据路由规则把消息放入匹配的对队列)
        //不同消费者,绑定相同队列到相同交换器,这时候会均衡消费队列中的消息,不管路由规则
        //不同消费者,绑定不同队列到相同交换器,这时候(交换器根据路由规则把消息放入匹配的对队列)
        channel.queueBind(Constants.queue_direct, Constants.direct_exchange, "haha");

        /**
         * 订阅消息
         * autoAck是否自动确认,是否消息订阅到队列就确认
         * basicConsume(String queue, boolean autoAck, String consumerTag, TopicConsumer_1 callback)
         * basicConsume(String queue, boolean autoAck, TopicConsumer_1 callback)
         */
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(Constants.queue_direct, false, consumer);

        while (true) {
            try {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();

                String msg = new String(delivery.getBody(), "UTF-8");

                System.out.println("我接收到的消息是:"+msg);       // 返回接收到消息的确认信息

                // 显示确认
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    }


    public static void main(String[] args) {
        try {
            new DirectConsumer_2().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
消息提供者
package com.dy.direct;

import com.dy.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 *消费完,就没有了
 */
public class DirectProducer {
    public String host ="127.0.0.1";
    public Connection connection;
    public Channel channel;

    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setUsername("rpc_user");
//        factory.setPassword("rpcme");
        factory.setHost(host);

        //获取链接
        connection = factory.newConnection();
        //创建信道
        channel = connection.createChannel();


        /**
         * 声明交换器
         * 交换器类型主要有三种
         * direct:精准匹配路由键,fanout:广播匹配,topic模糊(多)匹配路由键(可以优先级)
         * exchangeDeclare(String exchange, String type)
         */
        channel.exchangeDeclare(Constants.direct_exchange, "direct");


//        /**
//         * 声明队列
//         * durable是否持久化,exclusive是否私有,autoDelete当最后一个消费者取消订阅的时候,对列是否自动移出
//         * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
//         */
//        channel.queueDeclare(queueName, false, false, false, null);
//
//        //绑定队列,交换器,路由键(交换器根据路由规则把消息放入匹配的对队列)
//        channel.queueBind(queueName, exchangeName, "routeKey");

    }
    public void publisch(String msg) throws Exception{
        //basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
        this.channel.basicPublish(Constants.direct_exchange, "test", null, msg.getBytes("UTF-8"));

    }
    public void close() throws Exception{
        channel.close();
        connection.close();
    }

    public static void main(String[] args) {
        try {
            DirectProducer producer = new DirectProducer();
            producer.init();
            String msg ="this is a msg from producer!我的序列是:";
            for (int i = 1;i<8;i++){
                producer.publisch(msg+i);
            }
            producer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




你可能感兴趣的:(rabbitmq消息队列,rabbitmq消息发布订阅,rabbitmq消息代码实例)