消息队列——RabbitMQ
在图中,
P
代表producer
,它是消息的生产者;C
代表consumer
,它是消息的消费者;而红色的矩形正是我们所谓的消息队列,它位于RabbitMQ
中(RabbitMQ
中可以有很多这样的队列,并且每个队列都有一个唯一的名字)。生产者(们)可以将消息发送到消息队列中,消费者(们)可以从消息队列中取出消息。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mq</groupId>
<artifactId>mq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
</project>
public class ConnectionUtils {
//定义链接工厂
public static Connection getConnection() throws IOException, TimeoutException {
//定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost("127.0.0.1");
//端口
factory.setPort(5672);//amqp协议 端口 类似与mysql的3306
//设置账号信息,用户名、密码、vhost
factory.setVirtualHost("/test");
factory.setUsername("user");
factory.setPassword("123456");
// 通过工程获取连接
Connection connection = factory.newConnection();
return connection;
}
}
生产者
public class SendMq {
private static final String QUEUE_NAME = "QUEUE_simple";
public static void sendMsg() throws Exception {
/* 获取一个连接 */
Connection connection = ConnectionUtils.getConnection();
/* 从连接中创建通道 */
Channel channel = connection.createChannel();
// 创建队列 (声明) 因为我们要往队列里面发送消息,这是后就得知道往哪个队列中发送,就好比在哪个管子里面放水,
boolean durable = false;
boolean exclusive = false;
boolean autoDelete = false;
channel.queueDeclare(QUEUE_NAME, durable, exclusive, autoDelete, null);// 如果这个队列不存在,其实
// 这句话是不需要的
String msg = "Hello 张国强 !";
// 第一个参数是exchangeName(默认情况下代理服务器端是存在一个""名字的exchange的,
// 因此如果不创建exchange的话我们可以直接将该参数设置成"",如果创建了exchange的话
// 我们需要将该参数设置成创建的exchange的名字),第二个参数是路由键
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
System.out.println("---------send ms :" + msg);
channel.close();
connection.close();
}
public static void main(String[] args) throws Exception {
sendMsg();
}
消费者:
public class Consumer {
private static final String QUEUE_NAME = "QUEUE_simple";
public static void main(String[] args) throws Exception {
/* 获取一个连接 */
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, BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
};
// 监听队列
channel.basicConsume(QUEUE_NAME, true, consumer);
}