Ваш скрипт работает отлично.Я отправил сообщение в очередь с именем simple_queue, используя обмен ProgressNotification
, и ваш сценарий был напечатан.
b'Hello World! '
Я использовал этот сценарий на основев моей собственной библиотеке RabbitMQ, но вы можете просто использовать этот пример pika в качестве ссылки.
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')
В Java вам нужно будет опубликовать ваше сообщение следующим образом.
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 + "'");
}
}
}