1. 程式人生 > >RabbitMQ入門及常用的5種模式的簡單使用(二)

RabbitMQ入門及常用的5種模式的簡單使用(二)

RabbitMQ是一個非常常用也非常強大的訊息中介軟體,主要用於應用與應用之間的通訊,有五種常見的使用方式,分別是:簡單模式,工作模式,釋出訂閱模式,路由模式以及萬用字元模式,這裡主要是路由模式的三種具體實現!

釋出訂閱模式

package cn.itcast.rabbitmq.ps;


import cn.itcast.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


public class Send {


    private final static String EXCHANGE_NAME = "test_exchange_fanout";


    public static void main(String[] argv) throws Exception {
        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");


        // 訊息內容
        String message = "Hello World!";
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");


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

}

package cn.itcast.rabbitmq.ps;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv {


    private final static String QUEUE_NAME = "test_queue_ps01";


    private final static String EXCHANGE_NAME = "test_exchange_fanout";


    public static void main(String[] argv) throws Exception {


        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告佇列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 繫結佇列到交換機
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");


        // 同一時刻伺服器只會發一條訊息給消費者
        channel.basicQos(1);


        // 定義佇列的消費者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 監聽佇列,手動返回完成
        channel.basicConsume(QUEUE_NAME, false, consumer);


        // 獲取訊息
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" 消費者1: '" + message + "'");
            Thread.sleep(10);


            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }

}

package cn.itcast.rabbitmq.ps;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv2 {


    private final static String QUEUE_NAME = "test_queue_ps02";


    private final static String EXCHANGE_NAME = "test_exchange_fanout";


    public static void main(String[] argv) throws Exception {


        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告佇列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 繫結佇列到交換機
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");


        // 同一時刻伺服器只會發一條訊息給消費者
        channel.basicQos(1);


        // 定義佇列的消費者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 監聽佇列,手動返回完成
        channel.basicConsume(QUEUE_NAME, false, consumer);


        // 獲取訊息
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" 消費者2: '" + message + "'");
            Thread.sleep(10);


            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }

}

路由模式

package cn.itcast.rabbitmq.routing;


import cn.itcast.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


public class Send {


    private final static String EXCHANGE_NAME = "test_exchange_direct";


    public static void main(String[] argv) throws Exception {
        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");


        // 訊息內容
        String message = "使用者修改,id=10123";
        channel.basicPublish(EXCHANGE_NAME, "update", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");


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

}

package cn.itcast.rabbitmq.routing;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv {


    private final static String QUEUE_NAME = "test_queue_direct01";


    private final static String EXCHANGE_NAME = "test_exchange_direct";


    public static void main(String[] argv) throws Exception {


        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告佇列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 繫結佇列到交換機
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "update");
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
        // 同一時刻伺服器只會發一條訊息給消費者
        channel.basicQos(1);


        // 定義佇列的消費者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 監聽佇列,手動返回完成
        channel.basicConsume(QUEUE_NAME, false, consumer);


        // 獲取訊息
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println("前臺:'" + message + "'");
            Thread.sleep(10);


            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }

}

package cn.itcast.rabbitmq.routing;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv2 {


    private final static String QUEUE_NAME = "test_queue_direct02";


    private final static String EXCHANGE_NAME = "test_exchange_direct";


    public static void main(String[] argv) throws Exception {


        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告佇列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 繫結佇列到交換機
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "insert");
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "update");
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
        // 同一時刻伺服器只會發一條訊息給消費者
        channel.basicQos(1);


        // 定義佇列的消費者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 監聽佇列,手動返回完成
        channel.basicConsume(QUEUE_NAME, false, consumer);


        // 獲取訊息
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println("搜尋: '" + message + "'");
            Thread.sleep(10);


            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}

萬用字元模式

package cn.itcast.rabbitmq.topic;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


public class Send {


    private final static String EXCHANGE_NAME = "test_exchange_topic";


    public static void main(String[] argv) throws Exception {
        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");


        // 訊息內容
        String message = "使用者修改,id=155552";
        channel.basicPublish(EXCHANGE_NAME, "user.update", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");


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

}

package cn.itcast.rabbitmq.topic;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv {


    private final static String QUEUE_NAME = "test_queue_topic_01";


    private final static String EXCHANGE_NAME = "test_exchange_topic";


    public static void main(String[] argv) throws Exception {


        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告佇列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 繫結佇列到交換機
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "user.insert");
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "user.error");
        // 同一時刻伺服器只會發一條訊息給消費者
        channel.basicQos(1);


        // 定義佇列的消費者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 監聽佇列,手動返回完成
        channel.basicConsume(QUEUE_NAME, false, consumer);


        // 獲取訊息
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" 前臺: '" + message + "'");
            Thread.sleep(10);


            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }

}

package cn.itcast.rabbitmq.topic;


import cn.itcast.rabbitmq.util.ConnectionUtil;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


public class Recv2 {


    private final static String QUEUE_NAME = "test_queue_topic_02";


    private final static String EXCHANGE_NAME = "test_exchange_topic";


    public static void main(String[] argv) throws Exception {


        // 獲取到連線以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();


        // 宣告佇列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        // 繫結佇列到交換機
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "user.*");


        // 同一時刻伺服器只會發一條訊息給消費者
        channel.basicQos(1);


        // 定義佇列的消費者
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // 監聽佇列,手動返回完成
        channel.basicConsume(QUEUE_NAME, false, consumer);


        // 獲取訊息
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" 搜尋:'" + message + "'");
            Thread.sleep(10);


            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    }
}