diff --git a/tepechin_kirill_lab_4/README.md b/tepechin_kirill_lab_4/README.md new file mode 100644 index 0000000..0f4c1fc --- /dev/null +++ b/tepechin_kirill_lab_4/README.md @@ -0,0 +1,28 @@ +## Лабораторная работа №4, ПИбд-42 Тепечин Кирилл + +### Пример работы +Publisher + +![publisher](publisher.png) + +Consumer1 + +![Consumer1](consumer1.png) + +Consumer2 + +![Consumer2](consumer2.png) + +Отчёт Management UI, Consumer1 и Consumer2 + +![report](report.png) + +Отчёт Management UI, Consumer1 и Consumer1 + +![report2](report2.png) + +Здесь в очереди скапливаются сообщения + +### Ссылка на видео +https://youtu.be/GyO68cdpgT0 + diff --git a/tepechin_kirill_lab_4/consumer1.png b/tepechin_kirill_lab_4/consumer1.png new file mode 100644 index 0000000..c1364d2 Binary files /dev/null and b/tepechin_kirill_lab_4/consumer1.png differ diff --git a/tepechin_kirill_lab_4/consumer1/src/main/java/Consumer1.java b/tepechin_kirill_lab_4/consumer1/src/main/java/Consumer1.java new file mode 100644 index 0000000..ab5a82e --- /dev/null +++ b/tepechin_kirill_lab_4/consumer1/src/main/java/Consumer1.java @@ -0,0 +1,44 @@ +import com.rabbitmq.client.*; + + +public class Consumer1 { + private static final String EXCHANGE_NAME = "event_exchange"; + private static final String QUEUE_NAME = "consumer1_queue"; + + public static void main(String[] args) { + try { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + + try (Connection connection = factory.newConnection(); + Channel channel = connection.createChannel()) { + // Объявление очереди + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + + // Привязка очереди к обмену + channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ""); + + // Настройка consumer + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody()); + System.out.println("Consumer 1 Получено: " + message); + try { + Thread.sleep(3000); // Моделирование времени обработки + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("Consumer 1 Обработано: " + message); + }; + + // Начало приема сообщений + channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {}); + System.out.println("Consumer 1 ожидает сообщений. Для выхода нажмите CTRL+C"); + while (true) { + // Поддержание работы приложения + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/tepechin_kirill_lab_4/consumer2.png b/tepechin_kirill_lab_4/consumer2.png new file mode 100644 index 0000000..7312c1b Binary files /dev/null and b/tepechin_kirill_lab_4/consumer2.png differ diff --git a/tepechin_kirill_lab_4/consumer2/src/main/java/Consumer2.java b/tepechin_kirill_lab_4/consumer2/src/main/java/Consumer2.java new file mode 100644 index 0000000..23fb6f8 --- /dev/null +++ b/tepechin_kirill_lab_4/consumer2/src/main/java/Consumer2.java @@ -0,0 +1,40 @@ +import com.rabbitmq.client.*; + + +public class Consumer2 { + private static final String EXCHANGE_NAME = "event_exchange"; + private static final String QUEUE_NAME = "consumer2_queue"; + + public static void main(String[] args) { + try { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + + try (Connection connection = factory.newConnection(); + Channel channel = connection.createChannel()) { + // Объявление очереди + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + + // Привязка очереди к обмену + channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ""); + + // Настройка consumer + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody()); + System.out.println("Consumer 2 Получено: " + message); + // Нет задержки обработки для Потребителя 2 + System.out.println("Consumer 2 Обработано: " + message); + }; + + // Начало приема сообщений + channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {}); + System.out.println("Consumer 2 ожидает сообщений. Для выхода нажмите CTRL+C"); + while (true) { + // Поддержание работы приложения + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/tepechin_kirill_lab_4/publisher.png b/tepechin_kirill_lab_4/publisher.png new file mode 100644 index 0000000..5f3e409 Binary files /dev/null and b/tepechin_kirill_lab_4/publisher.png differ diff --git a/tepechin_kirill_lab_4/publisher/src/main/java/Publisher.java b/tepechin_kirill_lab_4/publisher/src/main/java/Publisher.java new file mode 100644 index 0000000..11ecf26 --- /dev/null +++ b/tepechin_kirill_lab_4/publisher/src/main/java/Publisher.java @@ -0,0 +1,33 @@ +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +public class Publisher { + private static final String EXCHANGE_NAME = "event_exchange"; + + public static void main(String[] args) { + try { + 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 = generateEventMessage(); + channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); + System.out.println("Отправлено: " + message); + Thread.sleep(1000); // Ожидание 1 секунду + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String generateEventMessage() { + return "Необходимо создать отчет"; + } +} diff --git a/tepechin_kirill_lab_4/report.png b/tepechin_kirill_lab_4/report.png new file mode 100644 index 0000000..d1da1ef Binary files /dev/null and b/tepechin_kirill_lab_4/report.png differ diff --git a/tepechin_kirill_lab_4/report2.png b/tepechin_kirill_lab_4/report2.png new file mode 100644 index 0000000..405dc89 Binary files /dev/null and b/tepechin_kirill_lab_4/report2.png differ