Simple Java sender and receiver

This commit is contained in:
Michael Bridgen 2010-09-21 17:25:01 +01:00
commit dc5b76b9b5
2 changed files with 48 additions and 0 deletions

28
java/recv.java Normal file
View File

@ -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");
}
}
}

20
java/send.java Normal file
View File

@ -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");
}
}
}