Sending mail asynchronously by using Amazon SQS and ActiveMessaging

From our application we send mail to our users in many cases. Sending mail synchronously slows down the whole process. So to improve our application’s performance we feel the need of sending mail asynchronously. And I have done it using the Amazon SQS and ActiveMessaging.

What is Amazon SQS:

Amazon Simple Queue Service (Amazon SQS) offers a reliable, highly scalable, hosted queue for storing messages as they travel between computers.
Developers can push message to the queue any time and get the message when required. They can create an unlimited number of Amazon SQS queues, each of which can send and receive an unlimited number of messages. The message body can contain up to 8 KB of text in any format. A message is "locked" while a computer is processing it Messages can be retained in queues for up to 4 days

What is ActiveMessaging:

ActiveMessaging is a Rails plug-in framework, which simplifies messaging integration. It is a generic framework to ease using messaging, but is not tied to any particular messaging system - in fact, it now has support for Stomp, JMS (using Stomp Connect or direct on JRuby), WebSphere MQ, Amazon Simple Queue Service (SQS), and the all Ruby ReliableMessaging. With ActiveMessaging and Rails now you can loosely integrate with systems as disparate as mainframes sending MQ messages or J2EE webapps, offload processing long-running tasks, or create applications with event or message-driven architectures.

To integrate ActiveMessagin with your apllication
you have to install the following gem and pugin

gem install daemons
ruby script/plugin installhttp://activemessaging.googlecode.com/svn/trunk/plugins/activemessaging

First create a process using the following command
script/generate processor message
Running this generate for the first time outputs the following:

create app/processors
create app/processors/message_processor.rb
create config/messaging.rb
create config/broker.yml
create app/processors/application.rb
create script/poller

Now configure the broker.yml as which broker you are using.
I have used Amazon SQS as a broker. Here is my configure file

development:
adapter: asqs
access_key_id: xxxxxxxxxxxxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

test:
adapter: asqs
access_key_id: xxxxxxxxxxxxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

production:
adapter: asqs
access_key_id: xxxxxxxxxxxxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Define your own queue by changing the config/messaging.rb file

s.destination :queue_name, ' queue_name

Add the following to the controller from where you push message to the queue


require 'activemessaging/processor'
include ActiveMessaging::MessageSender
publishes_to : queue_name

def method_from_where_push_message_to_queue
publish : scrumpadmailer, “put the required value to process the message"
end

Add the following to the app/processors/message_processor.rb


subscribes_to : queue_name
def on_message(message)
begin
send mail by processing the message
rescue
publish : scrumpadmailer, “put the received message"
end
end

Now run the poller which monitors the queue continuously and upon receiving a message it calls the on_message method of app/processors/message_processor.rb

Go to your application directory and run the following command
ruby script/poller run

To run the poller as a background process in linux run the following command
nohup ruby script/poller run >> mail_logger.txt &


Everything is done. Now when a message is pushed in the queue the on_message method
Is triggered and mail will be sent

2 comments:

Anonymous June 19, 2008 at 12:51 PM  

Nice post Ratul!

Rubaiat September 16, 2008 at 11:35 PM  

Thanks, that was really helpful. :)

Total Pageviews

Tags

Twitter Updates
    follow me on Twitter

    Followers