Hello World
RabbitMQ工作图示:
如上图:
P代表Producing,也就是消息的发送程序
中间的队列代表消息将会被存到对应的队列中。可以多个Producers向同一个队列发送消息,也可以多个Consumer接收同一个队列的消息
C代表Consumer,也就是等待接收消息的程序
需要的依赖:
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
程序实例:
Producers
private final static String QUEEN_NAME = "hello";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("101.33.239.187");
// Connection 负责版本协商以及身份认证工作
// Chanel 包含了大部分任务所需的API
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEEN_NAME, false, false, false, null);
channel.basicPublish("", QUEEN_NAME, null, ("Hello RabbitMQ!" + Math.random()).getBytes(StandardCharsets.UTF_8));
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
Consumers
private final static String QUEEN_NAME = "hello";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("101.33.239.187");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEEN_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback callback = (consumerTag, delivery) -> {
String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + msg + "'");
};
channel.basicConsume(QUEEN_NAME, true, callback, (consumerTag) -> {});
}
channel.basicConsume会阻塞进程有消息通知时会调用callback
PS:备注
-
如果有多个消费者,那么队列中的消息将会按照消费者队列进行依次递送
-
rabbitmqctl list_queues 可以展示所有的队列信息
Hello World
RabbitMQ工作图示:
如上图:
P代表Producing,也就是消息的发送程序
中间的队列代表消息将会被存到对应的队列中。可以多个Producers向同一个队列发送消息,也可以多个Consumer接收同一个队列的消息
C代表Consumer,也就是等待接收消息的程序
需要的依赖:
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
程序实例:
Producers
private final static String QUEEN_NAME = "hello";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("101.33.239.187");
// Connection 负责版本协商以及身份认证工作
// Chanel 包含了大部分任务所需的API
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEEN_NAME, false, false, false, null);
channel.basicPublish("", QUEEN_NAME, null, ("Hello RabbitMQ!" + Math.random()).getBytes(StandardCharsets.UTF_8));
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
Consumers
private final static String QUEEN_NAME = "hello";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("101.33.239.187");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEEN_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback callback = (consumerTag, delivery) -> {
String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + msg + "'");
};
channel.basicConsume(QUEEN_NAME, true, callback, (consumerTag) -> {});
}
channel.basicConsume会阻塞进程有消息通知时会调用callback
PS:备注
-
如果有多个消费者,那么队列中的消息将会按照消费者队列进行依次递送
-
rabbitmqctl list_queues 可以展示所有的队列信息
Hello World
RabbitMQ工作图示:
如上图:
P代表Producing,也就是消息的发送程序
中间的队列代表消息将会被存到对应的队列中。可以多个Producers向同一个队列发送消息,也可以多个Consumer接收同一个队列的消息
C代表Consumer,也就是等待接收消息的程序
需要的依赖:
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
程序实例:
Producers
private final static String QUEEN_NAME = "hello";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("101.33.239.187");
// Connection 负责版本协商以及身份认证工作
// Chanel 包含了大部分任务所需的API
try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEEN_NAME, false, false, false, null);
channel.basicPublish("", QUEEN_NAME, null, ("Hello RabbitMQ!" + Math.random()).getBytes(StandardCharsets.UTF_8));
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
Consumers
private final static String QUEEN_NAME = "hello";
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("101.33.239.187");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEEN_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback callback = (consumerTag, delivery) -> {
String msg = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + msg + "'");
};
channel.basicConsume(QUEEN_NAME, true, callback, (consumerTag) -> {});
}
channel.basicConsume会阻塞进程有消息通知时会调用callback
PS:备注
-
如果有多个消费者,那么队列中的消息将会按照消费者队列进行依次递送
-
rabbitmqctl list_queues 可以展示所有的队列信息