Created
February 8, 2012 18:36
-
-
Save pablomolnar/1771998 to your computer and use it in GitHub Desktop.
consumer.groovy
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 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