The simplicity of the whole integration blew me away. Under load the program performed marvelously. If it is load enough for you, you can decide for yourself. I wrote the same code twice today. First on a Ubuntu 12.04 machine and later on a Windows 7 machine. All components running locally. Code – identical. Setup – identical.
Problem:
Send a million Strings from a java program running in its own process to a Node.js program written in Java Script.
Step 1:
Install and configure RabbitMQ server. This is straight forward. Documentation is clear on this at http://www.rabbitmq.com/configure.html. I did not set any specific configuration parameters other than defining ERLANG_HOME as a system variable in Windows 7 machine. On the Linux environment apt-get install took care of configuring Erlang correctly. If you are wondering why Erlang is required – Rabbit MQ needs Erlang runtime for execution. It also needs Python for running its very useful web console.
Once installed and configured you will be able to navigate to http://localhost:15672 to access the management console. http://www.rabbitmq.com/management.html
Start RabbitMQ.
Install Node.js if you do not have it installed.
Java Program
I used the latest AMQP client dependency through maven to get things going.
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.0.1</version> </dependency>
Java code involved creating a ConnectionFactory pointing at the server running RabbitMQ, Creating a Channel to publish messages to the queue I want and lastly steps to release resources. These are exact steps that one would use with JMS also.
ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection con = factory.newConnection(); Channel channel = con.createChannel(); final String message = "Hello Rabbit"; for (int i = 0 ; i < 1000 * 1000 ; i++){ channel.basicPublish("", "java2nodeQueue", false,false,null, (message + i).getBytes()); } channel.close(); con.close(); JavaScript (Node.js) part var express = require("express"); var amqp = require("amqp"); var app = express(); var PORT = 3000; var connection = amqp.createConnection({host: 'localhost'}); app.listen(PORT); connection.addListener('ready', function () { console.log("connected to " + connection.serverProperties.product); var q = connection.queue('java2nodeQueue'); q.subscribe(function (m) { console.log(m.data.toString()); }); }); console.log("HTTP Server started at PORT:" + PORT + "...");
JavaScript (Node.js) part
var express = require("express"); var amqp = require("amqp"); var app = express(); var PORT = 3000; var connection = amqp.createConnection({host: 'localhost'}); app.listen(PORT); connection.addListener('ready', function () { console.log("connected to " + connection.serverProperties.product); var q = connection.queue('java2nodeQueue'); q.subscribe(function (m) { console.log(m.data.toString()); }); }); console.log("HTTP Server started at PORT:" + PORT + "...");
It is a good idea to use npm to install required modules into the Node.js project.
npm install express npm install amqp
Once that is done you can run the listener using command
rabbitmq-pocjs>node server.js HTTP Server started at PORT:3000... connected to RabbitMQ
Here you have both connected to RabbitMQ and have created a non-durable queue ‘java2nodeQueue’. Very cool!
Next run the java code to pump in messages (here for simplicity sake I am sending only 10 messages but will share the million string result at the end)
------------------------------------------------------- T E S T S ------------------------------------------------------- Running TestSendReceiveMessages Sent message :Hello Rabbit0 Sent message :Hello Rabbit1 Sent message :Hello Rabbit2 Sent message :Hello Rabbit3 Sent message :Hello Rabbit4 Sent message :Hello Rabbit5 Sent message :Hello Rabbit6 Sent message :Hello Rabbit7 Sent message :Hello Rabbit8 Sent message :Hello Rabbit9 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.147 sec
On the other console where the node is running server.js, the received messages are print out simultaneously.
On the RabbitMQ management console I see some good stats on my on-durable queue also. Note that this queue will disappear the moment I disconnect my Node server. That is pretty neat too!
When I pumped the million messages, I am only interested in the message through put rates. These are the statistics.
While Java Pumped in messages at around 27500 messages per second , RabbitMQ was able to manage deliveries into Node.js at around 7000 messages per second. These statistics are from the Windows 7 machine. All around it is pretty impressive for both node.js and RabbitMQ. Performance comparison for the same program on Linux machine with identical configurations proved that RabbitMQ and Node together works about three times as fast compared to Windows 7. On Linux RabbitMQ was able to deliver around 20,000 messages / second to the the Node.js receiver. Even the Java message pump program performed considerably better (41000 messages / s) on Linux machine.
Reblogged this on rg443blog and commented:
Messaging, Java, Node.js, RabbitMQ
Awesome article. I was looking to do some Java to node.js messaging so the code samples especially were great.