From 3eb5625335c712da28f593aa8d28fa5e0f7f62f8 Mon Sep 17 00:00:00 2001 From: jiulinxiri Date: Sat, 27 Nov 2021 19:29:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++ .idea/compiler.xml | 13 +++++ .idea/jarRepositories.xml | 20 ++++++++ .idea/misc.xml | 14 ++++++ .idea/runConfigurations.xml | 10 ++++ .idea/vcs.xml | 6 +++ pom.xml | 15 +++++- .../io/jiulinxiri/rabbitmq/one/Consumer.java | 42 ++++++++++++++++ .../io/jiulinxiri/rabbitmq/one/Producer.java | 48 +++++++++++++++++++ 9 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/io/jiulinxiri/rabbitmq/one/Consumer.java create mode 100644 src/main/java/io/jiulinxiri/rabbitmq/one/Producer.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..2a1f77d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5739229..39dd2cc 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.example + io.jiulinxiri RabbitMQ 1.0-SNAPSHOT @@ -13,4 +13,17 @@ 8 + + + com.rabbitmq + amqp-client + 5.14.0 + + + commons-io + commons-io + 2.6 + + + \ No newline at end of file diff --git a/src/main/java/io/jiulinxiri/rabbitmq/one/Consumer.java b/src/main/java/io/jiulinxiri/rabbitmq/one/Consumer.java new file mode 100644 index 0000000..17dddba --- /dev/null +++ b/src/main/java/io/jiulinxiri/rabbitmq/one/Consumer.java @@ -0,0 +1,42 @@ +package io.jiulinxiri.rabbitmq.one; + +import com.rabbitmq.client.*; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +/** + * 消费者 + */ +public class Consumer { + public static final String QUEUE_NAME = "hello"; + + public static void main(String[] args) throws IOException, TimeoutException { + // 创建连接工程 + ConnectionFactory factory = new ConnectionFactory(); + // 设置连接地址 + factory.setHost("shop.jiulinxiri.io"); + // 设置端口 + // factory.setPort(15672); + // 设置用户名 + factory.setUsername("jiulinxiri"); + // 设置密码 + factory.setPassword("123Abc@@"); + // 建立连接 + Connection connection = factory.newConnection(); + // 获取信道 + Channel channel = connection.createChannel(); + + DeliverCallback deliverCallback = (coustomerTag, message) -> { + System.out.println(new String(message.getBody())q); + }; + + CancelCallback cancelCallback = (coustomerTag) -> { + System.out.println("消费中断"); + }; + /** + * + */ + channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback); + } +} diff --git a/src/main/java/io/jiulinxiri/rabbitmq/one/Producer.java b/src/main/java/io/jiulinxiri/rabbitmq/one/Producer.java new file mode 100644 index 0000000..4e22c54 --- /dev/null +++ b/src/main/java/io/jiulinxiri/rabbitmq/one/Producer.java @@ -0,0 +1,48 @@ +package io.jiulinxiri.rabbitmq.one; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeoutException; + +public class Producer { + public static final String QUEUE_NAME = "hello"; + + public static void main(String[] args) throws IOException, TimeoutException { + // 创建连接工程 + ConnectionFactory factory = new ConnectionFactory(); + // 设置连接地址 + factory.setHost("shop.jiulinxiri.io"); + // 设置端口 +// factory.setPort(15672); + // 设置用户名 + factory.setUsername("jiulinxiri"); + // 设置密码 + factory.setPassword("123Abc@@"); + // 建立连接 + Connection connection = factory.newConnection(); + // 获取信道 + Channel channel = connection.createChannel(); + /** + * 生成队列 + * 1.队列名称 + * 2.从列里面的消息是否持久化(磁盘)默认情说消息存储在内存中 + * 3.该以列是否只供一个消费者进行消费 是否进行消息共享,true可以多个消费者消券 false:只能一个消费者消费 + * 4.是否自动册除 最后一个消费者端开连接以后 该队一句是否自动州除 true自动删除 false不自动删除 + * 5. 其它参数 + */ + channel.queueDeclare(QUEUE_NAME,false, false,false,null); + String message = "Hello World!"; + /** + * 1. 发送到哪个交换机 + * 2.路由的key值是哪个 本次是从列的名称 + * 3. 其它参数信息 + * 4 发送消总 + */ + channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); + System.out.println("消息发送完毕"); + } +}