Install RabbitMQ
A separate article on Installing RabbitMQ will be summarized later.
Because RabbitMQ is written in Erlang language, Erlang must be installed first.
After the installation is successful, the default VHost and Exchange will be provided.
Java API programming
Import dependency
Create Maven project, import dependency in pom.xml
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.6.0</version>
</dependency>
Producer
public class MyProducer {
private final static String EXCHANGE_NAME = "SIMPLE_EXCHANGE";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
// 连接 IP
factory.setHost("127.0.0.1");
// 连接端口
factory.setPort(5672);
// 虚拟机
factory.setVirtualHost("/");
// 用户
factory.setUsername("guest");
factory.setPassword("guest");
// 建立连接
Connection conn = factory.newConnection();
// 创建消息通道
Channel channel = conn.createChannel();
// 发送消息
String msg = "Hello world, Rabbit MQ";
// String exchange, String routingKey, BasicProperties props, byte[] body
channel.basicPublish(EXCHANGE_NAME, "bread", null, msg.getBytes());
channel.close();
conn.close();
}
}
consumer
public class MyConsumer {
private final static String EXCHANGE_NAME = "SIMPLE_EXCHANGE";
private final static String QUEUE_NAME = "SIMPLE_QUEUE";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
// 连接 IP
factory.setHost("127.0.0.1");
// 默认监听端口
factory.setPort(5672);
// 虚拟机
factory.setVirtualHost("/");
// 设置访问的用户
factory.setUsername("guest");
factory.setPassword("guest");
// 建立连接
Connection conn = factory.newConnection();
// 创建消息通道
Channel channel = conn.createChannel();
// 声明交换机
// String exchange, String type, boolean durable, boolean autoDelete, Map<String, Object> arguments
channel.exchangeDeclare(EXCHANGE_NAME, "direct", false, false, null);
// 声明队列
// String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" Waiting for message....");
// 绑定队列和交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "bread");
// 创建消费者
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println("Received message : '" + msg + "'");
System.out.println("consumerTag : " + consumerTag);
System.out.println("deliveryTag : " + envelope.getDeliveryTag());
}
};
// 开始获取消息
// String queue, boolean autoAck, Consumer callback
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
Detailed parameter
-
Declare the switch parameter
String type : the switch type, one of direct, topic, and fanout .
boolean durable : Whether it is durable, it represents whether the switch still exists after the server restarts. -
The parameter
boolean durable that declares the queue : Whether it is durable, it represents whether the queue still exists after the server restarts.
boolean exclusive : Whether the queue is exclusive. The exclusive queue can only be used in the Connection where it is declared (it can be used in different channels of the same Connection), and is automatically deleted when the connection is disconnected.
boolean autoDelete : Whether to delete automatically. If true, at least one consumer is connected to this queue. After all consumers connected to this queue are disconnected, the queue will be automatically deleted.Map<String, Object> arguments: other attributes of the queue, for example:
Attributes | meaning |
---|---|
x-message-ttl | The survival time of messages in the queue, in milliseconds |
x-expires | How long will the queue be deleted after no consumer access? |
x-max-length | Maximum number of messages in the queue |
x-max-length-bytes | The maximum capacity of the queue, in Byte |
x-dead-letter-exchange | Dead letter switch |
x-dead-letter-routing-key | Dead letter switch routing key |
x-max-priority | The maximum priority of the message in the queue, the priority of the message cannot exceed it |
- The message properties BasicProperties
lists some of the main parameters:
parameter | Paraphrase |
---|---|
Map<String,Object> headers | Other custom parameters of the message |
Integer deliveryMode | 2 Persistence, other: transient |
Integer priority | Priority of the message |
String correlationId | Correlation ID to facilitate RPC to be associated with the request |
String replyTo | Callback queue |
String expiration | TTL, message expiration time, in milliseconds |
Use of UI management interface
RabbitMQ can be managed through commands (RabbitMQ CLI), HTTP API, or through a visual interface. This web page is the managment plug-in.
- Enable management plugin
- Windows enable management plugin
cd C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.6\sbin
rabbitmq-plugins.bat enable rabbitmq_management
- Linux 启用管理插件
cd /usr/lib/rabbitmq/bin
./rabbitmq-plugins enable rabbitmq_management
-
Management interface access port The
default port is 15672, the default user guest, password guest. By default, guest users can only access on the local machine, and remote users need to create other users. -
Virtual machine
In the Admin tab: the default virtual machine is /, you can create a custom virtual machine.
-
Linux creates RabbitMQ user permissions.
For example, create user admin, password admin, and authorize access to all Vhosts
firewall-cmd --permanent --add-port=15672/tcp
firewall-cmd --reload
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"