Archive for the 'Code' Category

Simple Ruby Monitoring and Management Interface For beanstalkd, beanstalkd_view

Beanstalk is great option if you need a fast lightweight message queue. The protocol is simple, and there are lots of examples and libraries online. (Here's a nice summary article.)

However, it does not come with a standard management UI.

Resque, a message queue built on top of Redis, has a great web management interface as part of its source base. It was implemented as a Sinatra app, that can even be embedded into a Rails app.

(See the Resque Railscast)

Being jealous of that interface, I decided to create a similar Sinatra app that can be used to manage and monitor your beanstalkd queues. The application is called beanstalkd_view, and the source code is available on github:


Since beanstalkd_view is a Sinatra app it can be embedded into a Rails app. Simply mount the application in your routes.rb file:

Alternately, beanstalkd_view uses the Vegas gem, so you can start the application from the command line on an available port. Just execute the beanstalkd_view executable which will be available after executing gem install beanstalkd_view.

To configure beanstalkd_view, you have to set the ENV['BEANSTALK_URL'] with the URLs of your beanstalkd daemons.

  • If mounted in a Rails app, setting this variable could be done within an initializer.
  • If running from the command line, you'll currently have to edit the bin file: beanstalkd_view/bin/beanstalkd_view, which is hardcoded to the default localhost address of beanstalkd.


beanstalkd_view is a simple two page application.

The index page shows the overall beanstalkd statistics: charts showing the counts of the different job types submitted and buried, and a basic form for submitting new jobs with JSON data.

A page providing specific tube statistics and actions can be accessed by clicking on a tube name at the top of the index page. This page shows the individual tube's statistics and also forms for kicking the tube, pausing the tube, and peeking at jobs.

Screenshot of beanstalkd_view

Behavior with multiple beanstalk queues

Beanstalkd_view leverages the beanstalk-client gem to interact with the beanstalkd daemons. This library is great because it provides a ConnectionPool object to manage interacting with multiple beanstalkd daemons.

(Beanstalkd does not natively support any form of clustering, so multiple instances must be managed by the client.)

When using multiple queues:

Actions Behavior
Queue statistic, tube listing, and pause commands Sent to all queue instances
Kick and add new job commands (put) Sent to random queue instances
Peek and delete job commands (put) Sent to each queue instance, looking for response


See the github page for more information.

Please send back any feedback if you find beanstalkd_view useful. Or submit pull requests on github.


The Flex 4 TitleWindow Component: Adding the missing features

The new Spark components included in Flex 4 are a tremendous step forward in terms of ease of skinning and extensibility, but some of them seem a little barebones out of the box. In particular the Panel and TitleWindow components are lacking a number of features that I frequently wind up needing (and I'm guessing I'm not alone). I tackled something similar in Flex 3, when I created the BetterTitleWindow component, but I've had a few more ideas for features and I'm all about Flex 4 these days, so I thought I'd give it another go.

Here's a quick rundown of all the features I added:

  • - ability to collapse/expand
  • - optional collapse/expand indicator
  • - title bar or indicator clickable
  • - ability to display a close button (which dispatches a CloseEvent)
  • - closeButtonSkin can be specified
  • - titleBarContent property can be used to add components to the title bar
  • - layout of titleBarContent can be manipulated through titleBarLayout property
  • - title bar height settable through a style property

The most fun feature I added is the ability to make the component collapsible, so that it minimizes down to just the title bar and expand back up. This is a pretty cool capability to have, and I have some ideas about using this to create a full-fledged Accordion replacement.

Here's some mxml showing how to set up the title bar content and layout:

And here's a sample app that shows the new features. View source is enabled, and you can also download the source from our svn.


Invalidating a Spark List Component to Redraw All Item Renderers

A Flex 3 List component has a public method called invalidateList() that invalidates the display list causing all item renderers to be redrawn. In Flex 4 the Spark List component doesn't have this type of method but you can easily achieve the same goal by simply resetting the itemRenderer property temporarily and then restoring it to trigger a redraw. For example:

If you have a subclass of the List component you can simply add the following method or make it static and add it to a utlity class.

This becomes quite handy when you have a control (e.g. slider) that when interacted with changes the size of item renderers dynamically.


Programmatically creating bar or column charts in Flex

Creating Flex charts and series is easy if you lay them out in MXML.  But creating them programmatically can be a little trickier.  Here's an issue that's not too difficult to figure out but that I found wasn't documented in any other articles online.

Every chart object holds an array of series objects: LineSeries, AreaSeries, BarSeries, etc.  One day I wanted to create a chart that showed columns of data so I wrote what I thought would be a chart with ColumnSeries.

However, I was stumped when no series showed up on my chart.  Where did they go?

Turns out, all ColumnSeries have to be included in a ColumnSet, and the same for BarSeries in a BarSet.  The *Set objects in turn have another array of all the *Series they hold.  So a chart that correctly houses ColumnSeries would look like this:

Although the idea of having an array of series objects within an array called series on a chart seems a little difficult, it actually makes a lot of sense. The *Series are stored within an overall *Set object because they are grouped together and are able to be stacked. Using multiple *Sets on one chart allows these groupings to stay separate and stacked charts to stack correctly without being lumped together.


HtmlUnit is not just for unit testing

From the name it may appear that HtmlUnit is only used for unit testing. While it has extensive logging and error detection, it is really a browser client including Javascript and SSL support that can be used along with a testing framework.

A few uses of HtmlUnit without a testing framework would be to:

  1. monitor information from a web site that doesn't have a web services interface. An example would be an internal build servers status page.
  2. automate logging into a website and posting status reports on a regular schedule.

Here are a few tips to get HtmlUnit set up quickly for use in automation.
See documentation on the project page for more info

1. Download off of the project page: Or if you are using Maven to build your project, include the following dependency:


2. To turn off Javascript errors from stopping execution, add code as follows:

   WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);

3. To optionally add a proxy server, which can be useful to monitor traffic while debugging with a debug proxy, add:

   if (PROXY) {
       ProxyConfig config = new ProxyConfig();

4. To allow bypassing invalid SSL certificates (may be needed for an SSL debug proxy or for accessing sites with expired certificates):

      try {
      } catch (GeneralSecurityException e) {

5. To shut off all the warning messages that are generated by default:
(from the htmlunit docs)

    System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "error");

or if using log4j, add the following lines to log4j.conf:


Refer to the HtmlUnit project page for info on finding elements, submitting forms, using Javascript, etc.

other pages:
How to write a simple internet/web robot in Java?
htmlunit project page