RabbitMQ usage scenario exercise: work queue (2)

  • work queue
     The advantage of the work queue is that multiple worker threads share the execution task queue and use the round-robin method to distribute tasks (simple understanding: assign tasks to worker processes in turn, assign 1..n tasks at a time, and distribute them equally).

  • Points to Note

Generation of worker threads, declaring multiple Consumers on the same queue :
channel.basicConsume(queue, false, consumer);

Information endurance :
//The second parameter durable is set to true to achieve message persistence
channel.queueDeclare(queue, true, false, false, null);
// MessageProperties.PERSISTENT_TEXT_PLAIN, message persistence
channel.basicPublish("", queue, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(object));

The maximum amount of tasks on the client side, to avoid the accumulation of worker thread tasks, you need to open the manual answer mode :
//The maximum amount of tasks that can be received at the same time  
channel.basicQos(2);

Answer manually to ensure that the task is successfully executed :
//Turn off the automatic response mechanism, which is enabled by default; at this time, you need to manually perform the response  
channel.basicConsume(queue, false, consumer);
//After each processing, manually reply once, otherwise the server thinks that the assigned tasks are not completed  
channel.basicAck(envelope.getDeliveryTag(), false);

  • message sending class

package com.demo.mq.rabbitmq.example02;

import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

/**
 * Send message class
 * @author sheungxin
 *
 */
public class Send{

	/**
	 * Send messages to achieve message persistence
	 * @param queue queue name
	 * @param object message body
	 * @throws IOException
	 */
	public static void sendAToB(String queue,Serializable object) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		//The second parameter durable is set to true to achieve message persistence
		channel.queueDeclare(queue, true, false, false, null);
		// MessageProperties.PERSISTENT_TEXT_PLAIN, message persistence
		channel.basicPublish("", queue, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(object));
		System.out.println("A Send :'"+object+"'");
		channel.close();
		conn.close();
	}
	
	public static void main(String[] args) throws Exception {
		String channel="task_queue";
		for(int i=0;i<10;i++){
			sendAToB(channel, "Hello World "+i+"!");
		}
	}
}

  • message receiving class

package com.demo.mq.rabbitmq.example02;

import java.io.IOException;
import java.util.Random;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
 * Receive message class
 * @author sheungxin
 *
 */
public class Recv {
	
	/**
	 * Used to receive messages, turn on the response mechanism
	 * @param queue
	 * @throws Exception
	 */
	public static void recvAToB(String queue) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		//declare the queue here in order to prevent the receiver from running first, and create a queue when the queue does not exist yet (the same queue will only be created once)
		channel.queueDeclare(queue, true, false, false, null);
		//The maximum amount of tasks that can be received at the same time
		channel.basicQos(2);
		Consumer consumer=new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{
				String mes=SerializationUtils.deserialize(body);
				System.out.println("B Received :'"+mes+"'...");
				try {
					Thread.sleep(new Random().nextInt(10)*1000);
				} catch (InterruptedException e) {
					e.printStackTrace ();
				}
				System.out.println("B Received :'"+mes+"' done");
				//After each processing, manually reply once, otherwise the server thinks that the assigned tasks are not completed
				channel.basicAck(envelope.getDeliveryTag(), false);
			}
		};
		//Turn off the automatic response mechanism, which is enabled by default; at this time, you need to manually perform the response
		channel.basicConsume(queue, false, consumer);
	}
	
	public static void main(String[] args) throws Exception {
		recvAToB("task_queue");
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326860156&siteId=291194637