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

由于本人对消息机制和rabbitmq相关知识有过系统地学习和使用经验,

所以这里直接上代码,小白先去看《rabbitmq in action》


请先学习http://my.oschina.net/dyyweb/blog/667858   direct模式


消费者1


package com.dy.topic;

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 TopicConsumer_1 {
    public String host ="127.0.0.1";


    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);

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

        channel.queueDeclare(Constants.queue_topic_1, false, false, false, null);

        //绑定队列,交换器,路由键(交换器根据路由规则把消息放入匹配的对队列)
        channel.queueBind(Constants.queue_topic_1,Constants.topic_exchange, "*");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(Constants.queue_topic_1, false,consumer);

        while (true) {
            try {
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                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());
            }
        }
        //        channel.close();
//        connection.close();

    }

    public static void main(String[] args) {
        try {
            new TopicConsumer_1().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
消费者2


package com.dy.topic;

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 TopicConsumer_2 {

    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("127.0.0.1");

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

        channel.queueDeclare(Constants.queue_topic_2, false, false, false, null);

        //绑定队列,交换器,路由键(交换器根据路由规则把消息放入匹配的对队列)
        channel.queueBind(Constants.queue_topic_2,Constants.topic_exchange, "topic#");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(Constants.queue_topic_2, false, consumer);

        while (1==1) {
            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) {
               e.printStackTrace();
            }
        }

//        channel.close();
//        connection.close();
    }


    public static void main(String[] args) {
        try {
            new TopicConsumer_2().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 





消费者3

package com.dy.topic;

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 TopicConsumer_3 {

    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");

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

        channel.queueDeclare(Constants.queue_topic_3, false, false, false, null);

        //绑定队列,交换器,路由键(交换器根据路由规则把消息放入匹配的对队列)
        channel.queueBind(Constants.queue_topic_3,Constants.topic_exchange, "#");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(Constants.queue_topic_3, 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) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        try {
            new TopicConsumer_3().init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 





消息生产者

package com.dy.topic;

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

/**
 *
 */
public class TopicProducer {
    public Connection connection;
    public Channel channel;

    public void init() throws Exception{
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");

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

        channel.exchangeDeclare(Constants.topic_exchange, "topic");

    }
    public void publisch(String msg) throws Exception{
        this.channel.basicPublish(Constants.topic_exchange, "topic", null, msg.getBytes("UTF-8"));

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

    public static void main(String[] args) {
        try {
            TopicProducer producer = new TopicProducer();
            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();
        }
    }
} 

使用的变量

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";

}





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