0

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?

Jerry Bi
  • 321
  • 6
  • 24
  • It is a bit hard to understand what are you trying to accomplish here. Just pass the filename to the worker and let it `os.rename(filename, new_filename)`. – Paulo Scardine Nov 28 '17 at 19:28
  • It's not entirely clear to me what you are trying to do. As a general rule, message brokers are not to be used for file transfer. Indeed, any message larger than around 100K tends to gum things up. So- is this a message broker problem, or a Python problem? My thought: create messages pointing to the location of the files, and then your consumers perform the task directed at them via the messages. – theMayer Nov 28 '17 at 19:29
  • I need to use RabbitMQ and Kombu. I just want the producer to be able to send a file to the queue and the consumer to receive the file from the queue and then move it to a directory. I know about the rename function but that doesn't help when the source is from the RabbitMQ database. – Jerry Bi Nov 28 '17 at 19:38
  • RabbitMQ isn't a database - it's a message queue. Very different. – dsolimano Nov 28 '17 at 22:57
  • Then how do I get the file that was sent as a message, extract it, and then send it to a directory? – Jerry Bi Nov 29 '17 at 15:35

0 Answers0