demo代码在ssm-demo中
一、rabbitmq的几种工作模式
1、simple
三个对象:生产者、队列、消费者
代码:
Sender:
package com.my.test.rabbitmq.simple;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-18
*/
public class Send {
static final String QUEUE_NAME = "test_simple_queue";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//从连接中创建一个管道
Channel channel = connection.createChannel();
//创建队列声明
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String msg = "hello simple";
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
System.out.println();
channel.close();
connection.close();
}
}
Receiver:
package com.my.test.rabbitmq.simple;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-18
*/
public class Receive {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(Send.QUEUE_NAME, false, false, false, null);
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println(Send.QUEUE_NAME + "receive:" + msg);
}
};
channel.basicConsume(Send.QUEUE_NAME, true, consumer);
}
}
缺点:耦合性高,生产者一一对应消费者,如果我想有多个消费者消费队列中的消息,这就不行了;如果对列名变更,这时要把生产者和消费者同时变更
2、work queue 工作队列
- 模型:
-
为什么会出现工作队列
-
simple队列是一一对应的,而且实际开发中,生产者发送消息是毫不费力的,而消费者一般是要跟业务相结合的,消费者接收到消息之后就需要处理,可能会需要花费很多时间,这时候队列可能会积压很多消息
-
代码:
sender:
package com.my.test.rabbitmq.workqueue;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
public class Send {
protected final static String QUEUE_NAME = "test_work_queue";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for (int i = 0; i < 50; i++) {
String msg = "hello " + i;
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
TimeUnit.MICROSECONDS.sleep(i * 20);
}
channel.close();
connection.close();
}
}
receive1:
package com.my.test.rabbitmq.workqueue;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
public class Receive1 {
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(Send.QUEUE_NAME, false, false, false, null);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[1] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] receive msg :" + msg);
}
}
};
boolean autoAck = true;
channel.basicConsume(Send.QUEUE_NAME, autoAck, consumer1);
}
}
receive2:
package com.my.test.rabbitmq.workqueue;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
public class Receive2 {
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(Send.QUEUE_NAME, false, false, false, null);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[2] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
boolean autoAck = true;
channel.basicConsume(Send.QUEUE_NAME, autoAck, consumer1);
}
}
- 现象:
消费者1和消费者2处理的消息数是一样的,消费者1都是偶数 消费者2都是奇数,这种方式是轮询分发(round-robin)
不管谁忙或者谁清闲,都不会多给一个消息,任务消息总是轮询的
3、workqueue公平分发(fair dipatch)
-
必须关闭自动应答ack,改成手动
-
代码:
send:
package com.my.test.rabbitmq.workfairqueue;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Send {
final static String QUEUE_NAME = "test_work_queue";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
for (int i = 0; i < 50; i++) {
String msg = "hello " + i;
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
TimeUnit.MICROSECONDS.sleep(i * 20);
}
channel.close();
connection.close();
}
}
receive1:
package com.my.test.rabbitmq.workfairqueue;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
public class Receive1 {
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(Send.QUEUE_NAME, false, false, false, null);
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[1] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(Send.QUEUE_NAME, autoAck, consumer1);
}
}
receive2:
package com.my.test.rabbitmq.workfairqueue;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive2 {
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(Send.QUEUE_NAME, false, false, false, null);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[2] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(Send.QUEUE_NAME, autoAck, consumer1);
}
}
现象:
消费之2处理的消息比消费者1多,能者多劳
-
autoAck消息应答:
boolean autoAck = false;
channel.basicConsume(Send.QUEUE_NAME, autoAck, consumer1);①如果ack为true
自动确认模式,一旦rabbitmq把消息给到消费者,会自动从内存中吧消息删除
这种情况下,如果杀死正在执行的消费者,就会丢失正在处理的消息
②如果ack为false
手动确认模式,如果有一个消费者挂掉,就会交付给其他消费者,消费者执行成功后可以发送一个消息应答,告知mq已经消费完毕,可以删除消息了
消息应答默认是打开的,false
Message Acknowkedgment
-
消息的持久化
如果mq挂了,因为消息在内存中存储,我们的消息仍然会丢失
//声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments) throws IOException;
durable 就是是否持久化,生产端和消费端都要,声明好的队列不能修改,true就是持久化,这是队列持久化
channel.basicPublish(EXCHANGE_NAME, routingKey, MessageProperties.MINIMAL_PERSISTENT_BASIC//持久化消息 , msg.getBytes());
增加properties,这个properties 就是消费端 callback函数中的properties
delivery_mode = 2 持久化消息
4、订阅模式(publish/subscribe)
RocketMQ的设计上,是不考虑消息去重的问题,即不考虑消息是否会重复的消费的问题,而是将这个问题抛给业务端自己去处理幂等的问题。
- 模型:
work模式,一个生产者对应一个队列 一个队列对应多个消费者
订阅模式,一个生产者对应多个队列 一个队列对应一个消费者
生产者没有直接把消息发到队列中,而是发送到了交换机
- send:
package com.my.test.rabbitmq.publish_subscribe;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Send {
final static String EXCHANGE_NAME = "test_exchange_fanout";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");//分发
//发送消息
String msg = "hello ps";
channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
System.out.println("send :" + msg);
channel.close();
connection.close();
}
}
消息哪里去了?
丢失了,rabbitmq中只有队列有存储能力,因为这时候还没有队列绑定到这个交换机,所以消息就丢失了
- receive:
package com.my.test.rabbitmq.publish_subscribe;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive1 {
final static String QUEUE_NAME = "test_ps_fanout_queue1";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "");
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[1] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer1);
}
}
package com.my.test.rabbitmq.publish_subscribe;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive2 {
final static String QUEUE_NAME = "test_ps_fanout_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "");
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[2] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer1);
}
}
-
Exchange 交换机 转换器
①一方面是接收生产者的消息,另一方面是向队列推送消息
②匿名转发 exchange=""
basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
③fanout(不处理路由键)只要是跟exchange绑定的队列都可以接收到消息
//声明交换机 channel.exchangeDeclare(EXCHANGE_NAME, "fanout");//分发
④direct(处理路由键)
5、路由模式
send:
package com.my.test.rabbitmq.routing;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Send {
final static String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);//direct
//发送消息
String msg = "hello direct";
//routing key
String routingKey = "error";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("send :" + msg);
channel.close();
connection.close();
}
}
receive1:
package com.my.test.rabbitmq.routing;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive1 {
final static String QUEUE_NAME = "test_routing_queue1";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "error");
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[1] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer1);
}
}
receive2:
package com.my.test.rabbitmq.routing;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive2 {
final static String QUEUE_NAME = "test_routing_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "error");
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "info");
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "warning");
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[2] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer1);
}
}
6、topic主题模式
-
将路由和某个模式匹配
-
topic exchange
-
#:匹配一个或者多个
-
*:匹配一个
-
商品 Goods.# (Goods.fruit)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jjNS7GBY-1570518722699)(E:\anotes_and_information\一些后端服务\rabbitmq\Snipaste_2019-09-30_11-46-24.png)]
send:
package com.my.test.rabbitmq.topic;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Send {
final static String EXCHANGE_NAME = "test_exchange_topic";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);//topic
//发送消息
String msg = "商品......";
//routing key
String routingKey = "goods.add";
// String routingKey = "goods.delete";
// String routingKey = "goods.update";
channel.basicPublish(EXCHANGE_NAME,
routingKey,
MessageProperties.MINIMAL_PERSISTENT_BASIC//持久化消息
, msg.getBytes());
System.out.println("------send :" + msg);
channel.close();
connection.close();
}
}
receive1:
package com.my.test.rabbitmq.topic;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive1 {
private final static String QUEUE_NAME = "test_topic_queue1";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "goods.add");
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[1] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer1);
}
}
receive2:
package com.my.test.rabbitmq.topic;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-29
*/
@SuppressWarnings("DuplicatedCode")
public class Receive2 {
/**
* 如果和receive1是同一个队列名,则每个消息只会有一个消费者消费
*/
private final static String QUEUE_NAME = "test_topic_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//获取channel
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定队列到交换机
channel.queueBind(QUEUE_NAME, Send.EXCHANGE_NAME, "goods.#");
/**
* 每个消费者 发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一条消息
*/
channel.basicQos(1);
//定义消费者
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
//消息到达 触发这个方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("[2] receive msg :" + msg);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done");
/**
* 手动回执一个消息
* deliveryTag:该消息的index
* multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。
*/
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
/**
* 自动应答改成false
*/
boolean autoAck = false;
channel.basicConsume(QUEUE_NAME, autoAck, consumer1);
}
}
7、rabbitmq消息确认机制
在rabbitmq中,我们可以通过持久化数据 解决rabbitmq服务器的异常的数据丢失问题
问题:生产者将消息发送出去之后,消息到底有没有到达rabbitmq服务器
两种方式:
①、AMQP实现了事务机制
txSelect txCommit txRollback
txSelect :用于将当前channel设置成transaction模式
txCommit :用于提交事务
txRollback:回滚事务
send:
package com.my.test.rabbitmq.transaction;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-18
*/
@SuppressWarnings("DuplicatedCode")
public class Send {
static final String QUEUE_NAME = "test_simple_queue_tx";
public static void main(String[] args) throws IOException, TimeoutException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//从连接中创建一个管道
Channel channel = connection.createChannel();
//创建队列声明
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
String msg = "hello tx";
try {
/**
* 开启事务
*/
channel.txSelect();
/**
* 发送消息
*/
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
/**
* 提交事务
*/
channel.txCommit();
System.out.println("send " + msg);
} catch (Exception e) {
e.printStackTrace();
/**
* 回滚事务
*/
channel.txRollback();
System.out.println("send rollback " + msg);
}
channel.close();
connection.close();
}
}
receive:
package com.my.test.rabbitmq.transaction;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-18
*/
@SuppressWarnings("DuplicatedCode")
public class Receive {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(Send.QUEUE_NAME, true, false, false, null);
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println(Send.QUEUE_NAME + "receive:" + msg);
}
};
channel.basicConsume(Send.QUEUE_NAME, true, consumer);
}
}
②、confirm模式
生产者端confirm模式的实现原理:
-
生产者将信道设置成confirm模式,一旦信道进入confirm模式,所有在该信道上发布的消息都会被指派一个唯一的ID(从1开始),一旦消息被投递到所有匹配的队列之后,broker就会发送一个确认给生产者(包含消息的唯一ID),这就使得生产者知道消息已经正确到达目的队列了,如果消息和队列是可持久化的,那么确认消息会将消息写入磁盘之后发送给生产者。broker回传给生产者的确认消息中deliver-tag域中包含了确认消息的序列号,此外broker也可以设置basic.ack的multiple域,表示到这个序列号之前的所有消息都已经得到了处理。
-
confirm模式最大的好处在于异步
开启confirm模式:channel.confirmSelect()
编程模式:
1、普通 发一条 waitForConfirms()
package com.my.test.rabbitmq.confirm;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-18
* 单条确认
*/
@SuppressWarnings("DuplicatedCode")
public class Send1 {
static final String QUEUE_NAME = "test_queue_confirm1";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//从连接中创建一个管道
Channel channel = connection.createChannel();
//创建队列声明
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
/**
* 将channel设为confirm模式,注意不能同时设为事务模式
*/
channel.confirmSelect();
String msg = "hello confirm";
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
if (!channel.waitForConfirms()) {
System.out.println("message send failed");
} else {
System.out.println("send success " + msg);
}
channel.close();
connection.close();
}
}
2、批量的 发一批 waitForConfirms()
package com.my.test.rabbitmq.confirm;
import com.my.test.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author QinHe at 2019-09-18
* 批量确认
*/
@SuppressWarnings("DuplicatedCode")
public class Send2 {
static final String QUEUE_NAME = "test_queue_confirm2";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//获取连接
Connection connection = ConnectionUtils.getConnection();
//从连接中创建一个管道
Channel channel = connection.createChannel();
//创建队列声明
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
/**
* 将channel设为confirm模式,注意不能同时设为事务模式
*/
channel.confirmSelect();
String msg = "hello confirm";
//批量发送
for (int i = 0; i < 10; i++) {
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
}
//确认
if (!channel.waitForConfirms()) {
System.out.println("message send failed");
} else {
System.out.println("send success " + msg);
}
channel.close();
connection.close();
}
}
3、异步confirm模式 提供一个回调方法
channel对象提供的ConfirmListen()回调方法中质保函deliveryTag(当前channel发出的消息序号),我们需要自己为每一个channel维护一个unconfirm的消息序号集合,每publish一条数据,集合中元素加1,每回调一次handleAck方法,unconfirm集合删掉相应的一条(multiple=false) 或者多条(multiple=true)记录。从程序运行效率上看,这个unconfirm集合最好采用有序集合SortedSet存储结构
8、spring+rabbitmq
-
添加maven依赖
<!--rabbit spring--> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.0.0.RELEASE</version> </dependency>
-
spring配置文件 spring-rabbitmq.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!--定义rabbitmq的连接工厂--> <rabbit:connection-factory id="rabbitConnectionFactory" host="dev.com" port="5672" username="user_mmr" password="123456" virtual-host="/vhost_mmr"/> <!--定义rabbit模板,指定连接工厂以及定义exchange--> <rabbit:template id="rabbitTemplate" connection-factory="rabbitConnectionFactory" exchange="fanoutExchange"/> <!--mq的管理 包括队列 交换机声明等--> <rabbit:admin connection-factory="rabbitConnectionFactory"/> <!--定义队列,自动声明--> <rabbit:queue name="myQueue" auto-declare="true" durable="true"/> <!--定义交换机,自动声明--> <rabbit:fanout-exchange name="fanoutExchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="myQueue"/> </rabbit:bindings> </rabbit:fanout-exchange> <!--队列监听--> <rabbit:listener-container connection-factory="rabbitConnectionFactory"> <rabbit:listener ref="myConsumer" method="listen" queue-names="myQueue"/> </rabbit:listener-container> <!--消费者--> <bean id="myConsumer" class="com.my.rabbitmq.MyConsumer"/> </beans>
-
发送消息 SendService:
package com.my.rabbitmq; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; /** * @author QinHe at 2019-09-30 */ @Service public class SendService { private final RabbitTemplate rabbitTemplate; public SendService(RabbitTemplate rabbitTemplate) { this.rabbitTemplate = rabbitTemplate; } /** * 发送消息 * @param msg */ public void send(String msg) { rabbitTemplate.convertAndSend(msg); } }
-
消费 MyConsumer:
package com.my.rabbitmq; /** * @author QinHe at 2019-09-30 */ public class MyConsumer { public void listen(String foo) { System.out.println("消费者接受消息:" + foo); } }
rable=“true”/>
<rabbit:fanout-exchange name=“fanoutExchange” auto-declare=“true”>
rabbit:bindings
<rabbit:binding queue=“myQueue”/>
</rabbit:bindings>
</rabbit:fanout-exchange>
<rabbit:listener-container connection-factory=“rabbitConnectionFactory”>
<rabbit:listener ref=“myConsumer” method=“listen” queue-names=“myQueue”/>
</rabbit:listener-container>
* 发送消息 SendService:
```java
package com.my.rabbitmq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
/**
* @author QinHe at 2019-09-30
*/
@Service
public class SendService {
private final RabbitTemplate rabbitTemplate;
public SendService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
/**
* 发送消息
* @param msg
*/
public void send(String msg) {
rabbitTemplate.convertAndSend(msg);
}
}
-
消费 MyConsumer:
package com.my.rabbitmq; /** * @author QinHe at 2019-09-30 */ public class MyConsumer { public void listen(String foo) { System.out.println("消费者接受消息:" + foo); } }