Created
October 8, 2014 03:01
-
-
Save mastfish/8a834ed3c642b195035d to your computer and use it in GitHub Desktop.
Setting up SNS and SQS
This file contains hidden or 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
# Credentials | |
AWS.config(access_key_id: ENV['AWS_KEY_ID'], secret_access_key: ENV['AWS_ACCESS_KEY']) | |
# Get the queue | |
sqs = AWS::SQS.new | |
queue = sqs.queues.create('search-service-uat') | |
# This will run forever, listening for messages and processing them | |
queue.poll do |message| | |
event = Hashie::Mash.new(JSON.parse(message.body)) | |
if event.type == 'product_updated' | |
puts "Product #{event.product.id} was updated! Check it out at #{event.product.uri}" | |
else | |
raise "I don't know what do to do with #{event.type}" | |
end | |
rescue => e | |
log e | |
end |
This file contains hidden or 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
# Credentials | |
AWS.config(access_key_id: ENV['AWS_KEY_ID'], secret_access_key: ENV['AWS_ACCESS_KEY']) | |
# Get the topic | |
sns = AWS::SNS.new | |
# Create looks weird, right? | |
# It should really be called "create_or_find_by_name" | |
topic = sns.topics.create('product-service-uat') | |
update_message = { | |
sender: 'product-service-uat', | |
type: 'product_updated', | |
data: { | |
product: { | |
id: 12, | |
uri: "http://www.westfield.com.au/api/product/master/products/12.json" | |
} | |
}, | |
} | |
# Send the message | |
topic.publish update_message.to_json | |
deleted_message = { | |
sender: 'product-service-uat', | |
type: 'product_deleted', | |
data: { | |
product: { | |
id: 12, | |
# It's polite to keep a consistent format | |
uri: "" | |
} | |
} | |
} | |
# Send the message | |
topic.publish deleted_message.to_json |
This file contains hidden or 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
# Credentials | |
AWS.config(access_key_id: ENV['AWS_KEY_ID'], secret_access_key: ENV['AWS_ACCESS_KEY']) | |
# Create a topic (or get an existing one with name=product-service-uat) | |
sns = AWS::SNS.new | |
topic = sns.topics.create('product-service-uat') | |
puts "Got topic: #{topic.name}" | |
# Create a queue (or get an existing one with name=search-service-uat) | |
sqs = AWS::SQS.new | |
queue = sqs.queues.create('search-service-uat') | |
puts "Got queue #{queue.url}" | |
# Set up a subscription | |
subscription = topic.subscribe(queue) | |
# Amazon puts a whole pile of cruft in the message by default | |
subscription.raw_message_delivery = true | |
puts "#{subscription.endpoint} is listening to #{subscription.topic.name}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment