Linux常用shell命令
This commit is contained in:
20
code/RabbitMQ/rabbitmq-basis/pom.xml
Normal file
20
code/RabbitMQ/rabbitmq-basis/pom.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?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>
|
||||
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>rabbitmq-basis</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.rabbitmq</groupId>
|
||||
<artifactId>amqp-client</artifactId>
|
||||
<version>5.6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,45 @@
|
||||
package com.heibaiying;
|
||||
|
||||
import com.rabbitmq.client.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 消费者
|
||||
*/
|
||||
public class Consumer {
|
||||
|
||||
private static final String QUEUE_NAME = "queue-hello";
|
||||
private static final String USER_NAME = "root";
|
||||
private static final String PASSWORD = "root";
|
||||
private static final String IP_ADDRESS = "192.168.200.229";
|
||||
/* RabbitMQ 服务默认的连接端口号 */
|
||||
private static final int PORT = 5672;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost(IP_ADDRESS);
|
||||
factory.setPort(PORT);
|
||||
factory.setUsername(USER_NAME);
|
||||
factory.setPassword(PASSWORD);
|
||||
Connection connection = factory.newConnection();
|
||||
final Channel channel = connection.createChannel();
|
||||
// 设置客户端最多接收多少个未被 Ack 的消息, 0 代表没有限制
|
||||
channel.basicQos(64);
|
||||
DefaultConsumer consumer = new DefaultConsumer(channel) {
|
||||
@Override
|
||||
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
|
||||
System.out.println("收到消息:" + new String(body));
|
||||
// 消息 Ack
|
||||
channel.basicAck(envelope.getDeliveryTag(), false);
|
||||
}
|
||||
};
|
||||
channel.basicConsume(QUEUE_NAME, consumer);
|
||||
// 等到回调函数执行后再关闭连接
|
||||
TimeUnit.SECONDS.sleep(5);
|
||||
channel.close();
|
||||
connection.close();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.heibaiying;
|
||||
|
||||
import com.rabbitmq.client.*;
|
||||
|
||||
/**
|
||||
* 生产者
|
||||
*/
|
||||
public class Producer {
|
||||
|
||||
private static final String EXCHANGE_NAME = "exchange-hello";
|
||||
private static final String ROUTING_KEY = "routingkey-hello";
|
||||
private static final String QUEUE_NAME = "queue-hello";
|
||||
private static final String USER_NAME = "root";
|
||||
private static final String PASSWORD = "root";
|
||||
private static final String IP_ADDRESS = "192.168.200.229";
|
||||
/* RabbitMQ 服务默认的连接端口号 */
|
||||
private static final int PORT = 5672;
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// 创建连接工厂
|
||||
ConnectionFactory factory = new ConnectionFactory();
|
||||
factory.setHost(IP_ADDRESS);
|
||||
factory.setPort(PORT);
|
||||
factory.setUsername(USER_NAME);
|
||||
factory.setPassword(PASSWORD);
|
||||
// 获取新的连接
|
||||
Connection connection = factory.newConnection();
|
||||
// 创建信道
|
||||
Channel channel = connection.createChannel();
|
||||
// 创建一个 type="direct"、持久化的、非自动删除的交换器
|
||||
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, null);
|
||||
// 创建一个持久化、非排他的、非自动删除的队列
|
||||
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
|
||||
// 将交换器与队列通过路由键绑定
|
||||
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
|
||||
String message = "hello world";
|
||||
// 发送一条持久化的文本消息
|
||||
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
|
||||
// 关闭信道
|
||||
channel.close();
|
||||
// 关闭连接
|
||||
connection.close();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user