Work queues

Posted by Wave on Wed, 22 Dec 2021 04:46:44 +0100

Work queue:

 

Work queue The main idea of (also known as Work Queues) is to avoid executing a resource intensive task immediately and having to wait for it to complete. Instead, we schedule the task to be executed later. We encapsulate the task as a message and send it to the queue. The work process running in the background will pop up the task and finally execute the job. When there are multiple worker threads, these worker threads will Handle these tasks together.

Rotation training distribution message:

In this case, we will start two worker threads, a message sending thread. Let's see how their two worker threads work

Extraction tool class:

public classRabbitMqUtil{
    //Get a connected channel
    public static Channel getChannel() throws Exception{
        //Create a connection factory
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.200.129");
        factory.setUsername("admin");
        factory.setPassword("123");
        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();
        return channel;
    }
}

Start two worker threads:

public class Task01{
    //Queue name
    public static final String QUEUE_NAME="hello";
    
    //Send a large number of messages
    public static void main(String[] args) throws Exception{
         /* Declaration of queue
           1,Queue name
           2,Whether the messages in the queue are persistent (disk). By default, the messages are stored in memory
           3,Whether the queue is only for consumption by one consumer or message sharing. true can be used by multiple consumers
               true: Multiple consumers can consume true: only one consumer can consume
           4,Whether to delete automatically. Whether to delete the queue automatically after the last consumer disconnects 
               true Auto delete, false, no auto delete
            5,Other parameters
        */  
        channel.queueDeclare(QUEUE_NAME,false,false,false,false,null);
        //Receive messages from the console
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            String message=scanner.next();
            /*
                Send a consumption
                1,To which switch
                2,What is the Key value of the route? This is the name of the queue
                3,Other parameter information
                4,The body of the message sent
            */
            channel.basicPublish("",QUEUE_NAME,null,message.getBytes());
            System.out.println("Send message complete:"+message);
        }
    }
}

Message response:

Concept:

It may take some time for a consumer to complete a task. What happens if one of the consumers handles a long task and hangs up after only completing part of it. Once RabbitMQ delivers a message to the consumer, it immediately marks the message for deletion. In this case, a consumer suddenly hangs up, and we will lose the message being processed. And subsequent messages sent to the consumer because it cannot be accepted.

Auto answer:

The message is considered to have been successfully transmitted immediately after it is sent. This mode needs to make a trade-off between high throughput and data transmission security, because in this mode, if the connection or channel is closed on the consumer's side before the message is received, the message will be lost. On the other hand, of course, this mode delivers overloaded messages on the consumer's side, There is no limit on the number of messages delivered. Of course, this may cause consumers to receive too many messages and have no time to process them, resulting in the squeeze of these messages, eventually running out of memory, and finally these consumer threads are killed by the operating system, Therefore, this pattern is only applicable when consumers can process these messages efficiently and at a certain rate.

Message response method:

A.Channel. Basicack (for positive confirmation)

RabbitMQ knows the message and processes it successfully. It can be discarded

B.Channel. Basicnack (for negative confirmation)

C.Channel. Basicnack (for negative confirmation)

And channel One less parameter than basicnack

If the message is rejected without processing, it can be discarded

Explanation of Multiplc:

The advantage of manual response is that it can respond in batches and reduce network congestion

channel.basicAck(deliveryTag,true);

true and false of multiple mean different things

true means to batch respond to the unresponsive messages on the channel. For example, if there are messages 5, 6, 7 and 8 transmitting the tag on the channel, and the current tag is 8, then the unresponsive messages of 5-8 will be confirmed to receive the message response

false compared with the above, only messages 5, 6 and 7 of tag-8 will be answered, and the message response will not be confirmed

 

Topics: Java RabbitMQ p2p