Skip to content

Instantly share code, notes, and snippets.

@mpassell
Created September 28, 2012 20:35
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 mpassell/3801945 to your computer and use it in GitHub Desktop.
Save mpassell/3801945 to your computer and use it in GitHub Desktop.
Updated version of example from Andrey Paramonov's blog post - http://ndpar.blogspot.com/2010/03/get-started-with-rabbitmq.html
import com.rabbitmq.client.*
@Grab(group='com.rabbitmq', module='amqp-client', version='2.8.7')
factory = new ConnectionFactory([username: 'guest', password: 'guest',
virtualHost: '/', requestedHeartbeat: 0])
conn = factory.newConnection(new Address('localhost', 5672))
channel = conn.createChannel()
exchangeName = 'myExchange'
queueName = 'myQueue'
channel.exchangeDeclare exchangeName, 'direct'
channel.queueDeclare(queueName, false, true, true, [:])
channel.queueBind queueName, exchangeName, 'myRoutingKey'
def consumer = new QueueingConsumer(channel)
channel.basicConsume queueName, false, consumer
while (true) {
delivery = consumer.nextDelivery()
println "Received message: ${new String(delivery.body)}"
channel.basicAck delivery.envelope.deliveryTag, false
}
channel.close()
conn.close()
import com.rabbitmq.client.*
@Grab(group='com.rabbitmq', module='amqp-client', version='2.8.7')
factory = new ConnectionFactory([username: 'guest', password: 'guest',
virtualHost: '/', requestedHeartbeat: 0])
conn = factory.newConnection(new Address('localhost', 5672))
channel = conn.createChannel()
channel.basicPublish 'myExchange', 'myRoutingKey', null, "Hello, world!".bytes
channel.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment