微服务 教程
1085个小节阅读:195.6k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
接下来编写三个消费者,分别监听各自的队列。
xxxxxxxxxx
// 站内信消费者
public class CustomerStation {
public static void main(String[] args) throws IOException, TimeoutException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.162");
connectionFactory.setPort(5672);
connectionFactory.setUsername("itbaizhan");
connectionFactory.setPassword("itbaizhan");
connectionFactory.setVirtualHost("/");// 默认虚拟机
//2.创建连接
Connection conn = connectionFactory.newConnection();
//3.建立信道
Channel channel = conn.createChannel();
// 4.监听队列
channel.basicConsume("SEND_STATION", true, new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("发送站内信:"+message);
}
});
}
}
// 邮件消费者
public class Customer_Mail {
public static void main(String[] args) throws IOException, TimeoutException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.162");
connectionFactory.setPort(5672);
connectionFactory.setUsername("itbaizhan");
connectionFactory.setPassword("itbaizhan");
connectionFactory.setVirtualHost("/");// 默认虚拟机
//2.创建连接
Connection conn = connectionFactory.newConnection();
//3.建立信道
Channel channel = conn.createChannel();
// 4.监听队列
channel.basicConsume("SEND_MAIL", true, new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("发送邮件:"+message);
}
});
}
}
// 短信消费者
public class Customer_Message {
public static void main(String[] args) throws IOException, TimeoutException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.162");
connectionFactory.setPort(5672);
connectionFactory.setUsername("itbaizhan");
connectionFactory.setPassword("itbaizhan");
connectionFactory.setVirtualHost("/");// 默认虚拟机
//2.创建连接
Connection conn = connectionFactory.newConnection();
//3.建立信道
Channel channel = conn.createChannel();
// 4.监听队列
channel.basicConsume("SEND_MESSAGE", true, new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("发送短信:"+message);
}
});
}
}
也可以使用工作队列+发布订阅模式同时使用,两个消费者同时监听一个队列:
xxxxxxxxxx
// 短信消费者2
public class Customer_Message2 {
public static void main(String[] args) throws IOException, TimeoutException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.162");
connectionFactory.setPort(5672);
connectionFactory.setUsername("itbaizhan");
connectionFactory.setPassword("itbaizhan");
connectionFactory.setVirtualHost("/");// 默认虚拟机
//2.创建连接
Connection conn = connectionFactory.newConnection();
//3.建立信道
Channel channel = conn.createChannel();
// 4.监听队列
channel.basicConsume("SEND_MESSAGE", true, new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("发送短信2:"+message);
}
});
}
}
效果如下: