laba done

This commit is contained in:
Софья Островская 2023-12-15 19:09:53 +04:00
parent a88869747e
commit 7ea3ef3097
26 changed files with 303 additions and 0 deletions

View File

@ -0,0 +1,115 @@
# Отчет по лабораторной работе №4
Выполнила студентка гр. ИСЭбд-41 Островская С. Ф.
## Прохождение tutorial
Установила rabbitMQ server, erlang и зашла в брокер под гостем по http://localhost:15672/#/
![](pic/pic1.jpg)
Туториал 1:
![](pic/pic2.jpg)
![](pic/pic3.jpg)
Туториал 2:
![](pic/pic4.jpg)
![](pic/pic5.jpg)
Туториал 3:
![](pic/pic6.jpg)
![](pic/pic7.jpg)
## Разработка демонстрационных приложений
Предметная область: Дополнительный соглашения и договоры на проживание.
Разработаны три приложения, согласно предметной области.
1. Publisher
```py
import pika
import time
import random
collection = ["Application for accommodation in a dormitory", "Rector's order", "Conclusion of an agreement", "Check-into a dormitory"]
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='Agreements', exchange_type='fanout')
while True:
message = f"Message: {random.choice(collection)}"
channel.basic_publish(exchange='Agreements', routing_key='', body=message)
time.sleep(1)
connection.close()
```
2. Consumer 1.
```py
import pika
import time
def process_message(ch, method, properties, body):
print(f"Получено сообщение: {body}")
time.sleep(3)
print("Сообщение успешно обработано")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='Agreements', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
print('Ожидание сообщений...')
channel.start_consuming()
```
3. Consumer 2.
```py
import pika
def process_message(ch, method, properties, body):
print(f"Получено сообщение: {body}")
print("Сообщение успешно обработано")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='Agreements', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
print('Ожидание сообщений...')
channel.start_consuming()
```
## Результаты выполнения лабораторной работы
Результат отработки Consumer_1:
![](pic/pic8.jpg)
![](pic/pic9.jpg)
Результат отработки Consumer_2:
![](pic/pic10.jpg)
![](pic/pic11.jpg)
Вывод: Consumer_2 нагружает меньше памяти, чем Consumer_1 и принимает сообщения гораздо быстрее, тем самым не позволяя очереди накапливать огромное количество сообщений.

View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/lab_4_py.iml" filepath="$PROJECT_DIR$/.idea/lab_4_py.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,20 @@
import pika
import time
def process_message(ch, method, properties, body):
print(f"Получено сообщение: {body}")
time.sleep(3)
print("Сообщение успешно обработано")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='Agreements', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
print('Ожидание сообщений...')
channel.start_consuming()

View File

@ -0,0 +1,19 @@
import pika
def process_message(ch, method, properties, body):
print(f"Получено сообщение: {body}")
print("Сообщение успешно обработано")
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='Agreements', queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
print('Ожидание сообщений...')
channel.start_consuming()

View File

@ -0,0 +1,19 @@
import pika
import time
import random
collection = ["Application for accommodation in a dormitory", "Rector's order", "Conclusion of an agreement", "Check-into a dormitory"]
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='Agreements', exchange_type='fanout')
while True:
message = f"Message: {random.choice(collection)}"
channel.basic_publish(exchange='Agreements', routing_key='', body=message)
time.sleep(1)
connection.close()

View File

@ -0,0 +1,25 @@
import pika, sys, os
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='OstrovsofaLogs')
def callback(ch, method, properties, body):
print(f" [x] Получено сообщение: {body}")
channel.basic_consume(queue='OstrovsofaLogs', on_message_callback=callback, auto_ack=True)
print(' [*] Ожидание сообщений. Для завершения CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Произошла ошибка')
try:
sys.exit(0)
except SystemExit:
os._exit(0)

View File

@ -0,0 +1,11 @@
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='ostrovsofa')
channel.basic_publish(exchange='',routing_key='ostrovsofa',body='My cat"s name is Montik!')
print(" [x] Отправлено сообщение")
connection.close()

View File

@ -0,0 +1,13 @@
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='ostrovsofa2', durable=True)
message = ' '.join(sys.argv[1:]) or "My cat's name is Montik!"
channel.basic_publish(exchange='', routing_key='ostrovsofa2', body=message, properties=pika.BasicProperties(
delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))
print(f" [x] Отправлено {message}")
connection.close()

View File

@ -0,0 +1,19 @@
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='ostrovsofa2', durable=True)
print(' [*] Ожидание сообщений. Для завершения CTRL+C')
def callback(ch, method, properties, body):
print(f" [x] Получено сообщение: {body.decode()}")
time.sleep(body.count(b'.'))
print(" [x] Выполнено")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='ostrovsofa2', on_message_callback=callback)
channel.start_consuming()

View File

@ -0,0 +1,12 @@
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='OstrovsofaLogs', exchange_type='fanout')
message = ' '.join(sys.argv[1:]) or "Info: My cat's name is Montik!"
channel.basic_publish(exchange='OstrovsofaLogs', routing_key='', body=message)
print(f" [x] Отправлено сообщение: {message}")
connection.close()

View File

@ -0,0 +1,21 @@
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='OstrovsofaLogs', exchange_type='fanout')
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='OstrovsofaLogs', queue=queue_name)
print(' [*] Ожидание сообщений. Для завершения CTRL+C')
def callback(ch, method, properties, body):
print(f" [x] {body}")
channel.basic_consume(
queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB