RabbitMQ usage scenario exercise: Validated User ID, Length Limit (12)

  • Validated User ID
The userid is specified when sending a message. Only the username of the current conn can send the message (the test found that the username is not the current conn, and the queue has not been created or created, probably because it is in the same channel). Used when the consumer needs to know which user the message is sent from

package com.demo.mq.rabbitmq.example12;
import java.io.IOException;
import java.io.Serializable;
import java.util.concurrent.TimeoutException;
import org.apache.commons.lang3.SerializationUtils;
import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BlockedListener;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
 * Verify User ID
 * @author sheungxin
 *
 */
public class ValidUserSend {
	private static String userId="sheungxin";
	
	/**
	 * Check userId when sending message
	 * @param month
	 * @throws IOException
	 * @throws TimeoutException
	 */
	public static void validUserSend(Serializable mes) throws IOException, TimeoutException{
		Connection conn=MqManager.newConnection();
		//Link blocking listener, in addition to ShutdownListener (not the focus of this use case, briefly)
		conn.addBlockedListener(new BlockedListener() {
			
			@Override
			public void handleUnblocked() throws IOException {
				// connection is now unblocked
			}
			
			@Override
			public void handleBlocked(String reason) throws IOException {
				// connection is now blocked
			}
		});

		Channel channel=conn.createChannel();
		String queueName=channel.queueDeclare().getQueue();
		//Declare that the consumer is used to receive messages (you can also receive messages after basicPublish, maybe because they are on the same channel)
		channel.basicConsume(queueName, true, new DefaultConsumer(channel){
			@Override
		    public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{
				System.out.println(SerializationUtils.deserialize(body));
			}
		});
		//Specify userid when sending a message, only the user name of the current conn can send the message (the test found that the user name is not the current conn, the queue has not been created, maybe because it is in the same channel)
		Builder properties=new BasicProperties.Builder();
		properties.userId(userId);
		channel.basicPublish("", queueName, properties.build(), SerializationUtils.serialize(mes));
	}
	
	public static void main(String[] args) throws IOException, TimeoutException {
		validUserSend("Hello World!");
	}

}

  • Queue Length Limit
x-max-length: refers to the maximum number of messages that can be stored in the queue, not the maximum length of a single message in the queue
max-length-bytes: refers to the maximum byte length of the message content in the queue
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-max-length", 10);
//args.put("x-max-length-bytes",10000);
channel.queueDeclare("myqueue", false, false, false, args);

Can be set by server policy
rabbitmqctl set_policy Ten "^one-meg$" '{"max-length-bytes":1000000}' --apply-to queues

Guess you like

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