Producer Code Example

  1. Download the RabbitMQ Java client from http://www.rabbitmq.com/download.html (there should be libraries and examples for other languages there)
  2. Here is a SampleProducer.java you can try.
    • Replace the following with your own values:
      • yourVhostNameHere
      • yourExchangeNameHere
      • yourRabbitMqUsernameHere
      • yourRabbitMqPasswordHere
      • esb-test.its.hawaii.edu with esb.hawaii.edu when your code is ready to use the production RabbitMQ service.

        import java.io.*;
        import java.security.*;
        import com.rabbitmq.client.*;
        
        public class SampleProducer {
        
            private final static  String exchangeName = "yourExchangeNameHere";
            private final static  String vhostName    = "yourVhostNameHere";
            private final static  String username     = "yourRabbitMqUsernameHere";
            private final static  String password     = "yourRabbitMqPasswordHere";
        
            private final static boolean durable       = true;
            private final static boolean exclusive     = true;
            private final static boolean autoDelete    = false;
            private final static boolean autoAck       = true;
        
            public static void main(String[] argv) throws Exception {
        
            ConnectionFactory factory = new ConnectionFactory();
        
            factory.setHost("esb-test.its.hawaii.edu");
            factory.setPort(5671);
            factory.setVirtualHost(vhostName);
            factory.setUsername(username);
            factory.setPassword(password);
        
            factory.useSslProtocol(); // Tells the library to setup the default Key and Trust managers for you
                                      // which do not do any form of remote server trust verification
        
            Connection conn       = factory.newConnection();
            Channel    channel    = conn.createChannel();
        
            String message    = "<registration> <action>add</action> <courseID>MAN_MATH_101</courseID> <studentID>12345678</studentID> </registration>";
            String routingKey = "registration.add.MAN";
            channel.basicPublish(exchangeName, routingKey, new AMQP.BasicProperties.Builder().deliveryMode(2), message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
        
            channel.close();
            conn.close();
        
          }
        }
        
  3. To quickly compile and run the above, you could save the above code as SampleProducer.java in the same directory where you downloaded the above Java client (we tried it with rabbitmq-java-client-bin-2.7.1):
    • javac -cp rabbitmq-client.jar SampleProducer.java
    • java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar SampleProducer
  4. That's it!

(info) See Download page for more complete code examples.
(info) Javadoc at http://www.rabbitmq.com/javadoc/