Created
May 19, 2011 05:22
-
-
Save iconara/980238 to your computer and use it in GitHub Desktop.
RabbitMQ Producer & Consumer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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