- ConnectionFactory: 获取连接工厂
- Connection 一个连接
- Channel 数据通信信道,可发送和接受消息
- Queue 具体消息存储队列
- Producer & Consumer 生产者和消费者
具体实现代码
pom.xml
注意:这里是5.30版本
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.3.0</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>wang.chunsen</groupId>
<artifactId>rabbitmq-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmq-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.3.0</version>
</dependency>
<!--<version>3.6.5</version>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
生产者:
package wang.chunsen.quickstart.version2;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @Author: itastro
* @Description:
* @Date: Created in 13:45 2018/12/12
* @Package: wang.chunsen.quickstart.version2
* @Modified By:
*/
public class Producer {
private static final String EXCHANGE_NAME = "test";
public static void main(String[] args) throws Exception {
//创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.114");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//创建一个连接
Connection connection = connectionFactory.newConnection();
//创建一个channel
Channel channel = connection.createChannel();
//通过channel 发送数据
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String message = "i love you rabbitmq";
for (int i = 0; i <10 ; i++) {
channel.basicPublish(EXCHANGE_NAME, "test001", null, message.getBytes("UTF-8"));
}
}
}
消费者
package wang.chunsen.quickstart.version2;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
/**
* @Author: itastro
* @Description:
* @Date: Created in 13:54 2018/12/12
* @Package: wang.chunsen.quickstart.version2
* @Modified By:
*/
public class Cosumer {
private static final String EXCHANGE_NAME = "test";
public static void main(String[] args) throws Exception {
//创建连接工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.114");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//创建一个连接
Connection connection = connectionFactory.newConnection();
//创建一个channel
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
//4 声明(创建)一个队列
String queueName = "test002";
channel.queueDeclare(queueName, true, false, false, null);
channel.queueBind(queueName, EXCHANGE_NAME, "test001");
System.out.println(" [*] Waiting for messages");
// DeliverCallback deliverCallback = (consumerTag, delivery) -> {
//
// String message = new String(delivery.getBody(), "UTF-8");
// System.out.println(" [x] Received '" + message + "'");
// };
//
//
// channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
while (true) {
//消费消息
boolean autoAck = false;
String consumerTag = "";
channel.basicConsume(queueName, autoAck, consumerTag, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException {
String routingKey = envelope.getRoutingKey();
String contentType = properties.getContentType();
System.out.println("消费的路由键:" + routingKey);
System.out.println("消费的内容类型:" + contentType);
long deliveryTag = envelope.getDeliveryTag();
//确认消息
channel.basicAck(deliveryTag, false);
System.out.println("消费的消息体内容:");
String bodyStr = new String(body, "UTF-8");
System.out.println(bodyStr);
}
});
}
}
}
以下代码使用 3.6.5
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.3.0</version>
</dependency>
生产者:
package wang.chunsen.quickstart.version1;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @Author: itastro
* @Description: 生产者
* @Date: Created in 10:47 2018/12/12
* @Package: wang.chunsen.quickstart
* @Modified By:
*/
public class producer {
public static void main(String[] args) throws Exception {
//1 创建一个ConnectFactory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.114");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//2 通过连接工厂创建连接
Connection connection = connectionFactory.newConnection();
//3 创建一个Channel
Channel channel = connection.createChannel();
//4通过Channel 发送数据
// void basicPublish(String var1, String var2, BasicProperties var3, byte[] var4) throws IOException;
for (int i = 0; i < 5; i++) {
String message = "Hello RabbitMQ";
// 1 exchange 2 routingkey
//当不指定exchange 时 会使用默认的
channel.basicPublish("", "test001", null, message.getBytes());
System.out.println("1");
}
//5 记得要关闭相关的连接
channel.close();
connection.close();
}
}
消费者:
package wang.chunsen.quickstart.version1;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Delivery;
/**
* @Author: itastro
* @Description: 消费端
* @Date: Created in 10:48 2018/12/12
* @Package: wang.chunsen.quickstart
* @Modified By:
*/
public class Consumer {
public static void main(String[] args) throws Exception {
//1 创建一个ConnectFactory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.0.114");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
//2 通过连接工厂创建连接
Connection connection = connectionFactory.newConnection();
//3 创建一个Channel
Channel channel = connection.createChannel();
//4 声明(创建)一个队列
String queueName = "test001";
channel.queueDeclare(queueName, true, false, false, null);
//5 创建一个消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
//6 设置Chanel
channel.basicConsume(queueName, true, consumer);
while (true) {
//7 获取消息
Delivery delivery = consumer.nextDelivery();
String msg = new String(delivery.getBody());
System.err.println("消费端: " + msg);
}
}
}