RabbitMQ usage scenario exercise: introductory example (1)

  • Points to Note

The same queue is created multiple times :
//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, false, false, false, null);

  • message sending class

package com.demo.mq.rabbitmq.example01;

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

import org.apache.commons.lang3.SerializationUtils;

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

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

	/**
	 * Single send, single receive scenarios, no special processing, used to receive messages
	 * @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();
		channel.queueDeclare(queue, false, false, false, null);
		channel.basicPublish("", queue, null, SerializationUtils.serialize(object));
		System.out.println("A Send :'"+object+"'");
		channel.close();
		conn.close();
	}
	
	public static void main(String[] args) throws Exception {
		String channel="hello";
//		sendAToB(channel, new String("Hello World!".getBytes(),"UTF-8"));
		UserBean user=new UserBean();
		user.setId("0001");
		user.setName("Test 001");
		sendAToB(channel, user);
	}
}


  • message receiving class

package com.demo.mq.rabbitmq.example01;

import java.io.IOException;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.demo.mq.rabbitmq.UserBean;
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 {
	
	/**
	 * Single send, single receive scenarios, no special processing, used to receive messages
	 * Note: At the same time, multiple receiving entities receive messages in sequence, and only one entity receives the same message
	 * @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, false, false, false, null);
		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);
				UserBean userBean=SerializationUtils.deserialize(body);
				System.out.println("B Received :'"+userBean.getId()+","+userBean.getName()+"'");
			}
		};
		channel.basicConsume(queue, true, consumer);
	}
	
	public static void main(String[] args) throws Exception {
		recvAToB("hello");
	}

}

Guess you like

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