-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16ee323
commit 2f4152f
Showing
6 changed files
with
167 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import json | ||
from asyncio import Queue | ||
|
||
from aiokafka import AIOKafkaConsumer | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KafkaMessageConsumer: | ||
def __init__( | ||
self, bootstrap_servers, consumer_topic, group_id, message_queue: Queue | ||
): | ||
self.bootstrap_servers = bootstrap_servers | ||
self.consumer_topic = consumer_topic | ||
self.group_id = group_id | ||
self.message_queue = message_queue | ||
self.consumer = None | ||
|
||
async def start(self): | ||
logger.info("starting") | ||
self.consumer = AIOKafkaConsumer( | ||
self.consumer_topic, | ||
group_id=self.group_id, | ||
loop=asyncio.get_event_loop(), | ||
bootstrap_servers=self.bootstrap_servers, | ||
) | ||
await self.consumer.start() | ||
|
||
async def stop(self): | ||
logger.info("stopping") | ||
if self.consumer: | ||
await self.consumer.stop() | ||
|
||
async def consume_messages(self): | ||
logger.info("start consuming messages") | ||
try: | ||
async for message in self.consumer: | ||
message = message.value.decode("utf-8") | ||
message = json.loads(message) | ||
await self.message_queue.put(message) | ||
except Exception as e: | ||
print(f"Error consuming message: {e}") | ||
|
||
async def consume_messages_continuously(self): | ||
while True: | ||
await self.start() | ||
try: | ||
await self.consume_messages() | ||
finally: | ||
await self.stop() | ||
|
||
await asyncio.sleep(5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import asyncio | ||
from asyncio import Queue | ||
from aiokafka import AIOKafkaProducer | ||
|
||
|
||
class KafkaMessageProducer: | ||
def __init__(self, bootstrap_servers, producer_topic): | ||
self.bootstrap_servers = bootstrap_servers | ||
self.producer_topic = producer_topic | ||
self.producer = None | ||
|
||
async def start(self): | ||
self.producer = AIOKafkaProducer( | ||
loop=asyncio.get_event_loop(), | ||
bootstrap_servers=self.bootstrap_servers, | ||
) | ||
await self.producer.start() | ||
|
||
async def stop(self): | ||
if self.producer: | ||
await self.producer.stop() | ||
|
||
async def produce_message(self, message): | ||
try: | ||
await self.producer.send(self.producer_topic, message.encode("utf-8")) | ||
except Exception as e: | ||
print(f"Error producing message: {e}") | ||
|
||
async def produce_messages_from_queue(self, producer_queue: Queue): | ||
while True: | ||
await self.start() | ||
try: | ||
async for message in producer_queue: | ||
await self.produce_message(message) | ||
producer_queue.task_done() | ||
finally: | ||
await self.stop() | ||
|
||
await asyncio.sleep(5) |