tepechin_kirill_lab_4 #45
28
tepechin_kirill_lab_4/README.md
Normal file
28
tepechin_kirill_lab_4/README.md
Normal file
@ -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
|
||||||
|
|
BIN
tepechin_kirill_lab_4/consumer1.png
Normal file
BIN
tepechin_kirill_lab_4/consumer1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 65 KiB |
44
tepechin_kirill_lab_4/consumer1/src/main/java/Consumer1.java
Normal file
44
tepechin_kirill_lab_4/consumer1/src/main/java/Consumer1.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
tepechin_kirill_lab_4/consumer2.png
Normal file
BIN
tepechin_kirill_lab_4/consumer2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 101 KiB |
40
tepechin_kirill_lab_4/consumer2/src/main/java/Consumer2.java
Normal file
40
tepechin_kirill_lab_4/consumer2/src/main/java/Consumer2.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
tepechin_kirill_lab_4/publisher.png
Normal file
BIN
tepechin_kirill_lab_4/publisher.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 71 KiB |
33
tepechin_kirill_lab_4/publisher/src/main/java/Publisher.java
Normal file
33
tepechin_kirill_lab_4/publisher/src/main/java/Publisher.java
Normal file
@ -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 "Необходимо создать отчет";
|
||||||
|
}
|
||||||
|
}
|
BIN
tepechin_kirill_lab_4/report.png
Normal file
BIN
tepechin_kirill_lab_4/report.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 68 KiB |
BIN
tepechin_kirill_lab_4/report2.png
Normal file
BIN
tepechin_kirill_lab_4/report2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 125 KiB |
Loading…
Reference in New Issue
Block a user