Aug 18

Amazon Simple Queue Service (Amazon SQS) is a distributed queue messaging service. The idea of SQS is to remove the direct associations between producer and consumer and act as mediator between them.

e.g

Consider that you have a large  application like websites monitoring which involve many stages like

  • Downloading a website
  • Processing the downloaded website
  • Generating report of the above processing

So instead of clubbing the above three one can just split them on their specific need(based on the work they do)  with each doing their respective task and on completion notify the other about it completion.

The traditional approach (called as Client-Server architecture) is to send the notification using a simple HTTPREST , SOAP etc request. But it too has some disadvantage as it require extra care and  precision  to handle all possible response e.g Time-out,Server down etc.

One can save this time and effort my using SQS.

e.g Instead of sending the direct request to Server one can use to SQS to interact with Server/Client an can eventually cut down some of drawback of traditional architecture.

Setting up Amazon SQS in Ruby: There are various library available for using Amazon SQS in Ruby (e.g right_aws, sqs)

Pre-requitise:

  • aws-access-key-id
  • aws-secret-access-key
  • ruby-library(right_aws or sqs gem)
    both of which is provide by the Amazon (or you can ask for incase) during the process of account-registration

In your irb console

require "rubygems"

require "right_aws"

aws_access_key_id  = "Your access key id"

aws_secret_access_key = "Your secret access Key"

sqs = RightAws::SqsGen2.new(aws_access_key_id,aws_secret_access_key)

This basically initialize the sqs object for you to work on.

One can create a new queue just by a single method.

 queue(queue_name, create=true, visibility=nil) 

carefully look at parameter supplied.


queue_name => name of  queue.

create => true (create a queue if it does not exist (optional)).

visibility => set visibility (which make the message reappear after a specified time if not  delete from queue).

One key point about visibility is that one can’t set visibility more than 7200 sec for a  queue or a message .But you can set so from RightScale (Cloud Computing management Platform).

e.g

my_queue = queue('queue_1')

just look at few other methods.

1. send_message or push :- Send message to the specified queue.

 
 my_queue.send_message "This is my first message"

2. receive or pop :- Receive message from the specified queue.

 
 message_received = my_queue.receive

puts message_received.body
=> "This is my first message"

2.1 receive_messages : – There is also a method called receive_messages to receive an  array of messages but it never returned me more than one message so if it work for you  it fine then syntax is like this

 
 receive_messages(number_of_message=1,visibility=nil)

 my_queue.receive_messages(2,60) => return and array of messages

3. delete : –  Delete a  message or a queue

  
 my_queue.delete => "delete the my_queue for SQS"
 
 message_received.delete => "delete the message from the intended queue"

4. size :- Specify the no of message in the queue

 
 my_queue.size => 4 ("4" message in my_queue)

5. name :- Specify name of the queue

  
 my_queue.name => "queue_1" (Name of the queue)

Note : – There is delay lag in all request and response sent/received to/from  SQS server don’t assume to work it instantaneous approximately of around 45 sec -1 minute(can be more)

e.g

If you send  a message to the queue don’t assume it to appear instantaneously in the queue

Pricing Model => $0.10 per 100,000 requests.

Message Size => 64 KB

For more information on Pricing and message size click  here.

Thank you

Tagged with:
Jul 22

Lately there have been too many people wanting to know how to execute Javascript in Celerity.
Let me show you how it done.
First a quick note on JRuby installation.
Here are few useful links to install JRuby under

    1. Linux(ubuntu) should work for other linux packages as well.
    2. Windows.

Just to confirm,kindly type jruby -v on terminal or cmd depending on OS you are using and you should get the version of jruby you are running.

Mine output was

jruby -v
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2010-03-11 6586) (OpenJDK Client VM 1.6.0_0) [i386-java]

Now go ahead and just install celerity gem.
More about celerity installation can be found here also just quickly check whether the sample code work for you.If it work then your ready go and for those it don’t well you just need to tweak a bit.

