Info
I love it...
After years of working with CORBA and not really seeing anything much on the market to replace, I was forced to look at an alternative the WebServices using a ReSTful api called Jersey - absolute nightmare to work it. It turns a simple task of marshalling complex objects across the wire in an Eaton Mess. It's a mishmash of code that just doesn't work properly, is hard to debug and test.
I've been familiar with MOM based technologies since the 1990s (DDE, JMS, MS-MQ, MQ-Series etc). I had heard about RabbitMQ some time ago but never actually used. So when I decided to get my teeth into, I was shocked how easy it was to deploy and work. It's elegant and easy to work with.
I'm working with the pub/sub model. The examples given in the RabbitMQ documentation pretty much outline the code as follows
import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; public class RabbitMqLogsSender { private static String EXCHANGE_NAME = "logs"; // specify as ReSTful URI param private static final String HOST_ID = "192.168.99.100"; // put in a config file public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_ID); // put this in a config file Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT); String message = argv[0]; channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
import com.rabbitmq.client.*; import java.io.IOException; public class RabbitMqLogsReceiver { private static String EXCHANGE_NAME = "logs"; private static final String HOST_ID = "192.168.99.100"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST_ID); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, ""); // Once a channel has been created, yu can send messages to yourself on it... String message = "Sending a message to myself to start"; channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8")); System.out.println(" [*] Waiting for messages on: " + queueName + ". To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); } }