I'm using RabbitMQ as an AMQP server. As a library I use the RabbitMQ Client for java. I started to code some examples in Java (Producer and Consumer) and they work very fine. However, the ultimate use case (and the reason why I use amqp) would be a combination of nodejs as a producer and a java process as a consumer.
Okay, so this is the code for nodejs:
var amqp = require('amqp');
var connection = amqp.createConnection({ host: 'localhost', port: 5672 },
{defaultExchangeName: "node_js_exchange"});
connection.on('ready', function() {
connection.publish('node_js_routing_key', 'Hello from NodeJS!');
connection.end();
});
and on the server side I use the following java code:
public class NodeJSServer implements Runnable {
private Connection conn;
private Channel channel;
private QueueingConsumer consumer;
public final static String RPC_QUEUE_NAME = "node_js_queue";
public final static String RPC_EXCHANGE_NAME = "node_js_exchange";
public final static String RPC_ROUTING_KEY = "node_js_routing_key";
@Override
public void run() { try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
conn = factory.newConnection();
channel = conn.createChannel();
channel.exchangeDeclare(RPC_EXCHANGE_NAME, "direct");
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
channel.queueBind(RPC_QUEUE_NAME, RPC_EXCHANGE_NAME, RPC_ROUTING_KEY);
consumer = new QueueingConsumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, true, consumer);
System.out.println("NodeJS Server: Start listening...");
while(true) {
System.out.println("Start waiting...");
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
System.out.println("Message from NodeJS: " + new String(delivery.getBody()));
}
} catch(Exception e) { e.printStackTrace(); } }
}
So as already said: When using a java producer I can sent messages to the NodeJSServer consumer thread without any problems. However, doing the same with node it fails: The message is sent, however the server never recieves the message. Therefore I'm asking myself what I am doing wrong here?
UPDATE: I just checked in the rabbitmq monitoring interface that no message is actually sent by nodejs to the server and the corresponding queue/exchange.