Publish/Subscribe Publish/Subscribe

Keywords: RabbitMQ Erlang

Function: Messages sent by a producer are retrieved by multiple consumers. One producer, one switch, multiple queues, multiple consumers

Producer: Messages can be sent to queues or switches.

Consumer: Only messages can be obtained from the queue.

If a message is sent to a switch that has no queue binding, the message will be lost.

Switches cannot store messages, messages are stored in queues

  1. X represents the internal component of rabbitMQ switch. The erlang message producer is the code completion. The code execution efficiency is not high. The message producer puts the message into the switch, the switch publishes and subscribes the message to all message queues, and the corresponding message queue consumers get the message for consumption.
  2. Relevant Scenarios: Mass Mail, Group Chat, Broadcasting (Advertising)

Producer realization idea:

Create Connection Factory, set service address 127.0.0.1, port number 5672, set user name, password, virtual host, get connection from connection factory, use connection to create channel, use channel to create queue, use channel to create switch and specify switch class. fanout, which uses channels to send messages to switches, closes channels and connections.

public class Send {

    private final static String EXCHANGE_NAME = "test_exchange_topic";

    public static void main(String[] argv) throws Exception {
        // Access connection and mq channel
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

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

        // Message content
        String message = "Hello World!";
        channel.basicPublish(EXCHANGE_NAME, "key.1", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

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

Consumer realization ideas:

Create Connection Factory, set service address 127.0.0.1, port number 5672, set user name, password, virtual host, get connection from connection factory, use connection to create channel, use channel to create queue, bind queue to switch, set Qos=1, create consumer And monitor the queue and return to completion manually. There can be multiple queues bound to switches and multiple consumers listening.

Recv: 
public class Recv {

    private final static String QUEUE_NAME = "test_queue_work";

    private final static String EXCHANGE_NAME = "test_exchange_fanout";

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

        // Access connection and mq channel
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // Declaration queue
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // Bind queues to switches
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");

        // At the same time, the server will send only one message to the consumer.
        channel.basicQos(1);

        // Define the consumers of the queue
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // Monitor the queue and return to completion manually
        channel.basicConsume(QUEUE_NAME, false, consumer);

        // Get the message
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
            Thread.sleep(10);

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

Recv2:

public class Recv2 {

    private final static String QUEUE_NAME = "test_queue_work2";

    private final static String EXCHANGE_NAME = "test_exchange_fanout";

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

        // Access connection and mq channel
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // Declaration queue
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // Bind queues to switches
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");

        // At the same time, the server will send only one message to the consumer.
        channel.basicQos(1);

        // Define the consumers of the queue
        QueueingConsumer consumer = new QueueingConsumer(channel);
        // Monitor the queue and return to completion manually
        channel.basicConsume(QUEUE_NAME, false, consumer);

        // Get the message
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
            Thread.sleep(10);

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

 

Posted by gazolinia on Fri, 30 Aug 2019 01:56:42 -0700