<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en" xml:base="http://www.symphonious.net/wp-atom.php"><title type="text">Symphonious</title> <subtitle type="text">Living in a state of accord.</subtitle> <updated>2012-01-25T21:25:34Z</updated><link rel="alternate" type="text/html" href="http://www.symphonious.net" /> <id>http://www.symphonious.net/feed/atom/</id> <generator uri="http://wordpress.org/" version="3.3.1">WordPress</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/symphonious" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="symphonious" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[The Business of Standards]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2012/01/25/the-business-of-standards/" /> <id>http://www.symphonious.net/?p=1626</id> <updated>2012-01-25T21:25:34Z</updated> <published>2012-01-25T21:25:34Z</published> <category scheme="http://www.symphonious.net" term="Rants" /> <summary type="html"><![CDATA[Recently I’ve been getting spam from the “standards organisation” OASIS inviting a company that I don’t work for1 to join a new standards initiative. There’s no pretense that I’m being invited because of my clearly superior knowledge of the area involved, merely that the company could get great advertising exposure by participating &#8211; including being [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2012/01/25/the-business-of-standards/">&lt;p&gt; Recently I’ve been getting spam from the “standards organisation” OASIS inviting a company that I don’t work for&lt;a
class="footnote" id="footlink1:1327526703777" href="#footnote1:1327526703777"&gt;1&lt;/a&gt; to join a new standards initiative. There’s no pretense that I’m being invited because of my clearly superior knowledge of the area involved, merely that the company could get great advertising exposure by participating &amp;#8211; including being listed on a press release! Naturally we’d need to become OASIS members and pay the appropriate fee, and to be in the press release you’d have to be at minimum a “sponsor level member”.&lt;/p&gt;&lt;p&gt; On the one hand it’s nice that they aren’t pretending to be anything other than a for-profit marketing company, but isn’t it a bit sad that we think standards are just a marketing tool?&lt;/p&gt;&lt;p
class="footnote"&gt; &lt;a
href="#footlink1:1327526703777" id="footnote1:1327526703777"&gt;1&lt;/a&gt; &amp;#8211; yes, they’ve done so much research into how much I have to contribute that they’ve got the company wrong &lt;a
class="footnotereturn" href="#footlink1:1327526703777"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/oVRe2xbvwiQ" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2012/01/25/the-business-of-standards/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2012/01/25/the-business-of-standards/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Development Mode &#8211; Concatenating Scripts and CSS]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/" /> <id>http://www.symphonious.net/?p=1623</id> <updated>2012-01-11T21:37:30Z</updated> <published>2012-01-11T21:37:30Z</published> <category scheme="http://www.symphonious.net" term="Code and Geek Stuff" /> <summary type="html"><![CDATA[HTML 5 Boilerplate reminded me of an old-school tool which is extremely useful for concatenating JS and CSS files on the fly &#8211; server side includes: &#60;FilesMatch &#34;.*\.combined\.(js&#124;css)$&#34;&#62; Options +Includes SetOutputFilter INCLUDES &#60;/FilesMatch&#62; Then you have a main scripts.combined.js (or css) which contains: &#60;!--#include file=&#34;libs/backbone-min.js&#34; --&#62; &#60;!--#include file=&#34;libs/underscore-min.js&#34; --&#62; &#60;!--#include file=&#34;libs/jquery-1.7.1.min.js&#34; --&#62; Plus any other [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/">&lt;p&gt; HTML 5 Boilerplate reminded me of an old-school tool which is extremely useful for concatenating JS and CSS files on the fly &amp;#8211; server side includes:&lt;/p&gt;&lt;pre&gt;
&amp;#60;FilesMatch &amp;quot;.*\.combined\.(js|css)$&amp;quot;&amp;#62;
  Options +Includes
  SetOutputFilter INCLUDES
