Rabbitmq learning Chapter 3_ Quick start case, native operation rabbitmq, error reporting solution com rabbitmq. client. ShutdownSignalException: connection error

catalogue

  • Quick start cases

    1. Simple mode
    2. Working mode
    3. Publish and subscribe mode
      • fanout
      • direct
      • topic
  • Lift chestnut

    1. Producer code
    2. Some notes
    3. Consumer code
    4. Some notes
    5. Failed to create connection object under windows. Error reporting and solution

1, Quick start cases

View the tutorials on the official website RabbitMQ Tutorials: https://www.rabbitmq.com/getstarted.html There are four mq mode scenarios

1. Simple model: single consumer

2. Working mode: multiple consumers. The distribution modes include polling and fair distribution
  • Polling distribution: by default, RabbitMQ will send each message to the next consumer in order. On average, every consumer receives the same amount of information. This way of distributing messages is called a loop.
  • Fair distribution: distribution according to weight
  • Message confirmation: if the consumer suddenly fails after accepting the task, a message confirmation mechanism is required, and then mq the unprocessed information is put back into the queue for distribution
3. Publish and subscribe mode: multiple queues and multiple consumers
  • An exchange switch and binding are required. The exchange types are: direct, topic, headers and fanout
  • Direct, topic, headers and fan out are the relationships between switches and queues
  • binding is the relationship between the queue and the consumer. Only when the consumer binds to the queue can they receive information
1.fanout switching mode
  • fanout simply broadcasts all messages it receives to all queues it knows
  • Consumer binding specifies the queue to receive information

2.direct exchange mode
  • Based on the above, a function is added to it to make it possible to subscribe to only a subset of messages. A subscription publication is the relationship between a queue and a consumer.

  • The fan out exchange is no longer used. The direct exchange will be used. The routing algorithm behind direct exchange is very simple - the message goes to the queue whose binding key exactly matches the routing key of the message.

Setting the same binding key in the direct exchange mode has the same effect as fanout

3.topic exchange mode
  • It is equivalent to adding the direct exchange mode of fuzzy matching

4. Lifting chestnut
Producer code
package henu.soft.xiaosi.simple;

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

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    public static void main(String[] args) {

        // All middleware technologies build new protocol specifications based on TCP / ip protocol, but rabbitmq follows amqp protocol ip and port


        /**
         * Using rabbitmq natively
         */
        // 1. Create a connection factory

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
//        factory.setVirtualHost("/");

        Connection connection = null;
        Channel channel = null;

        try {
            // 2. Create a Connection

            connection = factory.newConnection("producer");


            // 3. Obtain the pipeline channel through connection

            channel = connection.createChannel();


            // 4. Create a switch, declare queue, binding relationship, routing key, send message and accept information

            String queueName = "xiaosi";

            /**
             * Parameters:
             * Queue name
             * Need persistence
             * exclusiveness
             * Delete automatically (delete queue after the last consumer finishes consuming)
             * Carry additional parameters
             */

            channel.queueDeclare(queueName,false,false,false,null);

            // 5. Prepare message content
            String msg = "RabbitMQ~~";


            // 6. Send message to queue

            channel.basicPublish("",queueName,null,msg.getBytes());

            System.out.println("Message sent successfully!");


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            try {
                // 7. Close the channel
                if(channel != null && channel.isOpen()){
                    channel.close();
                }
                // 8. Close the connection
                if(connection != null && channel.isOpen()){
                    connection.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }


        }


    }
}

Some notes

Consumer Cosumer code
package henu.soft.xiaosi.simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) {

        // All middleware technologies build new protocol specifications based on TCP / ip protocol, but rabbitmq follows amqp protocol ip and port


        /**
         * Using rabbitmq natively
         */
        // 1. Create a connection factory

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
//        factory.setVirtualHost("/");

        Connection connection = null;
        Channel channel = null;

        try {
            // 2. Create a Connection

            connection = factory.newConnection("consumer");


            // 3. Obtain the pipeline channel through connection

            channel = connection.createChannel();


            // 4. From which queue


            String queueName = "xiaosi";


            channel.basicConsume(
                    queueName,
                    true,
                    new DeliverCallback() {
                        public void handle(String consumerTag, Delivery message) throws IOException {
                            System.out.println("Consumer received message:" + new String(message.getBody(), "UTF-8"));
                        }

                    },
                    new CancelCallback() {
                        public void handle(String consumerTag) throws IOException {
                            System.out.println("Consumer failed to accept message!");

                        }
                    });


            System.out.println("Start accepting new messages!");
            System.in.read();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            try {
                // 7. Close the channel
                if (channel != null && channel.isOpen()) {
                    channel.close();
                }
                // 8. Close the connection
                if (connection != null && channel.isOpen()) {
                    connection.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }


        }


    }
}



Some notes


1. Get the connection object factory NewConnection ("producer"); report errors
  • When using java Native connection to rabbitmq under windows, the connection acquisition fails, and the output is: com rabbitmq. client. ShutdownSignalException: connection error; protocol meth

  • Solution: rabbitmqctl set_permissions -p "/" login username ". *" ". *" ". *"

Tags: Java queue Middleware RabbitMQ switch MQ

Posted by jber on Thu, 20 Jan 2022 01:18:54 +1030