Ok enough of installation,now we start with running javascript under celerity.
Here is my test HTML File “index.html.”

 <html>
  <head>
    <script type=”text/javascript”>
      function populateDropDown() {
        var select = document.getElementById(‘colors’);
        var options = [‘Ruby’, ‘JRuby’, ‘IronRuby’, ‘Rails’];
        var index;
        for(index = 0; index < options.length; index++) {
          var option = document.createElement(‘option’);
          option.appendChild(document.createTextNode(options[index]));
          option.value = options[index];
          select.appendChild(option);
        }
     }
  </script>
  <script type=”text/javascript”>
    function add_new_language() {
      var wrapper = document.getElementById(‘wrapper’);
      var input = document.createElement(‘input’);
      var br = document.createElement(‘br’);
      input.setAttribute(‘type’,’text’);
      wrapper.appendChild(br);
      wrapper.appendChild(input);
    }
  </script>
 </head>
 <body onload=”populateDropDown()”>
  <h1>Select Your Favourite Language</h1>
  <form>
    <select id=”colors”>
    </select>   <a href=”#” onclick=”add_new_language()” >More Language</a>
    <br/>
    <span id=”wrapper”>
    </span>
  </form>
 </body>
</html>

Here my sample code in JRuby.

 require "rubygems"
 require "celerity"

 browser = Celerity::Browser.new(:browser => :firefox)
 browser.goto("file:///home/guest/Desktop/index.html")

Now basically one can run the javascript just by a simply calling the method fire_event on the html tag that has the associated event that you want to fire.
 e.g the anchor tag above has a onclick event associated with it.

 
browser.document.get_html_elements_by_tag_name('a').first.fire_event('click')
 

Now modified the html content i.e (after javascript execution) can be obtain using “browser.xml”.

Here mine output.

 
  puts browser.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<html>
 <head>
  <script type=”text/javascript”>
   //<![CDATA[
     function populateDropDown() {
       var select =        document.getElementById(‘colors’);
       var options = [‘Ruby’, ‘JRuby’, ‘IronRuby’, ‘Rails’];
       var index;
       for(index = 0; index < options.length; index++) {
         var option = document.createElement(‘option’);
         option.appendChild(document.createTextNode(options[index]));
         option.value = options[index];
         select.appendChild(option);
       }
     }
  //]]>
  </script>

  <script type=”text/javascript”>
   //<![CDATA[
     function add_new_language() {
       var wrapper =document.getElementById(‘wrapper’);
       var input = document.createElement(‘input’);
       var br = document.createElement(‘br’);
       input.setAttribute(‘type’,’text’);
       wrapper.appendChild(br);
       wrapper.appendChild(input);
     }
   //]]>
  </script>
 </head>

 <body onload=”populateDropDown()”>
  <h1>Select Your Favourite Language</h1>

    <form>
      <select id=”colors”>
        <option value=”Ruby” selected=”selected”>Ruby</option>
        <option value=”JRuby”>JRuby</option>
        <option value=”IronRuby”>IronRuby</option>
        <option value=”Rails”>Rails</option>
      </select>

      <a href=”#” onclick=”add_new_language()” >More Language</a>
      <br/>
      <span id=”wrapper”>
        <br/>
        <input type=”text”/> <!— generated by javascipt –>
      </span>
    </form>
 </body>
</html>

Here how I made the whole flow generic.

#List of Tag that has event Handler
TAGS   =  ['iframe' ,'img', 'frame', 'frameset', 'ilayer', 'input', 'layer', 'select', 'a', 'area', 'form', 'object', 'script', 'embed', 'applet', 'option', 'button']

# defining a Constant with list of event_hander
EVENT_HANDLERS  = ['onclick','onmouseover','onselect','onsubmit']

# List of events that can trigger a function
EVENTS    = ['click','mouseover','select','submit']

 TAGS.each do |tag|
    browser.document.get_html_elements_by_tag_name(tag).each do |element|
       EVENT_HANDLERS.each_with_index do |event_handle,i| 
          element.fire_event(EVENTS[i]) if element.hasAttribute(event_handle) 
       end 
    end
 end

Note : – One didn’t need to fire_event for onload as the onload event is fired as soon as the page is load using browser.goto .

Other options available are : –

    Johnson in Ruby
    Watir in Ruby
    Selenium in JRuby

Thank You

Tagged with:
preload preload preload