&amp;#60;/FilesMatch&amp;#62;
&lt;/pre&gt;&lt;p&gt; Then you have a main scripts.combined.js (or css) which contains:&lt;/p&gt;&lt;pre&gt;
&amp;#60;!--#include file=&amp;quot;libs/backbone-min.js&amp;quot; --&amp;#62;
&amp;#60;!--#include file=&amp;quot;libs/underscore-min.js&amp;quot; --&amp;#62;
&amp;#60;!--#include file=&amp;quot;libs/jquery-1.7.1.min.js&amp;quot; --&amp;#62;
&lt;/pre&gt;&lt;p&gt; Plus any other files you need, in the order you specify. This works great for development mode so you can change a file on the fly and just refresh the browser without running any kind of build script. When it comes time to push to production, it’s easy for a build script to process the file ahead of time and be confident that you’ll get exactly the same result.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/iz-v0QgM1dY" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Cross Pairing]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2012/01/03/cross-pairing/" /> <id>http://www.symphonious.net/?p=1613</id> <updated>2012-01-03T21:43:11Z</updated> <published>2012-01-03T21:42:38Z</published> <category scheme="http://www.symphonious.net" term="Code and Geek Stuff" /><category scheme="http://www.symphonious.net" term="XP" /> <summary type="html"><![CDATA[This evening I stumbled across two interesting posts about alternate layouts for pairing rather than sitting side by side. Firstly Tom Dale talking about Tilde&#8217;s corner desk pairing setup (and some of the software setup they use) and that was inspired by Josh Susser&#8217;s face to face pairing setup at Pivotal Labs. Both approaches require [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2012/01/03/cross-pairing/">&lt;p&gt; This evening I stumbled across two interesting posts about alternate layouts for pairing rather than sitting side by side.  Firstly &lt;a
href="http://tomdale.net/2012/01/tildes-pairing-setup/"&gt;Tom Dale talking about Tilde&amp;#8217;s corner desk pairing setup&lt;/a&gt; (and some of the software setup they use) and that was inspired by &lt;a
href="http://pivotallabs.com/users/jsusser/blog/articles/1505-pairing-tete-a-tete"&gt;Josh Susser&amp;#8217;s face to face pairing setup at Pivotal Labs&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Both approaches require more floor space which makes them difficult to setup but I would expect the face to face pairing to be a heck of a lot better if done well. I&amp;#8217;ve always preferred having separate screens in mirror configuration as well as separate keyboards and mice to allow the developers to sit a little further apart to be comfortable and to be able to look straight ahead at the screen. That said, I quite like having a second monitor for spreading out windows as we have at LMAX so it&amp;#8217;s not clear cut which is better.&lt;/p&gt;&lt;p&gt;It&amp;#8217;s also interesting to note the popularity of the flat screen iMacs as opposed to Mac Pros or laptops. The former being too expensive for extra power and extensibility that generally isn&amp;#8217;t required and the latter often being a bit too individualised to be good as pairing machines. Plus laptops, while amazingly powerful these days still have less bang for the buck and the reduction in performance is just enough to matter for development.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/93rBMBxBOjk" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2012/01/03/cross-pairing/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2012/01/03/cross-pairing/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Bottlenecks in Programmer Productivity]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/" /> <id>http://www.symphonious.net/?p=1618</id> <updated>2012-01-01T20:40:11Z</updated> <published>2012-01-02T14:10:53Z</published> <category scheme="http://www.symphonious.net" term="Code and Geek Stuff" /> <summary type="html"><![CDATA[Yan Pritzker recently posted “5 ways to get insane productivity boosts for coders” which provides some good tips on how to improve your usage of tools when doing technical work. To summarise: Never look when you can search Don’t repeat yourself (by setting up shortcuts and command line aliases) Learn a scripting language Learn an [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/">&lt;p&gt; Yan Pritzker recently posted “&lt;a
href="http://yanpritzker.com/2006/10/03/five-ways-to-get-insane-productivity-boosts/"&gt;5 ways to get insane productivity boosts for coders&lt;/a&gt;” which provides some good tips on how to improve your usage of tools when doing technical work. To summarise:&lt;/p&gt;&lt;ul&gt;&lt;li&gt; Never look when you can search&lt;/li&gt;&lt;li&gt; Don’t repeat yourself (by setting up shortcuts and command line aliases)&lt;/li&gt;&lt;li&gt; Learn a scripting language&lt;/li&gt;&lt;li&gt; Learn an editor (and use that editor everywhere)&lt;/li&gt;&lt;li&gt; Learn regular expressions&lt;/li&gt;&lt;/ul&gt;&lt;p&gt; However, nearly all of these tips really boil down to how to be more productive at writing text and the mechanics of writing code – editing actual source code files, jumping to different places in those files, executing commands more efficiently etc. Are these really the tasks that consume the vast majority of a developers time?&lt;/p&gt;&lt;p&gt; While often it feels like they are, after all we spend all day working with a text editor or the command line, are we really spending all that time struggling to keep up with the stream of code our brains are trying to output? Personally, I spend a lot more time thinking about what the best algorithm is, what direction we should be pushing the design in and how to do that and so on. Rarely do I need to type at full speed for extended periods of time.&lt;/p&gt;&lt;p&gt; Given that, I suspect the real bottlenecks in developer productivity are more along the lines of:&lt;/p&gt;&lt;ul&gt;&lt;li&gt; Comprehending an area of code and its design&lt;/li&gt;&lt;li&gt; Identifying, evaluating and selecting potential solutions to a problem (be that choice of algorithm, design choices or choice of classes/libraries etc)&lt;/li&gt;&lt;li&gt; Understanding and sharing a design vision within the team&lt;/li&gt;&lt;li&gt; Understanding and sharing requirements within the team&lt;/li&gt;&lt;/ul&gt;&lt;p&gt; This isn’t an exhaustive list but it should give an idea for the types of things that really limit programmer productivity. In other words, someone who types with two fingers can be much more productive than someone who knows every shortcut in their editor if they are able to identify a simpler solution to the problem.&lt;/p&gt;&lt;p&gt; For example, recently LMAX needed to make a large and potentially quite risky change to how our system worked. The change affected a core concept in the system and so could have knock-on effects to a wide range of components and be very hard to test effectively. At the start of our two week iteration we already had a potential solution planned out but the developers were uncomfortable with the amount of risk involved with that plan. So we spent a full week discussing options, exploring the code and experimenting, plus talking with business experts to get a better understanding of the domain model. All that discussion led us to find a much simpler solution which, if something was missed, would be sure to cause failures in our existing acceptance tests. Even counting that initial week of discussion, finding the right solution meant we could deliver the change in about half the time we had expected.&lt;/p&gt;&lt;p&gt; So by all means, spend time learning to use your tools better, it avoids a lot of tedious boring work, but if you really want a step change improvement in productivity, work to improve you communication, design and thinking skills.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/PXQlq0zdB5U" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/#comments" thr:count="1" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/feed/atom/" thr:count="1" /> <thr:total>1</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Searching on Google Maps Does Nothing In Chrome]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2012/01/01/searching-on-google-maps-does-nothing-in-chrome/" /> <id>http://www.symphonious.net/?p=1615</id> <updated>2012-01-01T19:58:24Z</updated> <published>2012-01-01T19:58:07Z</published> <category scheme="http://www.symphonious.net" term="Troubleshooting" /> <summary type="html"><![CDATA[If you’re experiencing an issue when using Chrome with Google Maps where typing in a location and hitting enter either does nothing or just says “Loading…” forever, you’ve very likely hit upon a bug in the Skype click-to-call extension. Skype automatically installs this “add-on” when you install Skype and it adds links to phone numbers [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2012/01/01/searching-on-google-maps-does-nothing-in-chrome/">&lt;p&gt; If you’re experiencing an issue when using Chrome with Google Maps where typing in a location and hitting enter either does nothing or just says “Loading…” forever, you’ve very likely hit upon a bug in the Skype click-to-call extension.&lt;/p&gt;&lt;p&gt; Skype automatically installs this “add-on” when you install Skype and it adds links to phone numbers so that you can click them to call on Skype. Unfortunately, Google Maps seems to be triggering a bug in the extension and so it is either corrupting the data returned to the Google Maps javascript or just preventing the request from ever returning.&lt;/p&gt;&lt;p&gt; To solve the issue, just uninstall the Skype click to call add-on. I did that by right clicking the Skype icon in the toolbar and selecting “Uninstall” but it should be possible to remove it from the &lt;a
href="about:extensions"&gt;about:extensions&lt;/a&gt; page.&lt;/p&gt;&lt;p&gt; I also found a Skype click-to-call plugin in &lt;a
href="about:plugins"&gt;about:plugins&lt;/a&gt; that I disabled as well &amp;#8211; I’m not sure if it’s removed along with the extension.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/s3XPiXFpd6E" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2012/01/01/searching-on-google-maps-does-nothing-in-chrome/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2012/01/01/searching-on-google-maps-does-nothing-in-chrome/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[More Career Advice]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2011/12/11/more-career-advice/" /> <id>http://www.symphonious.net/2011/12/11/more-career-advice/</id> <updated>2011-12-11T17:44:41Z</updated> <published>2011-12-11T17:44:41Z</published> <category scheme="http://www.symphonious.net" term="General" /> <summary type="html"><![CDATA[Following on from my last post, Jason Adam Young has some excellent advice to help you continually get better and better and building software and perhaps more importantly, be more and more valuable to whoever you happen to be working for.]]></summary> <content type="html" xml:base="http://www.symphonious.net/2011/12/11/more-career-advice/">Following on from my last post, &lt;a
href="http://rambleon.org/2011/12/01/career-advice/"&gt;Jason Adam Young has some excellent advice&lt;/a&gt; to help you continually get better and better and building software and perhaps more importantly, be more and more valuable to whoever you happen to be working for.&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/BIG5xASXHWo" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2011/12/11/more-career-advice/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2011/12/11/more-career-advice/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Career Progression in Technology]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2011/12/06/career-progression-in-technology/" /> <id>http://www.symphonious.net/?p=1598</id> <updated>2011-12-06T18:45:15Z</updated> <published>2011-12-06T18:45:15Z</published> <category scheme="http://www.symphonious.net" term="General" /> <summary type="html"><![CDATA[This afternoon during an interview, a potential new hire at LMAX asked what LMAX could offer in the way of career progression. Since I’ve been thinking a fair bit about what comes next in my career now that I’m moving back to Australia I thought I had a pretty good answer and that it might [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2011/12/06/career-progression-in-technology/">&lt;p&gt; This afternoon during an interview, a potential new hire at LMAX asked what LMAX could offer in the way of career progression. Since I’ve been thinking a fair bit about what comes next in my career now that &lt;a
href="http://www.symphonious.net/2011/11/27/looking-for-work-in-australia/"&gt;I’m moving back to Australia&lt;/a&gt; I thought I had a pretty good answer and that it might be worth sharing here&lt;a
class="footnote" id="footlink2:1323196996971" href="#footnote2:1323196996971"&gt;2&lt;/a&gt;.&lt;/p&gt;&lt;p&gt; In most industries career progression is signified by ever fancier sounding job titles, but for a software developer, the fancier the title sounds the less likely you are to actually do any development. Essentially, career progression often amounts to career migration – into “architecture”&lt;a
class="footnote" id="footlink1:1323196219398" href="#footnote1:1323196219398"&gt;1&lt;/a&gt; or managements. If you really love developing software though you need to look at it a different way. If I spend the rest of my career as a “Senior Software Engineer” I’ll be pretty happy, so long as I can keep honing my skills and becoming more valuable to the companies I work for. Ideally I’d like the pay I take home to grow in line with that value.&lt;/p&gt;&lt;p&gt; Given that world view, the best way I’ve found to progress my career as a software engineer is to work in a place where I’m surrounded by people smarter than I and where I regularly feel stupid. Feeling stupid is a pre-requisite to really learning – it means you’ve just found an area where you have a large gap in your knowledge. If you also work with people smarter than yourself that becomes an opportunity to learn from them.&lt;/p&gt;&lt;p&gt; What’s particularly amazing about this process – and this is something that working at LMAX has really taught me – is that if you spend enough time being excited by the learning opportunities that feeling stupid highlights, you find yourself suddenly providing the knowledge to teach other people who are feeling stupid. So now you get the satisfaction of feeling stupid and learning something new, plus the opportunity to demonstrate how much you’ve learnt by sharing that knowledge with others.&lt;/p&gt;&lt;p&gt; If that’s not an awesome way to spend your career, I don’t know what is&lt;a
class="footnote" id="footlink3:1323197059861" href="#footnote3:1323197059861"&gt;3&lt;/a&gt;.&lt;/p&gt;&lt;p
class="footnote"&gt; &lt;a
href="#footlink1:1323196219398" id="footnote1:1323196219398"&gt;1&lt;/a&gt; &amp;#8211; read, powerpoint production&lt;a
class="footnotereturn" href="#footlink1:1323196219398"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;p
class="footnote"&gt; &lt;a
href="#footlink2:1323196996971" id="footnote2:1323196996971"&gt;2&lt;/a&gt; &amp;#8211; I think I was much more succinct when speaking off the cuff &lt;a
class="footnotereturn" href="#footlink2:1323196996971"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;p
class="footnote"&gt; &lt;a
href="#footlink3:1323197059861" id="footnote3:1323197059861"&gt;3&lt;/a&gt; &amp;#8211; and if you happen to have that kind of environment and are looking for new employees, &lt;a
href="/cv/"&gt;my CV is online&lt;/a&gt; &lt;a
class="footnotereturn" href="#footlink3:1323197059861"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/xodwQ8AR_kg" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2011/12/06/career-progression-in-technology/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2011/12/06/career-progression-in-technology/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Looking for Work In Australia]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2011/11/27/looking-for-work-in-australia/" /> <id>http://www.symphonious.net/?p=1593</id> <updated>2011-11-27T20:54:49Z</updated> <published>2011-11-27T20:54:49Z</published> <category scheme="http://www.symphonious.net" term="General" /> <summary type="html"><![CDATA[After four years living in the UK, my wife and I have decided to move back to Australia mid-February 2012 to be closer to family. As such, I’m now starting to look at job opportunities back in Oz. If you&#39;re hiring, I&#39;d love to hear from you and discuss how we might work together. My [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2011/11/27/looking-for-work-in-australia/">&lt;p&gt; After four years living in the UK, my wife and I have decided to move back to Australia mid-February 2012 to be closer to family. As such, I’m now starting to look at job opportunities back in Oz. If you&amp;#39;re hiring, &lt;a
href="mailto:adrian@symphonious.net"&gt;I&amp;#39;d love to hear from you&lt;/a&gt; and discuss how we might work together. &lt;a
href="/cv/"&gt;My CV&lt;/a&gt; is available online to give you an idea of my experience and of course the backlog of this blog shows some of my thinking and learning throughout the years&lt;a
class="footnote" id="footlink1:1322426906768" href="#footnote1:1322426906768"&gt;1&lt;/a&gt;.&lt;/p&gt;&lt;p&gt; My one key criteria is that I&amp;#39;m looking for a role that provides lots of challenges and opportunities to learn. I&amp;#39;m not particularly concerned with what language or the particular technologies used, but I do believe strongly that Agile methods, automated testing, continuous delivery and a focus on quality are essential to success.&lt;/p&gt;&lt;p&gt; If you think you could use someone like me or have any questions feel free to &lt;a
href="mailto:adrian@symphonious.net"&gt;drop me an email&lt;/a&gt;.&lt;/p&gt;&lt;p
class="footnote"&gt; &lt;a
href="#footlink1:1322426906768" id="footnote1:1322426906768"&gt;1&lt;/a&gt; &amp;#8211; as well as plenty of my mistakes but that’s just part of learning&lt;a
class="footnotereturn" href="#footlink1:1322426906768"&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/cJggLfyso4Q" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2011/11/27/looking-for-work-in-australia/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2011/11/27/looking-for-work-in-australia/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Simple Long Poll Patterns in JavaScript]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/" /> <id>http://www.symphonious.net/?p=1576</id> <updated>2011-11-13T20:10:11Z</updated> <published>2011-11-13T20:10:11Z</published> <category scheme="http://www.symphonious.net" term="Code and Geek Stuff" /><category scheme="http://www.symphonious.net" term="JavaScript" /> <summary type="html"><![CDATA[A couple of little patterns for writing long-poll based JavaScript applications have evolved in some internal apps I’ve been working on lately so I thought I’d document them for posterity. Simple Long Poll Loop There’s very little special about this &#8211; it basically just uses jQuery to do a standard long poll. The pattern that [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/">&lt;p&gt; A couple of little patterns for writing long-poll based JavaScript applications have evolved in some internal apps I’ve been working on lately so I thought I’d document them for posterity.&lt;/p&gt;&lt;h2&gt; Simple Long Poll Loop&lt;/h2&gt;&lt;div
id="gist-1362590" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC2'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;longPollUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/longpoll&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC3'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC4'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;lastReceivedSequence&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC5'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC6'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC7'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;longPollUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC8'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;lastSequence&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lastReceivedSequence&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC9'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;dataType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC10'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC11'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC12'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC13'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;lastReceivedSequence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sequenceNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC14'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;onLongPollEvent&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC15'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;},&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC16'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;poll&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC17'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;});&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC18'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC19'&gt;&lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1362590/7782cc645aaddefa59ec1502223dc5c59f62e31b/basic.js" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590#file_basic.js" style="float:right;margin-right:10px;color:#666"&gt;basic.js&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; There’s very little special about this &amp;#8211; it basically just uses jQuery to do a standard long poll.  The pattern that I like is that instead of calling a particular function directly to handle events it just uses the jQuery trigger function to fire an event that can be handled by any interested code, without introducing tight coupling between the two bits of code.  Listeners would receive events with:&lt;/p&gt;&lt;div
id="gist-1362590" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;onLongPollEvent&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;eve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;longPollData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1362590/e43c38b579ee776419bcd57f4c3a2b2ba3475ab9/listener.js" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590#file_listener.js" style="float:right;margin-right:10px;color:#666"&gt;listener.js&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h2&gt; Automatically Reload JavaScript Changes&lt;/h2&gt;&lt;p&gt; One of the internal tools I’ve been working on is our “big feedback” page for reporting build results. This page is displayed on a large monitor the whole team can see, so refreshing the page to load JavaScript changes requires actually getting up and walking over to it. Fortunately, there’s a simple pattern to get the JavaScript to reload itself:&lt;/p&gt;&lt;div
id="gist-1362590" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;serverVersion&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC2'&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC3'&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serverVersion&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;serverVersion&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC4'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC5'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC6'&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC7'&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1362590/3a6e0ffa1e0e78cc24dc0732c4c1dfd4e5a2ae52/reload.js" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590#file_reload.js" style="float:right;margin-right:10px;color:#666"&gt;reload.js&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; Each long poll event sent down to the browser includes the server version &amp;#8211; when it changes, the JavaScript reloads the page.  There are two nice little details here:&lt;/p&gt;&lt;ol&gt;&lt;li&gt; Deploying a new version actually requires some downtime while the server restarts. Naive implementations will reload the page when the long poll request returns an error but that usually results in the page attempting to reload and also getting an error or no response from the server and now there’s no JavaScript running to recover. Instead, this will wait for the server to come back up and be answering requests again before reloading.&lt;/li&gt;&lt;li&gt; So I don’t have to remember to update the server version, the server actually sends the UNIX time stamp from when it started up. Anytime the server restarts, that value changes and the JavaScript will reload. This can lead to some false positives but it’s extremely simple and effective. Another option would be for the build scripts to insert the SVN revision but that doesn’t work as well for development.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt; Usually I insert this reload code directly into the long poll loop so that it is guaranteed to run first and avoid any potential problems with data formats changing. Even the serverVersion data format can change since it doesn’t try to parse it in any way &amp;#8211; any change, including data.serverVersion no longer being present, will cause the page to reload.&lt;/p&gt;&lt;p&gt; Put together the code comes out something like:&lt;/p&gt;&lt;div
id="gist-1362590" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC2'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;longPollUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/longpoll&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC3'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC4'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;lastReceivedSequence&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC5'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;serverVersion&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC6'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC7'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC8'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;longPollUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC9'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;lastSequence&amp;#39;&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lastReceivedSequence&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC10'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;dataType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC11'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC12'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC13'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC14'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;lastReceivedSequence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sequenceNumber&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC15'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serverVersion&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;serverVersion&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC16'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC17'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC18'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC19'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;onLongPollEvent&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC20'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;},&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC21'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;poll&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC22'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;});&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC23'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC24'&gt;&lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1362590/62216c01d6f248c435e699d430da647c9e392c19/full.js" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590#file_full.js" style="float:right;margin-right:10px;color:#666"&gt;full.js&lt;/a&gt; &lt;a
href="https://gist.github.com/1362590"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; Nothing earth-shatteringly new, but handy little tools to have up your sleeve.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/_gCrANePSuw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/#comments" thr:count="0" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/feed/atom/" thr:count="0" /> <thr:total>0</thr:total> </entry> <entry> <author> <name>Adrian Sutton</name> <uri>http://www.symphonious.net</uri> </author><title type="html"><![CDATA[Background Logging with the Disruptor]]></title><link rel="alternate" type="text/html" href="http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/" /> <id>http://www.symphonious.net/?p=1568</id> <updated>2011-09-26T20:40:15Z</updated> <published>2011-09-26T20:27:26Z</published> <category scheme="http://www.symphonious.net" term="Code and Geek Stuff" /><category scheme="http://www.symphonious.net" term="Disruptor" /><category scheme="http://www.symphonious.net" term="Java" /><category scheme="http://www.symphonious.net" term="LMAX" /><category scheme="http://www.symphonious.net" term="Performance" /> <summary type="html"><![CDATA[Peter Lawrey posted an example of using the Exchanger class from core Java to implement a background logging implementation. He briefly compared it to the LMAX disruptor and since someone requested it, I thought it might be interesting to show a similar implementation using the disruptor. Firstly, let’s revisit the very high level differences between [...]]]></summary> <content type="html" xml:base="http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/">&lt;p&gt; &lt;a
href="http://vanillajava.blogspot.com/2011/09/exchange-and-gc-less-java.html"&gt;Peter Lawrey posted an example of using the Exchanger class from core Java to implement a background logging implementation&lt;/a&gt;. He briefly compared it to the &lt;a
href="http://code.google.com/p/disruptor/"&gt;LMAX disruptor&lt;/a&gt; and since someone requested it, I thought it might be interesting to show a similar implementation using the disruptor.&lt;/p&gt;&lt;p&gt; Firstly, let’s revisit the very high level differences between the exchanger and the disruptor. Peter notes:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt; This approach has similar principles to the &lt;a
href="http://code.google.com/p/disruptor/"&gt;Disruptor&lt;/a&gt;. No GC using recycled, pre-allocated buffers and lock free operations (The Exchanger not completely lock free and doesn&amp;#39;t busy wait, but it could)&lt;/p&gt;&lt;p&gt; Two keys difference are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt; there is only one producer/consumer in this case, the disruptor supports multiple consumers.&lt;/li&gt;&lt;li&gt; this approach re-uses a much smaller buffer efficiently. If you are using ByteBuffer (as I have in the past) an optimal size might be 32 KB. The disruptor library was designed to exploit large amounts of memory on the assumption it is relative cheap and can use medium sized (MBs) to very large buffers (GBs). e.g. it was design for servers with 144 GB. I am sure it works well on much smaller servers. ;)&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;p&gt; Actually, there’s nothing about the Disruptor that requires large amounts of memory. If you know that your producers and consumers are going to keep pace with each other well and you don’t have a requirement to replay old events, you can use quite a small ring buffer with the Disruptor. There are a lot of advantages to having a large ring buffer, but it’s by no means a requirement.&lt;/p&gt;&lt;p&gt; It’s also worth noting that the Disruptor does not require consumers to busy-spin, you can choose to use a blocking wait strategy, or strategies that combine busy-spin and blocking to handle both spikes and lulls in event rates efficiently.&lt;/p&gt;&lt;p&gt; There is also an important advantage to the Disruptor that wasn’t mentioned: it will process events immediately if the consumer is keeping up. If the consumer falls behind however, it can process events in a batch to catch up. This significantly reduces latency while still handling spikes in load efficiently.&lt;/p&gt;&lt;h2&gt; The Code&lt;/h2&gt;&lt;p&gt; First let’s start with the LogEntry class. This is a simple value object that is used as our entries on the ring buffer and passed from the producer thread over to the consumer thread.&lt;/p&gt;&lt;p&gt; Peter’s Exchanger based implementation &amp;#8211; the use of StringBuilder in the LogEntry class is actually a race condition and not thread safe. Both the publishing side and the consumer side are attempting to modify it and depending on how long it takes the publishing side to write the log message to the StringBuilder, it will potentially be processed and then reset by the consumer side before the publisher is complete. In this implementation I’m instead using a simple String to avoid that problem.&lt;/p&gt;&lt;div
id="gist-1243275" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.lmax.disruptor.EventFactory&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC2'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC3'&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LogEntry&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC4'&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC5'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;EventFactory&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FACTORY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;EventFactory&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC6'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC7'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;LogEntry&lt;/span&gt; &lt;span class="nf"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC8'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC9'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC10'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC11'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;};&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC12'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC13'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC14'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC15'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC16'&gt;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC17'&gt;&lt;br/&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1243275/025846f5c293bcf6dbe2a20c73bad514e5317ad0/LogEntry.java" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1243275#file_log_entry.java" style="float:right;margin-right:10px;color:#666"&gt;LogEntry.java&lt;/a&gt; &lt;a
href="https://gist.github.com/1243275"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; The one Disruptor-specific addition is that we create an EventFactory instance which the Disruptor uses to pre-populate the ring buffer entries.&lt;/p&gt;&lt;p&gt; Next, let’s look at the BackgroundLogger class that sets up the process and acts as the producer.&lt;/p&gt;&lt;div
id="gist-1243275" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.lmax.disruptor.RingBuffer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC2'&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.lmax.disruptor.dsl.Disruptor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC3'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC4'&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.ExecutorService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC5'&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.concurrent.Executors&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC6'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC7'&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BackgroundLogger&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC8'&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC9'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ENTRIES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC10'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC11'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ExecutorService&lt;/span&gt; &lt;span class="n"&gt;executorService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC12'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Disruptor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;disruptor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC13'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;RingBuffer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ringBuffer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC14'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC15'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;BackgroundLogger&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC16'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC17'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;executorService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Executors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newCachedThreadPool&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC18'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;disruptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Disruptor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;FACTORY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ENTRIES&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;executorService&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC19'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;disruptor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;handleEventsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;LogEntryHandler&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC20'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;disruptor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC21'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;ringBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;disruptor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getRingBuffer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC22'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC23'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC24'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC25'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC26'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sequence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ringBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC27'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;LogEntry&lt;/span&gt; &lt;span class="n"&gt;logEntry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ringBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC28'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC29'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;logEntry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentTimeMillis&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC30'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;logEntry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC31'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;logEntry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC32'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC33'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;ringBuffer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;publish&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC34'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC35'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC36'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC37'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC38'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;disruptor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;shutdown&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC39'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;executorService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;shutdownNow&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC40'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC41'&gt;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC42'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC43'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC44'&gt;&lt;br/&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1243275/d0fdee872f13a0a2c75b4b0e1c00355e0216eec6/BackgroundLogger.java" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1243275#file_background_logger.java" style="float:right;margin-right:10px;color:#666"&gt;BackgroundLogger.java&lt;/a&gt; &lt;a
href="https://gist.github.com/1243275"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; In the constructor we create an ExecutorService which the Disruptor will use to execute the consumer threads (a single thread in this case), then the disruptor itself. We pass in the LogEntry.FACTORY instance for it to use to create the entries and a size for the ring buffer.&lt;/p&gt;&lt;p&gt; The log method is our producer method. Note the use of two-phase commit. First claim a slot with the ringBuffer.next() method, then copy our values into that slot’s entry and finally publish the slot, ready for the consumer to process. We could have also used the Disruptor.publish method which can make this simpler for many use cases by rolling the two phase commit into call.&lt;/p&gt;&lt;p&gt; The producer doesn’t need to do any batching as the Disruptor will do that automatically if the consumer is falling behind, though there are also APIs that allow batching the producer which can improve the performance if it fits into your design (here it’s probably better to publish each log entry as it comes in).&lt;/p&gt;&lt;p&gt; The stop method uses the new shutdown method on the Disruptor which takes care of waiting until all consumers have processed all available entries for you, &lt;a
href="http://groups.google.com/group/lmax-disruptor/browse_thread/thread/511cadc6383260aa/35e537304fc6439f?lnk=gst&amp;#38;q=stop+disruptor#35e537304fc6439f"&gt;though the code for doing it yourself is quite straight-forward&lt;/a&gt;. Finally we shut down the executor.&lt;/p&gt;&lt;p&gt; Note that we don’t need a flush method since the Disruptor is always consuming log events as quickly as the consumer can.&lt;/p&gt;&lt;p&gt; Last of all, the consumer which is almost entirely implementation logic:&lt;/p&gt;&lt;div
id="gist-1243275" class="gist"&gt;&lt;div
class="gist-file"&gt;&lt;div
class="gist-data gist-syntax"&gt;&lt;div
class="highlight"&gt;&lt;pre&gt;&lt;div class='line' id='LC1'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC2'&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.lmax.disruptor.EventHandler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC3'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC4'&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LogEntryHandler&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;EventHandler&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogEntry&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC5'&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC6'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC7'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;LogEntryHandler&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC8'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC9'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC10'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC11'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onEvent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;LogEntry&lt;/span&gt; &lt;span class="n"&gt;logEntry&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;endOfBatch&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC12'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC13'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="c1"&gt;// Write&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC14'&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC15'&gt;&lt;br/&gt;&lt;/div&gt;&lt;div class='line' id='LC16'&gt;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class='line' id='LC17'&gt;&lt;br/&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div
class="gist-meta"&gt; &lt;a
href="https://gist.github.com/raw/1243275/f353a5c78c4bc6c75121b1870d37608e36935d31/LogEntryHandler.java" style="float:right;"&gt;view raw&lt;/a&gt; &lt;a
href="https://gist.github.com/1243275#file_log_entry_handler.java" style="float:right;margin-right:10px;color:#666"&gt;LogEntryHandler.java&lt;/a&gt; &lt;a
href="https://gist.github.com/1243275"&gt;This Gist&lt;/a&gt; brought to you by &lt;a
href="http://github.com"&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; The consumer’s onEvent method is called for each LogEntry put into the Disruptor. The endOfBatch flag can be used as a signal to flush written content to disk, allowing very large buffer sizes to be used causing writes to disk to be batched when the consumer is running behind, yet also ensure that our valuable log messages get to disk as quickly as possible.&lt;/p&gt;&lt;p&gt; &lt;a
href="https://gist.github.com/1243275"&gt;The full code is available as a Gist&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/symphonious/~4/ur6Bc1ZKXhs" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/#comments" thr:count="2" /><link rel="replies" type="application/atom+xml" href="http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/feed/atom/" thr:count="2" /> <thr:total>2</thr:total> </entry> </feed><!-- Dynamic page generated in 1.945 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-01-25 21:29:18 --><!-- Compression = gzip -->

