The last chapter talked about rabbitmq installation
This chapter explains the use of rabbitmq
First start rabbitmq
rabbitmq-server -detached
New java springboot project where I used directly spring init
first step:
pom dependent file introduced
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.8.0</version> </dependency>
Step 2: Create consumer and producer end
Consumer end:
public class Consumer0 {
/**
* 创建交换机
*
* 创建队列
*
* 将队列与交换机绑定
*
* 定义自己的消费方法
*
*进行监听
* @param args
* @throws IOException
* @throws TimeoutException
*/
public static void main(String[] args) throws IOException, TimeoutException {
//队列的名字
String queueName = "stu01queue";
//交换机的名字
String exchangeName = "stu01exchange";
//1:创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPort(5672);
connectionFactory.setHost("192.168.106.70");
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
//2:获取channel
Channel channel = connection.createChannel();
/**
* 3:定义exchange
* 参数分别为:
* exchange名字
* exchange类型
* 是否持久化
* 自动删除?
* 自定义参数
*/
channel.exchangeDeclare(exchangeName, "topic", true, true, null);
/**
* 4:
* 定义queue
*
* 参数分别为
* 名字
* 是否持久化
* 是否顺序消费 只有我自己可以消费
* 是否自动删除
* 参数 拓展参数
*/
channel.queueDeclare(queueName, true, false, false, null);
/**
* 5:
* 将队列绑定到exchange
*/
channel.queueBind(queueName, exchangeName, "stu0");
//6:进行消费
channel.basicConsume(queueName,true,new MyConsumer0(channel));
}
}
The production side:
public class Produce0 {
public static String exchangeName = "stu01exchange";
/**
* 将消息发送到对应的交换机
*
* 发送的时候指定routingkey
*
* 这样 当消费者监听的队列绑定的是这个routingkey 就可以接受到消息
*
*
* @param args
* @throws IOException
* @throws TimeoutException
*/
public static void main(String[] args) throws IOException, TimeoutException {
//工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.106.70");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//链接
Connection connection = connectionFactory.newConnection();
//获取channel
Channel channel = connection.createChannel();
//创建要发送的数据
String[] strs = {"迪丽热巴", "古力娜扎"};
//将数组数据循环发送
Arrays.stream(strs).forEach(e -> {
try {
channel.basicPublish(exchangeName, "stu0", null, e.getBytes());
} catch (IOException ex) {
ex.printStackTrace();
}
});
String sendMsg = "Hello Rabbit";
channel.basicPublish(exchangeName, "stu0", null, sendMsg.getBytes());
channel.close();
connection.close();
}
}
Custom message reception method:
public class MyConsumer0 extends DefaultConsumer {
/**
* Constructs a new instance and records its association to the passed-in channel.
*
* @param channel the channel to which this consumer is attached
*/
public MyConsumer0(Channel channel) {
super(channel);
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("-----------consume message----------");
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
System.out.println("body: " + new String(body));
}
}
Such a simple topic to complete the type of demo
Start consumer -> startup producer
You can see in the console has been successful
Here explain some values in the above code:
durable: whether persistence is whether to save data
autoDelete: whether to automatically delete when deleted automatically if the current exchange after which it is bound queue is deleted?
argument: Custom parameters to be sent
There is a type of exchange is topic
In fact, there are direct and fanout
Mode, the topic, and a binding operation code
Binding of routingKey topic can be a vague, such as binding
test. # mean test. (*). (*) is routed to a plurality of values
test. * Representative test. (*) is routed to a value
If the mode is the direct mode is only routingKey just direct correspondence can be routed
If the mode is fanout is similar to a broadcast mode all listeners will be routed to the exchange
In fact, not difficult to see from the above step is a three-step
1: Define exchange
2 is defined queue
3 queue and exchange bind
The code is actually a queue monitor consumer side
Routingkey production and to transmit data to a terminal exchange and the specified time of transmission
For example routingkey sent is testRoutingKey
When the queue and exchange relations binding the consumer side of the listener happens to be the routingkey is routed
These are simple demo subsequent use my free will springboot in a simple way a detailed explanation