Skip to content

Instantly share code, notes, and snippets.

@KevinGreene
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save KevinGreene/8405f28b1bc7d7037d4d to your computer and use it in GitHub Desktop.

Select an option

Save KevinGreene/8405f28b1bc7d7037d4d to your computer and use it in GitHub Desktop.
RabbitMQ Expire from NodeJS
amqp = require 'amqp'
_ = require 'underscore'
console.log 'creating connection'
connection = amqp.createConnection
host: 'rabbitmq.local'
connection.addListener 'error', (e) -> throw e
connection.addListener 'close', (e) ->
console.log "connection closed"
connection.on 'ready', () ->
console.log 'connection ready'
exchangeOptions =
type: 'fanout'
connection.exchange 'timeout-exchange', exchangeOptions, (timeoutExchange) ->
connection.queue "timeout-queue", {}, (queue) ->
queue.bind timeoutExchange, "consumer-timeout"
queue.subscribe (msg) ->
console.log "timeout received message: #{msg.data.toString('ascii',0,msg.data.length)}"
onQueueOpen = (exchange, i, queue) ->
queue.bind(exchange, "consumer-#{i}")
# queue.subscribe (msg) ->
# console.log "Consumer #{i} received message: #{msg.data.toString('ascii',0,msg.data.length)}"
onExchangeOpen = (exchange) ->
console.log "Exchange open"
for i in [1..10]
queueOptions =
'arguments':
'x-dead-letter-exchange': "timeout-exchange",
'x-dead-letter-routing-key': "consumer-timeout",
'x-message-ttl': 3000,
'x-expires': 100000
console.log "Adding subscription for hello consumer #{i}"
queue = connection.queue "queue-t-#{i}", queueOptions, _.partial(onQueueOpen, exchange, i)
doPublish = () ->
console.log "Publishing hello world"
exchange.publish "hello-message", "Hello world",
contentType: 'text/plain'
setTimeout doPublish, 1000
connection.exchange 'test-exchange', exchangeOptions, onExchangeOpen
setTimeout connection.destroy, 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment