美文网首页
RabbitMQ-Tutorials-Two

RabbitMQ-Tutorials-Two

作者: Vanes丶 | 来源:发表于2018-11-08 15:41 被阅读0次

    Publish/Subscribe

    Introduction

    In this part we'll do something completely different -- we'll deliver a message to multiple consumers. This pattern is known as "publish/subscribe".
    To illustrate the pattern, we're going to build a simple logging system. It will consist of two programs -- the first will emit log messages and the second will receive and print them. And there will be many receivers to receive the messages.

    Full Message Model in Rabbit

    Previous model
    • A producer is a user application that sends messages.
    • A queue is a buffer that stores messages.
    • A consumer is a user application that receives messages.
    image
    Problems existed:
    • The core idea in the messaging model in RabbitMQ is that the producer never sends any messages directly to a queue. Actually, quite often the producer doesn't even know if a message will be delivered to any queue at all.
    • There may be a lot of queues. And one producer may send msg to many queues. Duplicate code is bad practice.
    New Model
    image
    • Exchange : An exchange is a very simple thing. On one side it receives messages from producers and the other side it pushes them to queues. The exchange must know exactly what to do with a message it receives.
      • Direct : Same routing key.(Same string)
      • Topic : Same string pattern of routing key.(eg. *.*.#, lazy.orange.rabbit)
      • Headers : Ignore the routing key and match header attributes.
      • Fanout : Ignore routing key.(Broadcast to all queues)
    Example of Fanout Exchange
    • Publisher
    package tech.shunzi.mq.demo.publish;
    
    import com.rabbitmq.client.BuiltinExchangeType;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    public class EmitLog {
    
        private static final String EXCHANGE_NAME = "logs";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            // Actively declare a non-autodelete, non-durable exchange with no extra arguments
            // Declare an FANOUT exchange with name "logs"
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
    
            String message = getMessage(argv);
    
            // Publishing to a non-existent exchange named "log"
            channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
    
            channel.close();
            connection.close();
        }
    
        private static String getMessage(String[] strings) {
            if (strings.length < 1) {
                return "info: Hello World!";
            }
            return joinStrings(strings, " ");
        }
    
        private static String joinStrings(String[] strings, String delimiter) {
            int length = strings.length;
            if (length == 0) {
                return "";
            }
            StringBuilder words = new StringBuilder(strings[0]);
            for (int i = 1; i < length; i++) {
                words.append(delimiter).append(strings[i]);
            }
            return words.toString();
        }
    }
    
    • Consumer
    package tech.shunzi.mq.demo.publish;
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    
    public class ReceiveLogs {
    
        private static final String EXCHANGE_NAME = "logs";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            // Actively declare a non-auto-delete, non-durable exchange with no extra arguments
            // Declare an FANOUT exchange with name "logs"
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
    
            // Create a non-durable, exclusive, auto-delete queue with a generated name
            String queueName = channel.queueDeclare().getQueue();
            
            // Bind the queues to the exchange, maintain the binding relationship between queues and exchange.
            // The FANOUT exchange will ignore the routing-key value.
            channel.queueBind(queueName, EXCHANGE_NAME, "");
    
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            Consumer consumer = new DefaultConsumer(channel) {
                @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(" [x] Received '" + message + "'");
                }
            };
            channel.basicConsume(queueName, true, consumer);
        }
    }
    
    Example of Direct Exchange
    • We will use a direct exchange instead. The routing algorithm behind a direct exchange is simple - a message goes to the queues whose binding key exactly matches the routing key of the message.
    image
    • If the routing keys are the same as other queues, it will behave like FANOUT exchange.

    • Producer

    package tech.shunzi.mq.demo.exchange.direct;
    
    import com.rabbitmq.client.BuiltinExchangeType;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    public class EmitLogDirect {
    
        private static final String EXCHANGE_NAME = "direct_logs";
    
        public static void main(String[] argv) throws Exception {
    
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            // Actively declare a non-auto-delete, non-durable exchange with no extra arguments
            // Declare an Direct exchange with name "direct_logs"
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
    
            // Declare routing key name
            String severity = getSeverity(argv);
            String message = getMessage(argv);
    
            channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
    
            channel.close();
            connection.close();
        }
    
        private static String getSeverity(String[] strings) {
            if (strings.length < 1) {
                return "info";
            }
            return strings[0];
        }
    
        private static String getMessage(String[] strings) {
            if (strings.length < 2) {
                return "Hello World!";
            }
            return joinStrings(strings, " ", 1);
        }
    
        private static String joinStrings(String[] strings, String delimiter, int startIndex) {
            int length = strings.length;
            if (length == 0) {
                return "";
            }
            if (length < startIndex) {
                return "";
            }
            StringBuilder words = new StringBuilder(strings[startIndex]);
            for (int i = startIndex + 1; i < length; i++) {
                words.append(delimiter).append(strings[i]);
            }
            return words.toString();
        }
    }
    
    • Consumer
    package tech.shunzi.mq.demo.exchange.direct;
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    
    public class ReceiveLogsDirect {
    
        private static final String EXCHANGE_NAME = "direct_logs";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            // Actively declare a non-auto-delete, non-durable exchange with no extra arguments
            // Declare an Direct exchange with name "direct_logs"
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
    
            // Create a non-durable, exclusive, auto-delete queue with a generated name
            String queueName = channel.queueDeclare().getQueue();
    
            if (argv.length < 1){
                System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
                System.exit(1);
            }
    
            for(String severity : argv){
                // Bind the queues to the exchange
                // Maintain the binding relationship between queues and exchange with given routing key name.
                channel.queueBind(queueName, EXCHANGE_NAME, severity);
            }
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            Consumer consumer = new DefaultConsumer(channel) {
                @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(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
                }
            };
            channel.basicConsume(queueName, true, consumer);
        }
    }
    
    Example of Topic Exchange
    • * (star) can substitute for exactly one word.
    • # (hash) can substitute for zero or more words.
    image
    • Producer
    package tech.shunzi.mq.demo.exchange.topic;
    
    import com.rabbitmq.client.BuiltinExchangeType;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    public class EmitLogTopic {
    
        private static final String EXCHANGE_NAME = "topic_logs";
    
        public static void main(String[] argv) {
            Connection connection = null;
            Channel channel = null;
            try {
                ConnectionFactory factory = new ConnectionFactory();
                factory.setHost("localhost");
    
                connection = factory.newConnection();
                channel = connection.createChannel();
    
                channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
    
                String routingKey = getRouting(argv);
                String message = getMessage(argv);
    
                channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (Exception ignore) {
                    }
                }
            }
        }
    
        private static String getRouting(String[] strings) {
            if (strings.length < 1) {
                return "anonymous.info";
            }
            return strings[0];
        }
    
        private static String getMessage(String[] strings) {
            if (strings.length < 2) {
                return "Hello World!";
            }
            return joinStrings(strings, " ", 1);
        }
    
        private static String joinStrings(String[] strings, String delimiter, int startIndex) {
            int length = strings.length;
            if (length == 0) {
                return "";
            }
            if (length < startIndex) {
                return "";
            }
            StringBuilder words = new StringBuilder(strings[startIndex]);
            for (int i = startIndex + 1; i < length; i++) {
                words.append(delimiter).append(strings[i]);
            }
            return words.toString();
        }
    }
    
    • Consumer
    package tech.shunzi.mq.demo.exchange.topic;
    
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    
    public class ReceiveLogsTopic {
    
        private static final String EXCHANGE_NAME = "topic_logs";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
            String queueName = channel.queueDeclare().getQueue();
    
            if (argv.length < 1) {
                System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
                System.exit(1);
            }
    
            for (String bindingKey : argv) {
                channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
            }
    
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            Consumer consumer = new DefaultConsumer(channel) {
                @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(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
                }
            };
            channel.basicConsume(queueName, true, consumer);
        }
    }
    
    Relationship between Different Exchange
    • When a queue is bound with "#" (hash) binding key - it will receive all the messages, regardless of the routing key - like in fanout exchange.
    • When special characters "*" (star) and "#" (hash) aren't used in bindings, the topic exchange will behave just like a direct one.

    Header Exchange

    RPC system with Rabbot MQ

    相关文章

      网友评论

          本文标题:RabbitMQ-Tutorials-Two

          本文链接:https://www.haomeiwen.com/subject/rjuwxqtx.html