I'm trying to get notifications from a RabbitMQ
residing on a server. I've been told to use this code, which should print progress notifications. But when running the code and submitting a job to the queue, I'm not seeing anything. The code doesn't print anything:
import pika
rabbitMqHost = 'host'
rabbitMqUser = 'user'
rabbitMqPass = 'password'
exchangeName = 'ProgressNotification'
credentials = pika.PlainCredentials(rabbitMqUser, rabbitMqPass)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitMqHost, 5672, '/', credentials))
channel = connection.channel()
# channel.exchange_delete(exchange=exchangeName)
# exit(3)
channel.exchange_declare(exchange=exchangeName, exchange_type='fanout')
result = channel.queue_declare()
queue_name = result.method.queue
channel.queue_bind(exchange=exchangeName,
queue=queue_name)
def callback(ch, method, properties, body):
print("> %r" % (body,))
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
Sorry, I'm very new to RabbitMQ
. But is there another step or something missing?! Why it doesn't show anything?
Your script works fine. I pushed a message to a queue called simple_queue using the exchange ProgressNotification
and your script printed.
b'Hello World!'
I used this script, based on my own RabbitMQ library, but you can just use this pika example as a reference.
from amqpstorm import Connection
from amqpstorm import Message
with Connection('127.0.0.1', 'guest', 'guest') as connection:
with connection.channel() as channel:
# Declare the Queue, 'simple_queue'.
channel.queue.declare('simple_queue')
# Create the message.
message = Message.create(channel, 'Hello World!')
# Publish the message to a queue called, 'simple_queue'.
message.publish('simple_queue', exchange='ProgressNotification')
In Java you would need to publish your message like this.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Send {
private final static String QUEUE_NAME = "simple_queue";
private final static String EXCHANGE_NAME = "ProgressNotification";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + message + "'");
}
}
}