Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary

tomwhoiscontrary/Recv.java

Last active Dec 25, 2015
Embed
What would you like to do?
Demonstrating my inability to write a simple receiver with Bunny's low-level API
import java.io.IOException;
import java.util.concurrent.Executors;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class Recv {
public static void main(String[] args) throws IOException, InterruptedException {
final String queue = args[0];
Connection connection = new ConnectionFactory().newConnection(Executors.newSingleThreadExecutor());
final Channel channel = connection.createChannel();
channel.basicQos(1);
channel.basicConsume(queue, false, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
System.out.println("recv from " + queue + ": " + new String(body));
channel.basicAck(envelope.getDeliveryTag(), false);
channel.basicCancel(consumerTag);
}
});
Thread.sleep(1000);
connection.close();
}
}
#! /usr/bin/ruby
require "rubygems"
require "bunny"
queue = ARGV.shift()
conn = Bunny.new()
conn.start()
ch = conn.create_channel()
ch.prefetch(1)
ch.basic_consume(queue, ch.generate_consumer_tag(), true) do |delivery_info, properties, message|
puts "recv from #{queue}: #{message}"
ch.ack(delivery_info.delivery_tag, false)
ch.basic_cancel(delivery_info.consumer_tag)
end
sleep(1)
conn.close()
#! /usr/bin/ruby
require "rubygems"
require "bunny"
queue = ARGV.shift()
message = ARGV.shift()
conn = Bunny.new()
conn.start()
ch = conn.create_channel()
ch.basic_publish(message, nil, queue)
puts "send to #{queue}: #{message}"
conn.close()
#! /bin/bash -eu
QUEUE_NAME=$(basename $(mktemp -u))
echo -n "declare $QUEUE_NAME: "
rabbitmqadmin declare queue name=$QUEUE_NAME
print_queue_size() {
sleep 2
echo -n "messages in $QUEUE_NAME:"
rabbitmqadmin list queues | fgrep $QUEUE_NAME | cut -d \| -f 11
}
for I in 1 2 3 4 5
do
"$(dirname "$0")"/send.rb $QUEUE_NAME "this is message $I"
done
print_queue_size
"$(dirname "$0")"/recv.rb $QUEUE_NAME
print_queue_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.