I want to create several Python programs that use RabbitMQ and Kombu. The programs allow the producer to read pdf files and send them to the RabbitMQ database where the consumer can receive them and move them to a certain directory on their computer. Here is the producer code I have so far:
producer.py
def kombu_producer(fileName, KEY):
with open(fileName, mode='rb') as file:
fileContent = file.read()
connection_string = 'amqp://guest:guest@localhost:5672//'
with kombu.Connection(connection_string) as producer_connection:
channel = producer_connection.channel()
exchange = kombu.Exchange("ExampleExchange", type="fanout")
producer = kombu.Producer(exchange=exchange, channel=channel, routing_key=KEY)
queue = kombu.Queue(name="Example-Queue", exchange=exchange, routing_key=KEY)
queue.maybe_bind(producer_connection)
queue.declare()
producer.publish(fileContent)
producer_connection.release()
file.close()
Here is the unfinished consumer code I have:
consumer.py
def kombu_consumer(KEY):
connection_string = 'amqp://guest:guest@localhost:5672//'
with kombu.Connection(connection_string) as consumer_connection:
exchange = kombu.Exchange("ExampleExchange", type="fanout")
queue = kombu.Queue(name="Example-Queue", exchange=exchange, routing_key=KEY)
I still haven't created the consumer or the callback function yet, but I'm currently at a loss. I'm trying to figure out how to code it so that the pdf file will move to a certain directory. For example let's say the producer sends a file called "example.pdf" to RabbitMQ. I want the consumer to receive the file and move it to the directory "C:\Documents\ConsumerDirectory\" with the name of the file intact. I feel that I need to do something with the callback function (def process_file(body, message)) and I either need to use the body or the message, but I'm not sure what. Do I perhaps need to do something with the producer.py? Thanks.
Edit: actually, perhaps I'm asking the wrong question. Since the pdf file is read in the producer and turned into a message, how do I turn turn the message back into a pdf file with the same formatting and name on the consumer side?