// Connect to the AMQP broker
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(hostName);
factory.setPort(portNumber);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// Establish the REQ/REP wiring.
channel.queueDeclare("REQREP", true, false, false, null);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("REQREP", true, consumer);
for (;;) {
// Get next request
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String replyTo = delivery.getProperties().getReplyTo();
String correlationId = delivery.getProperties().getCorrelationId();
BasicProperties props =
(BasicProperties) (delivery.getProperties().clone());
// We must set the correlation ID, because it is used
// by AMQP and 0MQ clients for correlating replies
props.setReplyTo(null);
System.err.println("processing request");
// Send the reply
channel.basicPublish("", replyTo, props, "World!".getBytes());
}
} catch (Exception e) {
System.err.println("Main thread caught exception: " + e);
e.printStackTrace();
System.exit(1);