diff --git a/afanasev_dmitry_lab_4/README.md b/afanasev_dmitry_lab_4/README.md new file mode 100644 index 0000000..58a3ac2 --- /dev/null +++ b/afanasev_dmitry_lab_4/README.md @@ -0,0 +1,16 @@ +# Лабораторная работа 4 + +## Описание +Данная лабораторная работа предназначена для изучения проектирования приложений при помощи брокера сообщений. + +### Уроки из RabbitMQ Tutorials +1. ![img.png](images/img1.png) +2. ![img.png](images/img2.png) +3. ![img.png](images/img3.png) + +### Задание +Ссылка на демонстрацию работы программы: https://vk.com/video215756667_456239454?list=ln-IJETwWy23zLuysjetJ + +## Выводы +1. Запуск 1 и 2 Consumer'а: Consumer-2 (без задержки) график Queued Messages держится на нуле, в то время как у Consumer-1 график возрастает (скорость обработки сообщений < 1 с). +2. Запуск 2 первых Consumer'ов: обрабатывают сообщения последовательно и накапливают очередь. diff --git a/afanasev_dmitry_lab_4/images/img1.png b/afanasev_dmitry_lab_4/images/img1.png new file mode 100644 index 0000000..31321e3 Binary files /dev/null and b/afanasev_dmitry_lab_4/images/img1.png differ diff --git a/afanasev_dmitry_lab_4/images/img2.png b/afanasev_dmitry_lab_4/images/img2.png new file mode 100644 index 0000000..0f98ad7 Binary files /dev/null and b/afanasev_dmitry_lab_4/images/img2.png differ diff --git a/afanasev_dmitry_lab_4/images/img3.png b/afanasev_dmitry_lab_4/images/img3.png new file mode 100644 index 0000000..33d3bf1 Binary files /dev/null and b/afanasev_dmitry_lab_4/images/img3.png differ diff --git a/afanasev_dmitry_lab_4/lesson1/.gitignore b/afanasev_dmitry_lab_4/lesson1/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson1/.idea/.gitignore b/afanasev_dmitry_lab_4/lesson1/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/afanasev_dmitry_lab_4/lesson1/.idea/encodings.xml b/afanasev_dmitry_lab_4/lesson1/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson1/.idea/misc.xml b/afanasev_dmitry_lab_4/lesson1/.idea/misc.xml new file mode 100644 index 0000000..9930577 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson1/.idea/uiDesigner.xml b/afanasev_dmitry_lab_4/lesson1/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson1/.idea/vcs.xml b/afanasev_dmitry_lab_4/lesson1/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Main.class b/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Main.class new file mode 100644 index 0000000..e9f0a1c Binary files /dev/null and b/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Main.class differ diff --git a/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Recv.class b/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Recv.class new file mode 100644 index 0000000..7d2134d Binary files /dev/null and b/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Recv.class differ diff --git a/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Send.class b/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Send.class new file mode 100644 index 0000000..b6138b1 Binary files /dev/null and b/afanasev_dmitry_lab_4/lesson1/out/ru/ulstu/Send.class differ diff --git a/afanasev_dmitry_lab_4/lesson1/pom.xml b/afanasev_dmitry_lab_4/lesson1/pom.xml new file mode 100644 index 0000000..137f696 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + ru.ulstu + lesson1 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.rabbitmq + amqp-client + 5.22.0 + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson1/src/main/java/ru/ulstu/Recv.java b/afanasev_dmitry_lab_4/lesson1/src/main/java/ru/ulstu/Recv.java new file mode 100644 index 0000000..d86b3f2 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/src/main/java/ru/ulstu/Recv.java @@ -0,0 +1,29 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DeliverCallback; + +public class Recv { + private final static String QUEUE_NAME = "hello"; + + public static void main(String[] argv) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + factory.setPort(5672); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); + + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody(), "UTF-8"); + System.out.println(" [x] Received '" + message + "'"); + }; + channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { + }); + } +} + diff --git a/afanasev_dmitry_lab_4/lesson1/src/main/java/ru/ulstu/Send.java b/afanasev_dmitry_lab_4/lesson1/src/main/java/ru/ulstu/Send.java new file mode 100644 index 0000000..50aade8 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson1/src/main/java/ru/ulstu/Send.java @@ -0,0 +1,24 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +public class Send { + private final static String QUEUE_NAME = "hello"; + + public static void main(String[] argv) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + factory.setPort(5672); + try (Connection connection = factory.newConnection(); + Channel channel = connection.createChannel()) { + + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + String message = "Hello World!"; + channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); + System.out.println(" [x] Sent '" + message + "'"); + } + } +} + diff --git a/afanasev_dmitry_lab_4/lesson2/.gitignore b/afanasev_dmitry_lab_4/lesson2/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson2/.idea/.gitignore b/afanasev_dmitry_lab_4/lesson2/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/afanasev_dmitry_lab_4/lesson2/.idea/encodings.xml b/afanasev_dmitry_lab_4/lesson2/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson2/.idea/misc.xml b/afanasev_dmitry_lab_4/lesson2/.idea/misc.xml new file mode 100644 index 0000000..9930577 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson2/.idea/vcs.xml b/afanasev_dmitry_lab_4/lesson2/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson2/pom.xml b/afanasev_dmitry_lab_4/lesson2/pom.xml new file mode 100644 index 0000000..077b2f1 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + ru.ulstu + lesson2 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.rabbitmq + amqp-client + 5.22.0 + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson2/src/main/java/ru/ulstu/NewTask.java b/afanasev_dmitry_lab_4/lesson2/src/main/java/ru/ulstu/NewTask.java new file mode 100644 index 0000000..2eac498 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/src/main/java/ru/ulstu/NewTask.java @@ -0,0 +1,29 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.MessageProperties; + +public class NewTask { + private static final String TASK_QUEUE_NAME = "task_queue"; + + public static void main(String[] argv) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + factory.setPort(5672); + try (Connection connection = factory.newConnection(); + Channel channel = connection.createChannel()) { + + channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); + + String message = String.join(" ", argv); + + + channel.basicPublish("", TASK_QUEUE_NAME, + MessageProperties.PERSISTENT_TEXT_PLAIN, + message.getBytes("UTF-8")); + System.out.println(" [x] Sent '" + message + "'"); + } + } +} \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson2/src/main/java/ru/ulstu/Worker.java b/afanasev_dmitry_lab_4/lesson2/src/main/java/ru/ulstu/Worker.java new file mode 100644 index 0000000..67c09c3 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson2/src/main/java/ru/ulstu/Worker.java @@ -0,0 +1,48 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DeliverCallback; + +public class Worker { + private static final String TASK_QUEUE_NAME = "task_queue"; + + public static void main(String[] argv) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + factory.setPort(5672); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); + System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); + + int prefetchCount = 1; + channel.basicQos(prefetchCount); + + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody(), "UTF-8"); + System.out.println(" [x] Received '" + message + "'"); + try { + doWork(message); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } finally { + System.out.println(" [x] Done"); + channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); + } + }; + + boolean autoAck = false; + channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, consumerTag -> { + }); + } + + private static void doWork(String task) throws InterruptedException { + for (char ch : task.toCharArray()) { + if (ch == '.') Thread.sleep(1000); + } + } +} + diff --git a/afanasev_dmitry_lab_4/lesson3/.gitignore b/afanasev_dmitry_lab_4/lesson3/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson3/.idea/.gitignore b/afanasev_dmitry_lab_4/lesson3/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/afanasev_dmitry_lab_4/lesson3/.idea/encodings.xml b/afanasev_dmitry_lab_4/lesson3/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson3/.idea/misc.xml b/afanasev_dmitry_lab_4/lesson3/.idea/misc.xml new file mode 100644 index 0000000..9930577 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson3/.idea/uiDesigner.xml b/afanasev_dmitry_lab_4/lesson3/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson3/.idea/vcs.xml b/afanasev_dmitry_lab_4/lesson3/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson3/pom.xml b/afanasev_dmitry_lab_4/lesson3/pom.xml new file mode 100644 index 0000000..e7d92b8 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + ru.ulstu + lesson3 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.rabbitmq + amqp-client + 5.22.0 + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/lesson3/src/main/java/ru/ulstu/EmitLog.java b/afanasev_dmitry_lab_4/lesson3/src/main/java/ru/ulstu/EmitLog.java new file mode 100644 index 0000000..f1ffc25 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/src/main/java/ru/ulstu/EmitLog.java @@ -0,0 +1,25 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +public class EmitLog { + private static final String EXCHANGE_NAME = "logs"; + + public static void main(String[] argv) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + factory.setPort(5672); + try (Connection connection = factory.newConnection(); + Channel channel = connection.createChannel()) { + + channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); + + String message = argv.length < 1 ? "info: Hello World!" : String.join(" ", argv); + + channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8")); + System.out.println(" [x] Sent '" + message + "'"); + } + } +} diff --git a/afanasev_dmitry_lab_4/lesson3/src/main/java/ru/ulstu/ReceiveLogs.java b/afanasev_dmitry_lab_4/lesson3/src/main/java/ru/ulstu/ReceiveLogs.java new file mode 100644 index 0000000..60760a8 --- /dev/null +++ b/afanasev_dmitry_lab_4/lesson3/src/main/java/ru/ulstu/ReceiveLogs.java @@ -0,0 +1,33 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DeliverCallback; + +public class ReceiveLogs { + private static final String EXCHANGE_NAME = "logs"; + + public static void main(String[] argv) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + factory.setPort(5672); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); + + String queueName = channel.queueDeclare().getQueue(); + channel.queueBind(queueName, EXCHANGE_NAME, ""); + + System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); + + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody(), "UTF-8"); + System.out.println(" [x] Received '" + message + "'"); + }; + channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { + }); + } +} + diff --git a/afanasev_dmitry_lab_4/task/.gitignore b/afanasev_dmitry_lab_4/task/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/task/.idea/.gitignore b/afanasev_dmitry_lab_4/task/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/afanasev_dmitry_lab_4/task/.idea/encodings.xml b/afanasev_dmitry_lab_4/task/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/afanasev_dmitry_lab_4/task/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/task/.idea/misc.xml b/afanasev_dmitry_lab_4/task/.idea/misc.xml new file mode 100644 index 0000000..9930577 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/.idea/misc.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/task/.idea/uiDesigner.xml b/afanasev_dmitry_lab_4/task/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/task/.idea/vcs.xml b/afanasev_dmitry_lab_4/task/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/task/pom.xml b/afanasev_dmitry_lab_4/task/pom.xml new file mode 100644 index 0000000..ad5b467 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/pom.xml @@ -0,0 +1,25 @@ + + + 4.0.0 + + ru.ulstu + task + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.rabbitmq + amqp-client + 5.22.0 + + + + \ No newline at end of file diff --git a/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderConsumer1.java b/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderConsumer1.java new file mode 100644 index 0000000..8bd74cb --- /dev/null +++ b/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderConsumer1.java @@ -0,0 +1,38 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DeliverCallback; + +public class OrderConsumer1 { + private static final String EXCHANGE_NAME = "orders"; + private static final String QUEUE_NAME = "consumer1_queue"; + + public static void main(String[] args) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ""); + + System.out.println(" [*] Consumer 1 ожидает сообщений."); + + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody(), "UTF-8"); + channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); + System.out.println(" [x] Consumer 1 получил сообщение, обработает через 3 с.: " + message); + + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }; + channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> { + }); + } +} diff --git a/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderConsumer2.java b/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderConsumer2.java new file mode 100644 index 0000000..b642111 --- /dev/null +++ b/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderConsumer2.java @@ -0,0 +1,34 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DeliverCallback; + +public class OrderConsumer2 { + private static final String EXCHANGE_NAME = "orders"; + private static final String QUEUE_NAME = "consumer2_queue"; + + public static void main(String[] args) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + // Создаём exchange и очередь + channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ""); + + System.out.println(" [*] Consumer 2 ожидает сообщений."); + + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody(), "UTF-8"); + channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); + System.out.println(" [x] Consumer 2 моментально обработал сообщение: " + message); + }; + channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> { + }); + } +} + diff --git a/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderPublisher.java b/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderPublisher.java new file mode 100644 index 0000000..dda3c1d --- /dev/null +++ b/afanasev_dmitry_lab_4/task/src/main/java/ru/ulstu/OrderPublisher.java @@ -0,0 +1,26 @@ +package ru.ulstu; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +public class OrderPublisher { + private static final String EXCHANGE_NAME = "orders"; + + public static void main(String[] args) throws Exception { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + try (Connection connection = factory.newConnection(); + Channel channel = connection.createChannel()) { + + channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); + + while (true) { + String message = "Новый заказ #" + System.currentTimeMillis(); + channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8")); + System.out.println(" [x] Отправлено сообщение: " + message); + Thread.sleep(1000); + } + } + } +}