Skip to content

Instantly share code, notes, and snippets.

@pablomolnar
Created February 8, 2012 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pablomolnar/1771998 to your computer and use it in GitHub Desktop.
Save pablomolnar/1771998 to your computer and use it in GitHub Desktop.
consumer.groovy
import com.rabbitmq.client.*
// Annotate the import is not working :S
@Grab(group='com.rabbitmq', module='amqp-client', version='2.7.1')
class Dummy {}
try{
// Get rabbitmq config
def config = new ConfigSlurper().parse(new File('../rabbitmq.properties').toURL())
// Connect
def connectionFactory = new ConnectionFactory([username: config.username, password: config.password, virtualHost: config.virtualHost])
def connection = connectionFactory.newConnection(Address.parseAddresses(config.addresses))
def channel = connection.createChannel()
// Declare
def queueName = channel.queueDeclare().getQueue()
channel.queueBind(queueName, 'items_feed_ex', '')
// Consume
QueueingConsumer consumer = new QueueingConsumer(channel)
channel.basicConsume(queueName, false, consumer)
while(true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery()
def msg = new String(delivery.body)
channel.basicAck(delivery.envelope.deliveryTag, false)
println msg
}
}
catch(e){ e.printStackTrace() }
finally {
channel.close()
connection.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment