Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary
Last active December 25, 2015 21:09
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 tomwhoiscontrary/7040230 to your computer and use it in GitHub Desktop.
Save tomwhoiscontrary/7040230 to your computer and use it in GitHub Desktop.
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