Website address: www.rabbitmq.com/tutorials/t…
Before you
Work Queues and “Hello World!” Compared to the working mode, there are more consumers, with multiple consumers consuming messages in the same queue. Using a work queue can speed up the processing of tasks when they are too heavy or too many.
There are no longer lengthy comments in the following sample code, as the same code is commented in “Hello World!” One of the Seven modes of RabbitMQ. The dependencies and tools class rabbitutils.java rabbitconstant. Java can also be found in “Hello World!” One of the Seven modes of RabbitMQ.
Production of the message
package com.baiqi.rabbitmq.workqueue;
import com.baiqi.rabbitmq.utils.RabbitConstant;
import com.baiqi.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class OrderSystem {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = RabbitUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(RabbitConstant.QUEUE_SMS, false.false.false.null);
for(int i = 1 ; i <= 100 ; i++) {
String sms = "Passengers" + i + "Your ticket has been booked.";
channel.basicPublish("" , RabbitConstant.QUEUE_SMS , null , sms.getBytes());
}
System.out.println("Data sent successfully"); channel.close(); connection.close(); }}Copy the code
consumers
package com.baiqi.rabbitmq.workqueue;
import com.baiqi.rabbitmq.utils.RabbitConstant;
import com.baiqi.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
public class SMSSender1 {
public static void main(String[] args) throws IOException {
Connection connection = RabbitUtils.getConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(RabbitConstant.QUEUE_SMS, false.false.false.null);
// If basicQos (1) is not written, automatic MQ will send all requests equally to all consumers
//basicQos,MQ no longer sends multiple requests to the consumer at once, but after the consumer has processed a message (after confirmation), a new one is fetched from the queue
channel.basicQos(1);// take one from each
channel.basicConsume(RabbitConstant.QUEUE_SMS , false , new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String sms = new String(body);
System.out.println("SMSSender1- SMS sent successfully :" + sms);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag() , false); }}); }}Copy the code
The consumer code can be copied three times to start three processes, three consumers.
conclusion
In code terms, Work queues and simple queues (Hello World!) There is no difference between modes, multiple channel.basicQos(1); It just defines how consumers get their messages, and nothing else changes. So the nature of the work queue pattern is the clustering of consumers to consume messages quickly.
Note: the above sample code from Turing College, I test collation, and then transfer to share.