环境信息

JDK 版本 :1.7

RabbiMQ 服务端版本:3.8.8

erlang版本:Erlang 23.3.4.7

一、maven 依赖文件

<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-core -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>compile</scope>
</dependency>

二、使用步骤

1. 消息生产者

package com.julong.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
* 消息发送简单示例
* @author julong
* @date 2022年1月19日 上午10:40:17
* @desc
*/
public class SendMessage {
private final static String QUEUE_NAME = "hello-rabbitmq";
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.10.222");//设置连接地址
factory.setPort(5672);//连接端口号
factory.setUsername("julong");//账户
factory.setPassword("julong");//密码
try {
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
//queue the name of the queue 队列名称
//durable true if we are declaring a durable queue (the queue will survive a server restart) 是否持久化
//exclusive true if we are declaring an exclusive queue (restricted to this connection) 排他性
//autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use) 是否自动删除
//arguments other properties (construction arguments) for the queue 其他参数
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World !"+ QUEUE_NAME;
//发布消息
//exchange the exchange to publish the message to 交换机
//routingKey the routing key 路由键
//props other properties for the message - routing headers etc 其他配置属性
//body the message body 消息内容
for(int i = 0;i<5;i++){
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [消息发送] Sent '" + message + "'");
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

2.消息消费者

package com.julong.hello;

import java.io.IOException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
* 消息接收简单示例
* @author julong
* @date 2022年1月19日 上午10:40:17
* @desc
*/
public class ReceiveMessage {
private final static String QUEUE_NAME = "hello-rabbitmq";
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.10.222");//设置连接地址
factory.setPort(5672);//连接端口号
factory.setUsername("julong");//账户
factory.setPassword("julong");//密码
try {
//创建连接
Connection connection = factory.newConnection();
//创建 channel
Channel channel = connection.createChannel();
//创建队列
//queue the name of the queue 队列名称
//durable true if we are declaring a durable queue (the queue will survive a server restart) 是否持久化
//exclusive true if we are declaring an exclusive queue (restricted to this connection) 排他性
//autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use) 是否自动删除
//arguments other properties (construction arguments) for the queue 其他参数
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
byte[] body) throws IOException {
// TODO Auto-generated method stub
String message = new String(body, "UTF-8");
System.out.println(" [接收消息] Received '" + message + "'");
}

};
//设置消息回调
channel.basicConsume(QUEUE_NAME,true, consumer);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

结果

服务端:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repo/org/slf4j/slf4j-simple/1.7.30/slf4j-simple-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13:21:27,530 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:21:27,530 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:21:27,530 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/Workspaces/OWER/java-learn-examples/java-learn-examples-rabbitmq/java-learn-examples-rabbitmq-java/target/classes/logback.xml]
13:21:27,640 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:21:27,640 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:21:27,650 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:21:27,700 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to TRACE
13:21:27,700 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:21:27,700 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:21:27,700 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@629f0666 - Registering current configuration as safe fallback point
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
[消息发送] Sent 'Hello World !hello-rabbitmq'
[消息发送] Sent 'Hello World !hello-rabbitmq'
[消息发送] Sent 'Hello World !hello-rabbitmq'
[消息发送] Sent 'Hello World !hello-rabbitmq'
[消息发送] Sent 'Hello World !hello-rabbitmq'

客户端

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repo/org/slf4j/slf4j-simple/1.7.30/slf4j-simple-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13:21:21,676 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:21:21,676 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:21:21,676 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/Workspaces/OWER/java-learn-examples/java-learn-examples-rabbitmq/java-learn-examples-rabbitmq-java/target/classes/logback.xml]
13:21:21,926 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:21:21,926 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:21:21,936 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:21:21,996 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to TRACE
13:21:21,996 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:21:21,996 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:21:21,996 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1ff8b8f - Registering current configuration as safe fallback point
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
[*] Waiting for messages. To exit press CTRL+C
[接收消息] Received 'Hello World !hello-rabbitmq'
[接收消息] Received 'Hello World !hello-rabbitmq'
[接收消息] Received 'Hello World !hello-rabbitmq'
[接收消息] Received 'Hello World !hello-rabbitmq'
[接收消息] Received 'Hello World !hello-rabbitmq'