From dc5b76b9b5312f133c89fa7bcd892d9529ce395c Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 21 Sep 2010 17:25:01 +0100 Subject: [PATCH] Simple Java sender and receiver --- java/recv.java | 28 ++++++++++++++++++++++++++++ java/send.java | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 java/recv.java create mode 100644 java/send.java diff --git a/java/recv.java b/java/recv.java new file mode 100644 index 0000000..7b764ac --- /dev/null +++ b/java/recv.java @@ -0,0 +1,28 @@ +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.QueueingConsumer; + +import java.io.IOException; + +public class recv { + public static void main(String[] argv) { + try { + Connection conn = new ConnectionFactory().newConnection(); + Channel chan = conn.createChannel(); + chan.queueDeclare("hello", false, false, false, null); + QueueingConsumer consumer = new QueueingConsumer(chan); + chan.basicConsume("hello", true, consumer); + while (true) { + QueueingConsumer.Delivery delivery = consumer.nextDelivery(); + System.out.println(new String(delivery.getBody())); + } + } + catch (IOException ioe) { + System.err.println("IOException while consuming"); + } + catch (InterruptedException ie) { + System.err.println("InterruptedException while consuming"); + } + } +} diff --git a/java/send.java b/java/send.java new file mode 100644 index 0000000..d24bb7b --- /dev/null +++ b/java/send.java @@ -0,0 +1,20 @@ +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.Channel; + +import java.io.IOException; + +public class send { + public static void main(String[] argv) { + try { + Connection conn = new ConnectionFactory().newConnection(); + Channel chan = conn.createChannel(); + chan.queueDeclare("hello", false, false, false, null); + chan.basicPublish("", "hello", null, "Hello World!".getBytes()); + conn.close(); + } + catch (IOException ioe) { + System.err.println("IOException while publishing"); + } + } +}