Brightbox
  • Home
  • Pricing & Sign up
  • Why Brightbox?
  • Products & Services
  • FAQs
  • About
  • Blog
  • Wiki
  • Contact
Blog RSS feed
twitter_banner

Flickr


more images...

Recent Posts

  • New deployment gem release, better bundler support
  • Passenger 3.0.11 Ubuntu Packages
  • Brightbox Cloud - general availability
  • It's a new brand day!
  • Apache Denial-of-Service Vulnerability

Archives

  • December 2011 (1)
  • November 2011 (1)
  • October 2011 (1)
  • September 2011 (2)
  • August 2011 (2)
  • May 2011 (1)
  • March 2011 (3)
  • January 2011 (1)
  • November 2010 (6)
  • September 2010 (4)
  • August 2010 (1)
  • June 2010 (3)
  • May 2010 (1)
  • April 2010 (3)
  • March 2010 (2)
  • February 2010 (3)
  • January 2010 (6)
  • December 2009 (4)
  • November 2009 (6)
  • October 2009 (2)
  • September 2009 (3)
  • August 2009 (4)
  • July 2009 (3)
  • June 2009 (3)
  • May 2009 (5)
  • April 2009 (4)
  • March 2009 (4)
  • February 2009 (3)
  • January 2009 (6)
  • December 2008 (8)
  • November 2008 (7)
  • October 2008 (8)
  • September 2008 (3)
  • August 2008 (5)
  • July 2008 (1)
  • June 2008 (4)
  • May 2008 (4)
  • April 2008 (3)
  • March 2008 (3)
  • February 2008 (3)
  • January 2008 (4)
  • December 2007 (4)
  • November 2007 (3)
  • October 2007 (1)
  • August 2007 (7)
  • July 2007 (1)
  • June 2007 (3)

Popular tags

    • announcements
    • apache
    • beta
    • deployment
    • hardy
    • packages
    • passenger
    • performance
    • phusion
    • rack
    • rails
    • ruby
    • ruby on rails
    • security
    • ubuntu

Tag Cloud

announcements apache beta brightbox brightbox cloud brightbox gem cloud conference control panel dapper debian deployment events gem hardy hosting launch leeds linux london lucid maintenance modrails mod_rails mysql network nginx packages passenger performance phusion pricing rack rails rails hosting ruby ruby on rails security team tech ubuntu uk updates upgrade xen

Posts tagged ‘activemq’

Load Balancing with Stomp and ActiveMessaging 14 Apr 10

Inevitably with modern distributed software architecture you’re going to end up talking to a message queue at some point. We’ve been using ActiveMQ as the message broker, STOMP as the message protocol of choice and the ActiveMessaging gem so that we can talk to the message broker from Rails.

ActiveMessaging is a great piece of code, but at first glance it appears to have a couple of wrinkles

  • The standard configuration doesn’t support load balancing (so that you can talk to another broker if your normal one isn’t responding).
  • The queues and topics you want to speak to are defined at class level – which is a problem if you want the queues to be dynamically defined based upon data values.

But thanks to Ruby, YAML and a bit of lateral thinking you can get around these issues.

ActiveMessaging uses the Stomp Gem under the hood, and that can do load balancing by itself. So all you have to do is get ActiveMessaging to pass the relevant incantations down to the Stomp Gem – intact. And you do that with a bit of fancy YAML. The trick is to add your host configurations to the login configuration entry in hash format like this:

 production:
    adapter: stomp
    login:
      :randomize: true
      :hosts:
      - :login: myloginid1
        :passcode: mypassword1
        :host: broker-host-1.somedomain.co.uk
      - :login: myloginid2
        :passcode: mypassword2
        :host: broker-host-2.somedomain.co.uk

The Stomp Connection class checks login to see if it is a hash and if it is uses that in preference to anything else. You can setup anything Stomp understands within that Hash. Check the class code for details.

The beauty of this approach is that it is entirely in the configuration. Your Rails code doesn’t need to know about it. The reconnection happens automatically in the background.

I’ve used an observer class within Rails to handle the message sending. It watches a model and sends messages when stuff is created and updated – nicely separating the concerns. But I wanted to talk to different queues depending upon what was in the model. So the standard ActiveMessaging::MessageSender approach didn’t seem appropriate.

After a bit of digging around it turns out that you can get hold of the broker connection directly rather than going via the Gateway.

client = ActiveMessaging::Gateway.connection

Then you can send to any queue you like, not just the ones defined up front in messaging.rb:

client.send("/queue/#{some_variable}", body, headers)

This is a lower level connection and you lose the filter chains and some error checking. However if you need it, it’s there.

Posted 14 April 2010 by Neil Wilson • 1 comment

activemessaging+ activemq+ rails+ ruby+ stomp


Recent blog posts

  • New deployment gem release, better bundler support
    2 months ago
  • Passenger 3.0.11 Ubuntu Packages
    2 months ago
  • Brightbox Cloud – general availability
    4 months ago
  • It’s a new brand day!
    4 months ago
  • Apache Denial-of-Service Vulnerability
    5 months ago
  • Pricing for Brightbox Cloud (and last call for private beta)
    5 months ago

Join our email list

Flickr (more...)

RSS feeds

Blog feed

Flickr feed

Recent Wiki updates

System Status feed




Wiki | Forums | Terms & Conditions | Privacy | Site Map

Copyright © 2011 Brightbox Systems Ltd. All rights reserved