简体   繁体   中英

Java/Python Communication through RabbitMQ

I want to develop a messaging application using java as producer and python as consumer of the message through RabbitMQ. Please suggest how can I do this. Thanks for your suggestions and comments.

Let me read the documentation for you.

reciever.py

import pika

def callback(ch, method, properties, body):
    print(body)

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='some_queue')

channel.basic_consume(callback,
                      queue='some_queue',
                      no_ack=True)

channel.start_consuming()

sender.java

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class Send {
   private final static String QUEUE_NAME = "some_queue";

   public static void main(String[] argv) throws java.io.IOException {
      ConnectionFactory factory = new ConnectionFactory();
      factory.setHost("localhost");
      Connection connection = factory.newConnection();
      Channel channel = connection.createChannel();
      channel.queueDeclare(QUEUE_NAME, false, false, false, null);

      String message = "Hello World!";
      channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

      channel.close();
      connection.close();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM