Skip to content

Instantly share code, notes, and snippets.

@iconara
Created May 19, 2011 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iconara/980238 to your computer and use it in GitHub Desktop.
Save iconara/980238 to your computer and use it in GitHub Desktop.
RabbitMQ Producer & Consumer
import java.util.*;
import com.rabbitmq.client.*;
public class Consumer {
private static int count = 0;
public static void main(String[] args) throws Exception {
Random random = new Random();
int host = random.nextInt(4) + 1;
System.out.println(String.format("Connecting to rfmmqstaging0%d", host));
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(String.format("rfmmqstaging0%d", host));
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("fragments" + args[0], false, consumer);
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(String.format("%.1f", count/1.0));
count = 0;
}
}, 0, 1000);
while (true) {
QueueingConsumer.Delivery delivery;
delivery = consumer.nextDelivery();
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
count++;
}
}
}
import java.util.*;
import com.rabbitmq.client.*;
public class Producer {
private static int count = 0;
public static void main(String[] args) throws Exception {
Random random = new Random();
int host = random.nextInt(4) + 1;
System.out.println(String.format("Connecting to rfmmqstaging0%d", host));
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(String.format("rfmmqstaging0%d", host));
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("fragments", "topic", false);
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println(String.format("%.1f", count/1.0));
count = 0;
}
}, 0, 1000);
while (true) {
channel.basicPublish("fragments", "f" + random.nextInt(12), null, "hello world".getBytes());
count++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment