<reponame>RomPiot/BachelorDIM-Lectures-Algorithms-2019<filename>simple_queue_read.py

import os
import pika
import time
import key_rabbitMQ
import argparse

# the key to connect to cloudamqp (not commited)
amqp_url = key_rabbitMQ.key

# Parse CLODUAMQP_URL (fallback to localhost)
url = os.environ.get('CLOUDAMQP_URL', amqp_url)
params = pika.URLParameters(url)
params.socket_timeout = 5

connection = pika.BlockingConnection(params)  # Connect to CloudAMQP
channel = connection.channel()
queue = channel.queue_declare(queue='presentation')

nb = 0


def readMessage(concurrency=False, sleep=False):
    ##
    # Function that read new messages from presentation queue
    # @concurrency = to activate message acknowledging
    #
    if (concurrency == True):
        print("Concurrency mode Activated.")

    def callback(ch, method, properties, body):
		##
    	# Function that callback continuously called 
    	#
        global nb
        print(" [x] Received %r" % body)
        #print("There is", queue.method.messages_count, "message(s).")
        nb = nb + 1

        if (concurrency == True):
            ch.basic_ack(delivery_tag=method.delivery_tag)

        print(nb, "message(s) read.")

    def callback_sleep(ch, method, properties, body):
		##
    	# Function that callback continuously called when arg sleep is true
    	#
        global nb
        print(" [x] Received %r" % body)
        #print("There is", queue.method.messages_count, "message(s).")
        nb = nb + 1
        time.sleep(1)

        if (concurrency == True):
            ch.basic_ack(delivery_tag=method.delivery_tag)

        print(nb, "message(s) read.")

    if (sleep == True):
        channel.basic_consume(
            queue='presentation', on_message_callback=callback_sleep, auto_ack=True)
    else:
        channel.basic_consume(
            queue='presentation', on_message_callback=callback, auto_ack=True)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

import os
import pika
import time
import key_rabbitMQ
import argparse

# the key to connect to cloudamqp (not commited)
amqp_url = key_rabbitMQ.key

# Parse CLODUAMQP_URL (fallback to localhost)
url = os.environ.get('CLOUDAMQP_URL', amqp_url)
params = pika.URLParameters(url)
params.socket_timeout = 5

connection = pika.BlockingConnection(params)  # Connect to CloudAMQP
channel = connection.channel()
queue = channel.queue_declare(queue='presentation')

nb = 0


def readMessage(concurrency=False, sleep=False):
    ##
    # Function that read new messages from presentation queue
    # @concurrency = to activate message acknowledging
    #
    if (concurrency == True):
        print("Concurrency mode Activated.")

    def callback(ch, method, properties, body):
		##
    	# Function that callback continuously called 
    	#
        global nb
        print(" [x] Received %r" % body)
        #print("There is", queue.method.messages_count, "message(s).")
        nb = nb + 1

        if (concurrency == True):
            ch.basic_ack(delivery_tag=method.delivery_tag)

        print(nb, "message(s) read.")

    def callback_sleep(ch, method, properties, body):
		##
    	# Function that callback continuously called when arg sleep is true
    	#
        global nb
        print(" [x] Received %r" % body)
        #print("There is", queue.method.messages_count, "message(s).")
        nb = nb + 1
        time.sleep(1)

        if (concurrency == True):
            ch.basic_ack(delivery_tag=method.delivery_tag)

        print(nb, "message(s) read.")

    if (sleep == True):
        channel.basic_consume(
            queue='presentation', on_message_callback=callback_sleep, auto_ack=True)
    else:
        channel.basic_consume(
            queue='presentation', on_message_callback=callback, auto_ack=True)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
# the key to connect to cloudamqp (not commited) # Parse CLODUAMQP_URL (fallback to localhost) # Connect to CloudAMQP ## # Function that read new messages from presentation queue # @concurrency = to activate message acknowledging # ## # Function that callback continuously called # #print("There is", queue.method.messages_count, "message(s).") ## # Function that callback continuously called when arg sleep is true # #print("There is", queue.method.messages_count, "message(s).")