Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Redis and Pika_RabbitMq experiment #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions dor-pika-rabbitmq/app/publisher/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use a lightweight Python image
FROM python:3.9-slim

WORKDIR /app

COPY publisher.py .

# Install dependencies
RUN pip install pika

CMD ["python", "publisher.py"]
35 changes: 35 additions & 0 deletions dor-pika-rabbitmq/app/publisher/publisher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pika
import time

def publish_message(message):
try:
# Connect to RabbitMQ with credentials
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='rabbitmq',
credentials=pika.PlainCredentials('user', 'password')
))
channel = connection.channel()

# Declare a queue
channel.queue_declare(queue='test_queue')

# Publish the message
channel.basic_publish(exchange='', routing_key='test_queue', body=message)
print(f"Sent: {message}")

connection.close()
except Exception as e:
print(f"Error publishing message: {e}")

if __name__ == "__main__":
print("Publisher starting...")
for i in range(1, 6): # Send 5 messages
xml_message = f'<event><type>rmq_created</type><data><id>{i}</id></data></event>'
publish_message(xml_message)
xml_message = f'<event><type>rmq_updated</type><data><id>{i}</id></data></event>'
publish_message(xml_message)
time.sleep(5)
xml_message = f'<event><type>rmq_unknown</type><data><id>{i}</id></data></event>'
publish_message(xml_message)
time.sleep(5)
print("All messages sent.")
11 changes: 11 additions & 0 deletions dor-pika-rabbitmq/app/subscriber/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use a lightweight Python image
FROM python:3.9-slim

WORKDIR /app

COPY subscriber.py .

# Install dependencies
RUN pip install pika

CMD ["python", "subscriber.py"]
47 changes: 47 additions & 0 deletions dor-pika-rabbitmq/app/subscriber/subscriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pika


def handle_event(event_type, event_id):
# Define what to do based on the event type and ID
if event_type == "rmq_created":
print(f"Handling created event for ID: {event_id}")
elif event_type == "rmq_updated":
print(f"Handling updated event for ID: {event_id}")
else:
print(f"Unknown event type: {event_type} for ID: {event_id}")

def callback(ch, method, properties, body):
message = body.decode('utf-8')
print(f"Received: {message}")

# Parse the XML message to extract the event type and ID
import xml.etree.ElementTree as ET
root = ET.fromstring(message)
event_type = root.find('type').text
event_id = root.find('data/id').text

# Handle the event based on the type and ID
handle_event(event_type, event_id)

def subscribe():
try:
# Connect to RabbitMQ with credentials
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='rabbitmq',
credentials=pika.PlainCredentials('user', 'password') # Use the credentials set in docker-compose.yml
))
channel = connection.channel()

# Declare a queue
channel.queue_declare(queue='test_queue')

# Set up subscription
channel.basic_consume(queue='test_queue', on_message_callback=callback, auto_ack=True)

print("Subscriber started...")
channel.start_consuming()
except Exception as e:
print(f"Error in subscriber: {e}")

if __name__ == "__main__":
subscribe()
25 changes: 25 additions & 0 deletions dor-pika-rabbitmq/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3.8'

services:
rabbitmq:
image: rabbitmq:3.9-management
ports:
- "5672:5672" # RabbitMQ default port
- "15672:15672" # RabbitMQ management UI
environment:
RABBITMQ_DEFAULT_USER: user
RABBITMQ_DEFAULT_PASS: password

publisher:
build:
context: ./app/publisher
depends_on:
- rabbitmq
command: python publisher.py

subscriber:
build:
context: ./app/subscriber
depends_on:
- rabbitmq
command: python subscriber.py
9 changes: 9 additions & 0 deletions dor-redis/app/publisher/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.9-slim

WORKDIR /app
COPY publisher.py .

# Install redis library
RUN pip install redis

CMD ["python", "publisher.py"]
21 changes: 21 additions & 0 deletions dor-redis/app/publisher/publisher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import redis
import time

def publish_message(message):
r = redis.Redis(host='redis', port=6379)
r.lpush('test_queue', message)
print(f"Sent: {message}")

if __name__ == "__main__":
print("Publisher starting...")
max_messages = 5
i = 1
while (i <= max_messages):
xml_message = '<event><type>redis_created</type><data><id>' + str(i) + '</id></data></event>'
publish_message(xml_message)
time.sleep(5)
i += 1
if i == max_messages:
time.sleep(20) #sleep before exiting so that all msg are read

print("All messages sent. Publisher finishing.")
12 changes: 12 additions & 0 deletions dor-redis/app/subscriber/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# app/subscriber/Dockerfile
FROM python:3.9-slim

# Install redis-tools for redis-cli
RUN apt-get update && apt-get install -y redis-tools

WORKDIR /app
COPY subscriber.py .

RUN pip install redis

CMD ["python", "subscriber.py"]
25 changes: 25 additions & 0 deletions dor-redis/app/subscriber/subscriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import redis
import xml.etree.ElementTree as ET

def handle_event(event_type, event_data):
if event_type == 'redis_created':
print(f"Handling redis created event for ID: {event_data['id']}")
else:
print(f"No handler for event type: {event_type}")

def main():
print("Subscriber starting...")

r = redis.Redis(host='redis', port=6379)
print('Waiting for messages...')

while True:
message = r.brpop('test_queue')[1].decode()
print(f"Received: {message}")
root = ET.fromstring(message)
event_type = root.find('type').text
event_data = {child.tag: child.text for child in root.find('data')}
handle_event(event_type, event_data)

if __name__ == "__main__":
main()
19 changes: 19 additions & 0 deletions dor-redis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'

services:
redis:
image: redis:latest
ports:
- "6379:6379"

publisher:
build:
context: ./app/publisher
depends_on:
- redis

subscriber:
build:
context: ./app/subscriber
depends_on:
- redis