https://blog.csdn.net/qq_35387940/article/details/100514134
https://www.bilibili.com/video/BV1gW411H7Az?p=1
https://www.rabbitmq.com/getstarted.html
https://www.rabbitmq.com/install-windows.html#installer
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
public class ConnectionUtils {
public static Connection getConnection() throws IOException, TimeoutException {
// 定义一个连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 服务地址
factory.setHost("127.0.0.1");
factory.setPort(5672); // AMQP 5672
factory.setVirtualHost("/vhost_mason");
factory.setUsername("user_mason");
factory.setPassword("123456");
return factory.newConnection(); // 获取连接
}
}
public class Send {
private 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("--send msg:" + msg);
channel.close();
connection.close();
}
}
public class Receive {
private 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);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("recv:" + msgString);
}
};
// 监听队列。
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
public class Send {
private static final String QUEUE_NAME = "test_work_queue";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
for (int i = 0; i < 50; i++) {
String msg = "hello " + i;
System.out.println("[Work Queue] send:" + msg);
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
Thread.sleep(i * 20);
}
channel.close();
connection.close();
}
}
public class Receive1 {
private static final String QUEUE_NAME = "test_work_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);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[1] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done ");
}
}
};
// 监听队列。
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
public class Receive2 {
private static final String QUEUE_NAME = "test_work_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);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[2] Recv:" + msgString);
try {
Thread.sleep(1000); // 比1快
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done ");
}
}
};
// 监听队列。
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_fanout";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
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();
}
}
public class Receive1 {
private static final String QUEUE_NAME = "test_queue_fanout_email";
private static final String EXCHANGE_NAME = "test_exchange_fanout";
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);
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"");
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[1] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done ");
// 手动回执一个消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 监听队列。
boolean autoAck = false; // 自动应答改为false
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
public class Receive2 {
private static final String QUEUE_NAME = "test_queue_fanout_sms";
private static final String EXCHANGE_NAME = "test_exchange_fanout";
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);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[2] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done ");
// 手动回执一个消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 监听队列。
boolean autoAck = false; // 自动应答改为false
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");// 类型是direct
String msg = "hello ps";
String routingKey = "error";// 路由键的名字
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("Send :" + msg);
channel.close();
connection.close();
}
}
public class Receive1 {
private static final String QUEUE_NAME = "test_queue_direct_1";
private static final String EXCHANGE_NAME = "test_exchange_direct";
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);
// 绑定队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[1] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done ");
// 手动回执一个消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 监听队列。
boolean autoAck = false; // 自动应答改为false
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
public class Receive2 {
private static final String QUEUE_NAME = "test_queue_direct_2";
private static final String EXCHANGE_NAME = "test_exchange_direct";
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);
// 绑定队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "warning");
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[2] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done ");
// 手动回执一个消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 监听队列。
boolean autoAck = false; // 自动应答改为false
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_topic";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");// 类型是topic
String msg = "商品.......";
String routingKey = "good.add";// 路由键的名字
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("Send :" + msg);
channel.close();
connection.close();
}
}
public class Receive1 {
private static final String QUEUE_NAME = "test_queue_topic_1";
private static final String EXCHANGE_NAME = "test_exchange_topic";
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);
// 绑定队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "good.add");
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[1] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done ");
// 手动回执一个消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 监听队列。
boolean autoAck = false; // 自动应答改为false
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
public class Receive2 {
private static final String QUEUE_NAME = "test_queue_topic_2";
private static final String EXCHANGE_NAME = "test_exchange_topic";
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);
// 绑定队列
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "good.#");
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("[2] Recv:" + msgString);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done ");
// 手动回执一个消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 监听队列。
boolean autoAck = false; // 自动应答改为false
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
}
}
public class TxSend {
private static final String QUEUE_NAME = "test_queue_tx";
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 msgString = "hello tx message!";
try {
channel.txSelect();
channel.basicPublish("", QUEUE_NAME, null, msgString.getBytes());
int i = 2 / 0; // 手动设置一个错误。
System.out.println("send:" + msgString);
channel.txCommit();
} catch (Exception e) {
channel.txRollback();
System.out.println("send message rollback");
}
channel.close();
connection.close();
}
}
public class TxRec {
private static final String QUEUE_NAME = "test_queue_tx";
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);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
// 获取到达的消息
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msgString = new String(body, "utf-8");
System.out.println("recv[tx]:" + msgString);
}
};
// 监听队列。
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
/**
* @Auther: Mason
* @Date: 2020/06/21/10:41
* @Description:
*/
@Configuration
public class TopicRabbitConfig {
// 第一个队列
@Bean
public Queue firstQueue() {
return new Queue("firstQueue");
}
// 第二个队列
@Bean
public Queue secondQueue() {
return new Queue("secondQueue");
}
// 路由
@Bean
TopicExchange exchange() {
return new TopicExchange("topicExchange");
}
//将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
//这样只要是消息携带的路由键是topic.man,才会分发到该队列
@Bean
Binding bindingExchangeMessage() {
return BindingBuilder.bind(firstQueue()).to(exchange()).with("topic.man");
}
//将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
// 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
@Bean
Binding bindingExchangeMessage2() {
return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
}
}
@Configuration
public class FanoutRabbitConfig {
/**
* 创建三个队列 :fanout.A fanout.B fanout.C
* 将三个队列都绑定在交换机 fanoutExchange 上
* 因为是扇型交换机, 路由键无需配置,配置也不起作用
*/
@Bean
public Queue queueA() {
return new Queue("fanout.A");
}
@Bean
public Queue queueB() {
return new Queue("fanout.B");
}
@Bean
public Queue queueC() {
return new Queue("fanout.C");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingExchangeA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC() {
return BindingBuilder.bind(queueC()).to(fanoutExchange());
}
}
@GetMapping("/sendFanoutMessage")
public String sendFanoutMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "message: testFanoutMessage ";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", createTime);
rabbitTemplate.convertAndSend("fanoutExchange", null, map);
return "ok";
}