/
Consumer Code Example
Consumer Code Example
This code connects to the UH Message Broker and consumes the messages described in UHIMS Events
- Download the RabbitMQ Java client from http://www.rabbitmq.com/download.html (there should be libraries and examples for other languages there)
- Here is a SampleUhimsConsumer.java you can try.
- Replace the following with your own values:
- yourRabbitMqUsernameHere
- yourRabbitMqPasswordHere
- yourQueueNameHere
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 SampleUhimsConsumer { private final static String queueName = "yourQueueNameHere"; private final static boolean durable = true; private final static boolean exclusive = true; private final static boolean autoDelete = false; private final static boolean autoAck = false; // we will ack each message ourselves private final static long timeout = 2*60*1000; // to exit after 2 minutes without messages public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("esb-test.its.hawaii.edu"); factory.setPort(5671); factory.setVirtualHost("uhims"); factory.setUsername("yourRabbitMqUsernameHere"); factory.setPassword("yourRabbitMqPasswordHere"); 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(); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, autoAck, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(timeout); if (delivery == null) { // stop consumer if no events arrive after timeout milliseconds conn.close(); } else { // Your code processes the message here String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); // If your code fails to acknowledge the message, // it will be redelivered. This is a safety feature. // Refer to Message Acknowledgement section in // http://www.rabbitmq.com/tutorials/tutorial-two-java.html channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } }
- Replace the following with your own values:
- To quickly compile and run the above, you could save the above code as SampleUhimsConsumer.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 SampleUhimsConsumer.java
- java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar SampleUhimsConsumer
- That's it!
See Download page for more complete code examples.
Javadoc at http://www.rabbitmq.com/javadoc/
, multiple selections available,
Related content
Download
More like this
Consuming UHIMS Events
Consuming UHIMS Events
More like this
Producer Code Example
Producer Code Example
More like this
Consuming Messages from the UH Message Broker
Consuming Messages from the UH Message Broker
More like this
UH Message Broker migrating to a cluster
UH Message Broker migrating to a cluster
More like this
UH Message Broker
UH Message Broker
More like this