saved
This commit is contained in:
36
CSV.py
Normal file
36
CSV.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import json, csv, os, time, stomp
|
||||
from datetime import datetime
|
||||
from flask_backend.Stomp_client import get_connection
|
||||
|
||||
QUEUE_NAME = "/queue/testQueue"
|
||||
OUTPUT_DIR = "/app/output"
|
||||
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
||||
|
||||
class MyListener(stomp.ConnectionListener):
|
||||
def on_message(self, frame):
|
||||
try:
|
||||
data = json.loads(frame.body)
|
||||
except json.JSONDecodeError:
|
||||
print("Received invalid JSON:", frame.body)
|
||||
return
|
||||
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
filename = os.path.join(OUTPUT_DIR, f"message_{timestamp}.csv")
|
||||
|
||||
try:
|
||||
with open(filename, "w", newline="") as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerow(data.keys())
|
||||
writer.writerow(data.values())
|
||||
print(f"Saved CSV: {filename}")
|
||||
except Exception as e:
|
||||
print("Error writing CSV:", e)
|
||||
|
||||
# Připojení k ActiveMQ s listenerem
|
||||
listener = MyListener()
|
||||
conn = get_connection(listener=listener)
|
||||
conn.subscribe(destination=QUEUE_NAME, id=1, ack="auto")
|
||||
|
||||
# keep process alive
|
||||
while True:
|
||||
time.sleep(1)
|
||||
Reference in New Issue
Block a user