<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>CodeDependents</title>
	<atom:link href="http://codedependents.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codedependents.com</link>
	<description>Two Geeks Walk Into A Blog</description>
	<lastBuildDate>Mon, 30 Apr 2012 15:53:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codedependents.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>CodeDependents</title>
		<link>http://codedependents.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codedependents.com/osd.xml" title="CodeDependents" />
	<atom:link rel='hub' href='http://codedependents.com/?pushpress=hub'/>
		<item>
		<title>Benchmarking More Seq Traversal Idioms in Scala</title>
		<link>http://codedependents.com/2012/04/30/benchmarking-more-seq-traversal-idioms-in-scala/</link>
		<comments>http://codedependents.com/2012/04/30/benchmarking-more-seq-traversal-idioms-in-scala/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 13:24:34 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[seq]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=553</guid>
		<description><![CDATA[Last week I had the luxury of spending some quality time with YourKit and our production system at Localytics and was pleasantly surprised to see things humming right along. Most of our time was spent building collections and iterating over them which got me thinking. What is the most efficient way to traverse a collection in Scala? After a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=553&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" style="border-right-width:10px;border-right-color:white;border-right-style:solid;border-left-width:10px;border-left-color:white;border-left-style:solid;" title="mobius strip" src="http://files.turbosquid.com/Preview/2010/12/21__13_50_36/mobius1.jpg24157079-a79d-4c7c-acca-c50ba4ca5386Large.jpg" alt="" width="100" height="100" />Last week I had the luxury of spending some quality time with <a href="http://yourkit.com/">YourKit</a> and our production system at <a href="http://www.localytics.com">Localytics</a> and was pleasantly surprised to see things humming right along. Most of our time was spent building collections and iterating over them which got me thinking. What is the most efficient way to traverse a collection in Scala? After a quick trip to google I had two blog posts in hand.</p>
<p>The <a href="http://blog.juma.me.uk/2009/10/26/new-jvm-options-and-scala-iteration-performance/">first post</a> was from way back in 2009. While its main focus was on the impact of JVM options on Scala iteration it pointed out that Vectors seem to be more performant than Lists for iteration. However, being from 2009, I was curious if this result still stood. The <a href="http://paradigmatic.streum.org/2012/02/benchmarking-scala-list-traversal-idioms/">second post</a> was from 2012 and benchmarked various ways of iterating over a List while applying two different transformations. The post didn&#8217;t benchmark Vector but the author was particularly rigorous with his methodology; making use of <a href="http://code.google.com/p/caliper/">Caliper</a> and posting his code on <a href="https://github.com/paradigmatic/seqBench">github</a>. I highly recommend reviewing the post.</p>
<p>I <a href="https://github.com/bdarfler/seqBench">forked</a> the repo, added two more tests, ensured the server vm was being used and re-ran the tests.</p>
<h2>Setup</h2>
<h3>Java</h3>
<pre>java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-4ubuntu2)
OpenJDK Server VM (build 20.0-b12, mixed mode)</pre>
<h3>Scala</h3>
<pre>Scala code runner version 2.9.1 -- Copyright 2002-2011, LAMP/EPFL</pre>
<h3>OS</h3>
<pre>Ubuntu 12.04</pre>
<h3>CPU</h3>
<pre>Dual Intel(R) Xeon(R) CPU E5410 @ 2.33GHz (EC2 c1.medium)</pre>
<h2>Code</h2>
<h3>Functional Vector</h3>
<p><pre class="brush: scala;">
val wLength = wordsIndexedSeq.map( _.length )
val wCaps = wordsIndexedSeq.map( isCapitalized )
(wLength, wCaps)
</pre></p>
<h3 id="LC90">Builder</h3>
<p><pre class="brush: scala;">
val n = wordsList.length
val wLength = List.newBuilder[Int]
val wCaps = List.newBuilder[Boolean]
for( word &lt;- wordsList ) {
  wLength += word.length
  wCaps += isCapitalized(word)
}
( wLength.result(), wCaps.result() )
</pre></p>
<h2>Results</h2>
<p>Each style of iteration was tested with four different sized collections. The graph shows how many times slower each style was versus the OldSchool style (i.e. arrays and while loops).</p>
<p><a href="http://codedependents.files.wordpress.com/2012/04/chart_1-11.png"><img class="aligncenter size-full wp-image-580" title="Results" src="http://codedependents.files.wordpress.com/2012/04/chart_1-11.png?w=630&#038;h=188" alt="Results" width="630" height="188" /></a></p>
<h2>Conclusions</h2>
<ul>
<li>Nothing beats arrays and while loops (i.e. the OldSchool solution)</li>
<li>Vector beats out List</li>
<li>Vector interestingly gets better with more items</li>
<li>In contrast to the original post none of the List optimizations seem like a clear win to me</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/553/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/553/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/553/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=553&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2012/04/30/benchmarking-more-seq-traversal-idioms-in-scala/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://files.turbosquid.com/Preview/2010/12/21__13_50_36/mobius1.jpg24157079-a79d-4c7c-acca-c50ba4ca5386Large.jpg" medium="image">
			<media:title type="html">mobius strip</media:title>
		</media:content>

		<media:content url="http://codedependents.files.wordpress.com/2012/04/chart_1-11.png" medium="image">
			<media:title type="html">Results</media:title>
		</media:content>
	</item>
		<item>
		<title>Decoding strings with an unknown encoding</title>
		<link>http://codedependents.com/2011/09/15/decoding-strings-with-an-unknown-encoding/</link>
		<comments>http://codedependents.com/2011/09/15/decoding-strings-with-an-unknown-encoding/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 11:49:40 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=495</guid>
		<description><![CDATA[We&#8217;ve all been there, the system is humming along when you bring up the UI only to see &#8220;Espa?ol&#8221; staring you back in the face. What is this? Why is there a question mark in there? Well, you&#8217;ve been bitten by they mystical world of unicode. Usually this issue is an easy fix; always use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=495&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://unicodesnowmanforyou.com/"><img class="alignleft size-thumbnail wp-image-502" style="border-right-width:10px;border-right-color:white;border-right-style:solid;border-left-width:10px;border-left-color:white;border-left-style:solid;" title="Unicode Snowman" src="http://codedependents.files.wordpress.com/2011/08/screen-shot-2011-08-21-at-12-23-10-pm1.png?w=120&#038;h=150" alt="Unicode Snowman" width="120" height="150" /></a>We&#8217;ve all been there, the system is humming along when you bring up the UI only to see &#8220;Espa?ol&#8221; staring you back in the face. What is this? Why is there a question mark in there? Well, you&#8217;ve been bitten by they <a href="http://www.joelonsoftware.com/articles/Unicode.html">mystical world of unicode</a>. Usually this issue is an easy fix; <a href="http://webdosanddonts.com/always-use-utf-8-encoding">always use UTF-8 encoding</a>. However, what to do if you don&#8217;t own the whole code path? What if you have to support poorly written third party client libraries? What if you have client libraries in the wild that will never be updated? What if these client libraries out right lie about what encoding they are using? Follow me and we&#8217;ll find out.</p>
<h3>Bytes To String</h3>
<p>On the JVM there are two standard ways of converting bytes to Strings; new String(bytes[], encoding) and using the NIO Charset. Since they both accomplish the same feat my decision came down to performance. Luckily someone else did <a href="http://www.javacodegeeks.com/2010/11/java-best-practices-char-to-byte-and.html">the heavy lifting</a> and figured out that new String(bytes[], encoding) ekes out a small win over NIO Charset. However, this option poses a challenge. How do we know if the decoding succeeded? The NIO option can throw an Exception (effective but slow) or insert a <a href="http://en.wikipedia.org/wiki/Specials_(Unicode_block)">Unicode Replacement Character</a> (easy to find with a String.contains()) if it encounters a some bytes that cannot be decoded . new String(bytes[], encoding) does no such thing. It blindly decodes characters and will output random garbage in the resulting string if the decoding chokes on some bad byte values. We need a way to find those garbage characters.</p>
<h3>Regex for non unicode characters</h3>
<p>The clue that got me on the right track was an odd looking pattern in the <a href="http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html">javadoc for Pattern</a>.</p>
<pre>[\p{L}&amp;&amp;[^\p{Lu}]] Any letter except an uppercase letter (subtraction)</pre>
<p>It seemed that \p{L} was some magical regex incantation for any unicode letter and after some additional searching it appeared that <a href="http://www.regular-expressions.info/refunicode.html">that was exactly the case</a>. Of course what we really want to find are characters that are not unicode letters, spaces, punctuation or digits. Lucky for us there are matching groups for all of these, leading us to this regex:</p>
<pre>[^\p{L}\p{Space}\p{Punct}\p{Digit}]</pre>
<h3>Performance Testing</h3>
<p>Since performance is of particular concern it was important to test the overhead of this regex check. I fired up the Scala REPL and <a href="https://gist.github.com/1203677">tested</a> the using new String(bytes[], encoding) with the above regex as compared to NIO Coded using String.contains() to check for the replacement character. After all that work it turns out that the regex was significantly more expensive than String.contains(). So much so that the NIO code was about 2x as fast. So, in the end, I ended up going with the simpler NIO option.</p>
<h3>The Code</h3>
<p><pre class="brush: scala;">
import io.Codec
import java.nio.ByteBuffer

val UTF8 = &quot;UTF-8&quot;
val ISO8859 = &quot;ISO-8859-1&quot;
val REPLACEMENT_CHAR = '\uFFFD'

def bytesToString(bytes: Array[Byte], encoding: String) = {
  val upper = encoding.toUpperCase
  val codec = if (ISO8859 == upper) Codec.ISO8859 else Codec.UTF8
  val decoded = codec.decode(ByteBuffer.wrap(bytes)).toString
  if (!decoded.contains(REPLACEMENT_CHAR)) {
    decoded
  } else {
    val otherCodec = if (ISO8859 == upper) Codec.UTF8 else Codec.ISO8859
    val otherDecoded = otherCodec.decode(ByteBuffer.wrap(bytes)).toString
    if (!otherDecoded.contains(REPLACEMENT_CHAR)) {
      otherDecoded
    } else {
      val utf8 = if (ISO8859 == upper) otherDecoded else decoded
      utf8.replace(REPLACEMENT_CHAR, '?')
    }
  }
}
</pre></p>
<h3>Update</h3>
<p>Thanks to <a href="http://jonisalonen.com/">Joni Salonen</a> for pointing out that I was wrong about new String() not inserting the unicode replacement char. In light of that info the following code is just a touch faster.</p>
<p><pre class="brush: scala;">
val UTF8 = &quot;UTF-8&quot;
val ISO8859 = &quot;ISO-8859-1&quot;
val REPLACEMENT_CHAR = '\uFFFD'
def bytesToString(bytes: Array[Byte], encoding: String) = {
  val upper = encoding.toUpperCase
  val firstEncoding = if (ISO8859 == upper) ISO8859 else UTF8
  val firstDecoded = new String(bytes, firstEncoding)
  if (!firstDecoded.contains(REPLECEMENT_CHAR)) {
    firstDecoded
  } else {
    val secondEncoding = if (ISO8859 == upper) UTF8 else ISO8859
    val secondDecoded = new String(bytes, secondEncoding)
    if (!secondDecoded.contains(REPLECEMENT_CHAR)) {
      secondDecoded
    } else {
      val utf8 = if (ISO8859 == upper) secondDecoded else firstDecoded
      utf8.replace(REPLECEMENT_CHAR, '?')
    }
  }
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/495/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=495&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2011/09/15/decoding-strings-with-an-unknown-encoding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://codedependents.files.wordpress.com/2011/08/screen-shot-2011-08-21-at-12-23-10-pm1.png?w=120" medium="image">
			<media:title type="html">Unicode Snowman</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Heaps and Garbage Collection with some Zazz</title>
		<link>http://codedependents.com/2011/06/10/java-heaps-and-garbage-collection-with-some-zazz/</link>
		<comments>http://codedependents.com/2011/06/10/java-heaps-and-garbage-collection-with-some-zazz/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 20:30:59 +0000</pubDate>
		<dc:creator>John Russell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=480</guid>
		<description><![CDATA[Here is a presentation I put together about Java heaps and garbage collection to go into some more detail than just raising the heap size. I put in links to some great websites and blog posts that are fantastic reads if you are trying to tune your app&#8217;s garbage collector. The best part though is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=480&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is a presentation I put together about Java heaps and garbage collection to go into some more detail than just raising the heap size. I put in links to some great websites and blog posts that are fantastic reads if you are trying to tune your app&#8217;s garbage collector.</p>
<p>The best part though is the &#8220;wicked awesome&#8221; presentation tool <a href="http://prezi.com" target="_blank">Prezi</a>. It is an infinitely zoomable moving twisting camera landscape thingy . . . its kinda hard to describe.  But it makes PowerPoint look like two cups with a string between them.  Just check it out.</p>
<iframe frameborder="0" width="558" height="408" src="http://wpcomwidgets.com/?src=http%3A%2F%2Fprezi.com%2Fbin%2Fpreziloader.swf&amp;type=application%2Fx-shockwave-flash&amp;allowfullscreen=true&amp;allowscriptaccess=always&amp;width=550&amp;height=400&amp;bgcolor=%23ffffff&amp;flashvars=prezi_id%3Dlzofqasgefim%26lock_to_path%3D0%26color%3Dffffff%26autoplay%3Dno%26autohide_ctrls%3D0&amp;_tag=gigya&amp;_hash=74e4d40cf13de720aa56213ea9115b3a" id="74e4d40cf13de720aa56213ea9115b3a"></iframe>
<p>You can view it fullscreen and Prezi lets you copy presentations and modify them. Here is the <a href="http://prezi.com/lzofqasgefim/java-garbage-collection-and-heap-analysis/" target="_blank">direct link to the presentation</a> above if you want to copy it.  You can download a self contained offline viewer of a presentation you own (or one you&#8217;ve copied).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/480/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=480&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2011/06/10/java-heaps-and-garbage-collection-with-some-zazz/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c75369f25cd6b34076875bdc308c2aca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnjamesrussell</media:title>
		</media:content>
	</item>
		<item>
		<title>Software Development is not Just Coding</title>
		<link>http://codedependents.com/2011/06/06/software-development-is-not-just-coding/</link>
		<comments>http://codedependents.com/2011/06/06/software-development-is-not-just-coding/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 13:58:50 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=323</guid>
		<description><![CDATA[Throughout my career as a startup software developer I have constantly come across fellow developers who seem to have a confused concept of our common profession. Specifically, they believe coding to be the single activity which comprises software development. Now its not hard to see how this misconception comes about. Too frequently developers are interrupted [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=323&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://codedependents.files.wordpress.com/2011/02/coding1.jpg"><img class="size-full wp-image-451 alignleft" style="margin-right:10px;" title="Coding" src="http://codedependents.files.wordpress.com/2011/02/coding1.jpg?w=630" alt="Coding"   /></a>Throughout my career as a startup software developer I have constantly come across fellow developers who seem to have a confused concept of our common profession. Specifically, they believe coding to be the single activity which comprises software development. Now its not hard to see how this misconception comes about. Too frequently developers are interrupted by frivolous meetings causing a backlash against anything that hints at time away from the desk. Moreover, at the end of the day, the top priority is getting features out the door and any time spent not coding smells of time lost. However, this narrow view of software development is holding us back and cramping our productivity. Specifically, there are three realms in which we need to spend more time up front so we can run faster over all.</p>
<h2>Quality assurance and the rise of unit tests</h2>
<p>I have yet to meet a startup that had enough resources for a QA team and rightly so. The rise of unit test and continuous integration tools throws into doubt the benefit of a dedicated QA team in all but the most extreme cases. However, I still see far too many developers who pay lip service to their value and then, in the name of speed, proceed to code without any. What they are missing is the insane productivity gains to be found in having a suit of tests running against every checkin. Being able to refactor and hack away without needing weeks of subsequent manual testing or months of sleepless nights fixing live bugs is something that is missed by many, particularly in the startup world, and is a huge drain on productivity.</p>
<h2>Ops becomes DevOps</h2>
<p>More recently we are beginning to see the influence of tools effecting the operations side as well. These days, through the power of <a href="http://aws.amazon.com/">Amazon Web Services</a>, <a href="http://www.rackspace.com/index.php">Rackspace</a>, and others, developers no longer need to rely on in house operations for their hardware needs. However, operations provided more services than simply assembling servers and connecting them to a network. Developers are now in the position where they must take on the responsibility of deploying and monitoring their software. Fortunately, hardware is something no startup can run without so, unlike QA, understanding the necessity of AWS or Rackspace is a no brainer. Unfortunately, understanding the need to spend time setting up monitoring and automating deployment is still a work in progress. Too often clients and customers are the first ones to discover a service outage and deploying more servers takes days and not hours. The seemingly never ending march of minor crises which is operations can destroy productivity if some judicious work and planning is not done upfront.</p>
<h2>What are we doing and we will we get there?</h2>
<p>Finally we come to the most important and also the most contentious issue, planning. Many developers approach any sort of planning with apprehension if not outright disgust. However, these are the same developers that end up taking days if not weeks longer to launch a product or wrap up a milestone because there was not enough coordination and thought ahead of time to plan the multitude of steps that goes into such an event. At a higher level I have seen whole companies spin while different developers over engineer and over refine products based on vague requirements causing a jump in coding &#8220;productivity&#8221; due to less meetings and yet a serious drop in output as nothing is delivered and what is delivered is inevitably not what anyone wanted. If we want to step up our game and get things done we need to stop shying away from all process and being adopting the right processes. There are any number of options (<a href="http://en.wikipedia.org/wiki/Scrum_(development)">SCRUM</a>, <a href="http://en.wikipedia.org/wiki/Lean_Startup">Lean</a>, <a href="http://en.wikipedia.org/wiki/Kanban">Kanban</a>, etc.) to learn and borrow from. Make your own process. Start with the simplest thing that works. Add (or remove!) when something isn&#8217;t working. Whatever it might be we need to move beyond the &#8220;all process is evil&#8221; mentality. Yes there will be meetings but you will shock yourself at how much faster you are in the end.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/323/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/323/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=323&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2011/06/06/software-development-is-not-just-coding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://codedependents.files.wordpress.com/2011/02/coding1.jpg" medium="image">
			<media:title type="html">Coding</media:title>
		</media:content>
	</item>
		<item>
		<title>Twitter &gt; RSS: How to keep up with developers</title>
		<link>http://codedependents.com/2010/10/14/twitter-rss-how-to-keep-up-with-developers/</link>
		<comments>http://codedependents.com/2010/10/14/twitter-rss-how-to-keep-up-with-developers/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 16:22:58 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=433</guid>
		<description><![CDATA[Times are a Changing I remember walking into a team meeting my first day on the job and overhearing my fellow blogger explaining the RSS feed setup that he used to keep up with developer news. At the time, this was a new idea to a majority of the yet here we are, a short [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=433&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://codedependents.files.wordpress.com/2010/10/group-of-people-talking.jpg"><img class="alignleft size-full wp-image-441" style="margin-right:10px;" title="group-of-people-talking" src="http://codedependents.files.wordpress.com/2010/10/group-of-people-talking.jpg?w=630" alt=""   /></a></p>
<h2>Times are a Changing</h2>
<p>I remember walking into a team meeting my first day on the job and overhearing my fellow <a href="http://codedependents.com/author/johnjamesrussell/">blogger</a> explaining the RSS feed setup that he used to keep up with developer news. At the time, this was a new idea to a majority of the yet here we are, a short four years later, and the landscape is changing again. Today, if you aren&#8217;t on Twitter you are missing the conversation. While RSS remains an important tool in the struggle to remain on top of our discipline, the latest news, conversation, and ideas are happening on Twitter.</p>
<h2>Open Source</h2>
<p>We all know that open source and documentation have a sordid affair and this expands just as readily to blogs. If you are luck there is a business selling support for the project and they have a blog but there is hardly ever a blog for any apache project for instance. In stark contrast, many comitters are on twitter and talking about what they love, namely their project. Take Apache ActiveMQ for instance, a community that I&#8217;m fairly familiar with. There is no blog.  If you do some digging you can find a <a href="http://bsnyderblog.blogspot.com/">few</a> <a href="http://www.nighttale.net/">developer&#8217;s</a> <a href="http://rajdavies.blogspot.com/">blogs</a>, but these long form posts are few and far between. However, if you <a href="http://twitter.com/brucesnyder">join</a> <a href="http://twitter.com/ccustine">in</a> <a href="http://twitter.com/jstrachan">the</a> <a href="http://twitter.com/dejanb">conversation</a> <a href="http://twitter.com/gnodet">over</a> <a href="http://twitter.com/rajdavies">on</a> <a href="http://twitter.com/hiramchirino">Twitter</a> you are privy to up to the minute information about decisions, up coming features, latest tips and tricks, interesting news, and not to forget a willing group of guys to answer questions and help you along in using their product.</p>
<h2>Developer Trends</h2>
<p>There are two trends that I&#8217;m following closely in the development world, namely <a href="http://en.wikipedia.org/wiki/NoSQL">NoSQL</a> and <a href="http://en.wikipedia.org/wiki/DevOps">DevOps</a>. Both of these topics are changing and evolving on a daily basis and the only way to follow the conversation is via Twitter. Blog posts are often the <a href="http://pl.atyp.us/wordpress/?p=3068">topic of conversation</a> but to really be part of the conversation you have to join in where the people are. The DevOps crowd is something I&#8217;m only starting to get into but <a href="http://twitter.com/mmarschall">@mmarschall </a>and <a href="http://twitter.com/danackerson">@danackerson</a> were nice enough to put together a <a href="http://www.agileweboperations.com/20-devops-guys/">great list</a> of the who&#8217;s who. In the NoSql camp <a href="http://mashable.com/">Mashable</a> has a <a href="http://mashable.com/twitterlists/list/tlists/nosql/">good list</a> to get you started. However, thats not the end of it. There are people <a href="http://twitter.com/odersky">talking</a> <a href="http://twitter.com/djspiewak">about</a> <a href="http://twitter.com/codemonkeyism">scala</a>, <a href="http://twitter.com/unclebobmartin">pontificating</a> <a href="http://twitter.com/martinfowler">on</a> <a href="http://twitter.com/KevlinHenney">craftsmanship</a>, and more.</p>
<h2>Go Get &#8216;em</h2>
<p>The only way to keep up with the conversation is to join in so sign up or start following, share what you are reading, respond to others ideas, and while you&#8217;re at it, check me out <a href="http://www.twitter.com/bdarfler">@bdarfler</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/433/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=433&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2010/10/14/twitter-rss-how-to-keep-up-with-developers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://codedependents.files.wordpress.com/2010/10/group-of-people-talking.jpg" medium="image">
			<media:title type="html">group-of-people-talking</media:title>
		</media:content>
	</item>
		<item>
		<title>Monit alerts sent from a Dreamhost email account</title>
		<link>http://codedependents.com/2010/09/01/monit-alerts-sent-from-a-dreamhost-email-account/</link>
		<comments>http://codedependents.com/2010/09/01/monit-alerts-sent-from-a-dreamhost-email-account/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 14:35:11 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[alert]]></category>
		<category><![CDATA[dreamhost]]></category>
		<category><![CDATA[mailserver]]></category>
		<category><![CDATA[monit]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=428</guid>
		<description><![CDATA[This little snippet of configuration took way to long to figure out so I&#8217;m sharing it in hopes of saving others the trouble. If you are using monit and trying to send alerts from a Dreamhost email account you will need to use the following settings. set mailserver mail.your.host.name port 465 username "monit@your.host.name" password "password" [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=428&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This little snippet of configuration took way to long to figure out so I&#8217;m sharing it in hopes of saving others the trouble. If you are using monit and trying to send alerts from a Dreamhost email account you will need to use the following settings.</p>
<pre>
<div id="_mcePaste">set mailserver mail.your.host.name port 465</div>
<div id="_mcePaste">    username "monit@your.host.name"</div>
<div id="_mcePaste">    password "password"</div>
<div id="_mcePaste">    using SSLV3</div>
<div id="_mcePaste">    with timeout 15 seconds</div>
<div id="_mcePaste"></div>
<div id="_mcePaste">set alert alerts@your.host.name</div>
<div id="_mcePaste">    with mail-format { from: monit@your.host.name }</div></pre>
<p>It turns out that monit and Dreamhost don&#8217;t get along on the default port 587. The solution <a href="http://wiki.dreamhost.com/Secure_E-mail">according to Dreamhost</a> is to use port 465. This works as long as you use SSLv3 instead of TLSv1. The other important part of this configuration is making sure you set the from address in your mail format. Otherwise monit tries to send from monit@&lt;hostname&gt; which in most cases is not a fully qualified name and will cause Dreamhost to fail.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/428/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/428/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/428/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=428&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2010/09/01/monit-alerts-sent-from-a-dreamhost-email-account/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>
	</item>
		<item>
		<title>ConnectionFactories and Caching with Spring and ActiveMQ</title>
		<link>http://codedependents.com/2010/07/14/connectionfactories-and-caching-with-spring-and-activemq/</link>
		<comments>http://codedependents.com/2010/07/14/connectionfactories-and-caching-with-spring-and-activemq/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 14:19:50 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=359</guid>
		<description><![CDATA[In my previous two posts on ActiveMQ and Spring I've shown how to implement both asynchronous and synchronous messaging using MessageListenerContainers and JmsTemplates. Since then I have continued to tweak and improve my use of Spring's JMS support and have uncovered a few more details about the ConnectionFactories supplied by both ActiveMQ and Spring and how they play together nicely and not so nicely. Some of this info is new while some was covered in my second post and lead to an edit in my initial post but I will reiterate that information here to keep it all in one place.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=359&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 250px"><a href="http://www.flickr.com/people/seanryan/"><img title="Connection Factory" src="http://farm4.static.flickr.com/3099/2298180435_c3162ff1d8_m.jpg" alt="" width="240" height="160" /></a><p class="wp-caption-text">© Sean Ryan</p></div>
<p>In my previous <a href="http://codedependents.com/2009/10/16/efficient-lightweight-jms-with-spring-and-activemq/">two</a> <a href="http://codedependents.com/2010/03/04/synchronous-request-response-with-activemq-and-spring/">posts</a> on ActiveMQ and Spring I&#8217;ve shown how to implement both asynchronous and synchronous messaging using MessageListenerContainers and JmsTemplates. Since then I have continued to tweak and improve my use of Spring&#8217;s JMS support and have uncovered a few more details about the ConnectionFactories supplied by both ActiveMQ and Spring and how they play together nicely and not so nicely. Some of this info is new while some was covered in my second post and lead to an edit in my initial post but I will reiterate that information here to keep it all in one place.</p>
<h2><a href="http://activemq.apache.org/maven/activemq-pool/apidocs/org/apache/activemq/pool/PooledConnectionFactory.html">PooledConnectionFactory</a></h2>
<p>This bad boy is the ActiveMQ provide connection pooler. It pools connections, sessions and producers and does a fine job of it. There is nothing particularly wrong with this connection factory though it does strike me as odd that it caches connections since, according to the JMS spec, connections support concurrent use.</p>
<h2><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jms/connection/SingleConnectionFactory.html">SingleConnectionFactory</a></h2>
<p>It might seem odd to have a connection factory that ensures only one connection but in reality this is quite useful. When using multiple JmsTemplates and MessageListenerContainers in Spring they will end up each creating their own connection since the default ConnectionFactory will create a new connection for each createConnection() call. Initially I thought this would be fine but I ran into some very weird issues where producers and consumers couldn&#8217;t see each other. I&#8217;m not sure if this had something to do with using embedded brokers and the VM Transport but it wasn&#8217;t good. Using a SingleConnectionFactory as the initial decorator around the default ConnectionFactory solved these issues.</p>
<h2><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jms/connection/CachingConnectionFactory.html">CachingConnectionFactory</a></h2>
<p>This is the real heavy lifter. It will cache sessions and can optionally cache consumers and producers. As any post on JmsTemplate will tell you, you MUST use caching for any sort of reasonable performance if you are not running in a JCA container. As such, this is the factory to use when using a JmsTemplate. A few points should be made clear though. First, the default is for only one session to be cached. The javadoc claims this is sufficient for low concurrency but if you are doing something more high end you probably want to increase the SessionCacheSize. Additionally, there is some weirdness around cached consumers. They don&#8217;t get closed until they are removed from the pool and they are cached using a key that contains the destination and the selector. This lead to essentially a memory leak when trying to use cached consumers for request response semantics using JMSCorrelationIds. In general caching consumers is hard which is why MessageListenerContainers should be used instead.</p>
<h2>How&#8217;s it all work</h2>
<p><pre class="brush: xml;">
&lt;amq:connectionFactory id=&quot;connectionFactory&quot; brokerURL=&quot;${jms.url}&quot;/&gt;
&lt;bean id=&quot;singleConnectionFactory&quot; p:targetConnectionFactory-ref=&quot;connectionFactory&quot; p:reconnectOnException=&quot;true&quot;/&gt;
&lt;bean id=&quot;cachingConnectionFactory&quot; p:targetConnectionFactory-ref=&quot;singleConnectionFactory&quot; p:sessionCacheSize=&quot;100&quot;/&gt;
&lt;bean id=&quot;jmsTemplate&quot; p:connectionFactory-ref=&quot;cachingConnectionFactory&quot;/&gt;
&lt;jms:listener-container connection-factory=&quot;singleConnectionFactory&quot;&gt;
&lt;jms:listener id=&quot;QueueHandler&quot; destination=&quot;Queue&quot; ref=&quot;queueHandler&quot;/&gt;
&lt;/jms:listener-container&gt;
</pre></p>
<p>First we start with a basic ActiveMQ ConnectionFactory. From there we wrap it in a SingleConnectionFactory and then wrap that in a CachingConnectionFactory. I&#8217;ve increased the SessionCacheSize on the CachingConnectionFactory to 100, this might be overkill but my application is anything but memory hungry so I figured I could err on the side of too many sessions. Additionally, and this is important, ReconnectOnException is set to true on the SingleConnectionFactory. The CachingConnectionFactory requires this and will complain loudly but in such as way as to make it unclear what is going on if this is not set. From there we use the CachingConnectionFactory to create a JmsTemplate and we use the SingleConnectionFactory to create a MessageListenerContainer since it takes care of any caching of sessions and consumers that it needs.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/359/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=359&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2010/07/14/connectionfactories-and-caching-with-spring-and-activemq/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://farm4.static.flickr.com/3099/2298180435_c3162ff1d8_m.jpg" medium="image">
			<media:title type="html">Connection Factory</media:title>
		</media:content>
	</item>
		<item>
		<title>ActiveRecord Naming Conventions with Spring JDBC</title>
		<link>http://codedependents.com/2010/07/12/activerecord-naming-conventions-with-spring-jdbc/</link>
		<comments>http://codedependents.com/2010/07/12/activerecord-naming-conventions-with-spring-jdbc/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 18:51:34 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=328</guid>
		<description><![CDATA[One of the best choices we made at my current job was splitting the web development from the heavy message processing system, allowing the former to be written in Ruby on Rails and the latter to be written in Java. However, for ease of development, we allowed the two code bases share a single database. What then to do about database naming conventions? Those familiar with ActiveRecord will know that it is very particular about its database naming conventions so it was up to the Java code to play nicely. The first solution to present itself required hardcoding database names in each Java object but a more elegant solution seemed possible and, after the discovery of one small but important 3rd party project, it revealed itself.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=328&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://codedependents.files.wordpress.com/2010/07/java-versus-ruby-on-rails.jpg"><img class="alignleft size-full wp-image-389" title="java ruby on rails" src="http://codedependents.files.wordpress.com/2010/07/java-versus-ruby-on-rails.jpg?w=630" alt="java ruby on rails"   /></a>One of the best choices we made at my current job was splitting the web development from the heavy message processing system, allowing the former to be written in Ruby on Rails and the latter to be written in Java. However, for ease of development, we allowed the two code bases share a single database. What then to do about database naming conventions? Those familiar with ActiveRecord will know that it is very particular about its database naming conventions so it was up to the Java code to play nicely. The first solution to present itself required hardcoding database names in each Java object but a more elegant solution seemed possible and, after the discovery of one small but important 3rd party project, it revealed itself.</p>
<h2>Inflection</h2>
<p>In Rails, the class responsible for converting object names to database names is called <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html">Inflections</a>. For those unfamiliar with the term (as I was), <a href="http://en.wikipedia.org/wiki/Inflection">inflection</a> is the modification of a word to express different grammatical categories. Armed with this new knowledge I set about googling and was relieved to find that <a href="http://java.net/pub/au/294">Tom White</a> had already implemented a <a href="https://inflector.dev.java.net/">Java Inflector</a>. However, the Java Inflector only implements the pluralization aspect of the Rails Inflections; for this to work I was going to need some additional string manipulation tools. Luckily the old <a href="http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringUtils.html">Apache Commons StringUtils</a> was there to lend a helping hand. Finally, confident in my string manipulation tools, the next step was to look into the JDBC side of things.</p>
<h2>ORMs</h2>
<p>When dealing with mapping objects to and from a database <a href="http://www.hibernate.org/">Hibernate</a> is always the name that pops up. I&#8217;ve worked with it in the past and, while incredibly powerful, I always felt like I was wielding a hatchet when what I wanted was a surgical knife. I&#8217;ve heard its configuration has been simplified with JPA but its still a beast of a project that relies on a bit more magic than I&#8217;m comfortable with. In a more recent project I played with <a href="http://ibatis.apache.org/">iBatis</a> (now <a href="http://www.mybatis.org/">myBatis</a>) and enjoyed the simplicity but felt overwhelmed by the xml configuration. Version 3.0 supports annotation configuration which greatly simplifies this but I was looking for something based on naming conventions so I wouldn&#8217;t have to supply the mapping myself. I don&#8217;t mind getting down and dirty with some SQL so I opted for <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html">Spring JDBC</a>. Spring provides a super thin layer on top of raw JDBC which takes what is a fairly developer hostile API and makes it a breeze to use, all while adding some wonderful utilities to speed development.</p>
<h2>Putting It All Together</h2>
<h3>AbstractDbObj</h3>
<p>The first step was to create an AbstractDbObj to allow all other database objects to inherit from. This is where the logic for creating db names using the Inflector and StringUtils lives along with some other shared code.</p>
<p><pre class="brush: java;">
import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Collections2.transform;
import static org.apache.commons.lang.StringUtils.join;
import static org.apache.commons.lang.StringUtils.lowerCase;
import static org.apache.commons.lang.StringUtils.splitByCharacterTypeCamelCase;
import static org.jvnet.inflector.Noun.pluralOf;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;

public abstract class AbstractDbObj  implements DbObj {
  private static final String[] EXCLUDED_FIELD_NAMES = new String[] { &quot;columnNames&quot;, &quot;map&quot;, &quot;fieldNames&quot;, &quot;tableName&quot; };
  private static final Predicate&lt;String&gt; IS_COLUMN = new Predicate&lt;String&gt;() {
    private final ImmutableSet&lt;String&gt; notColumns = ImmutableSet.of( &quot;class&quot;, &quot;tableName&quot;, &quot;columnNames&quot;, &quot;fieldNames&quot; );
    public boolean apply( final String propName ) {
      return !notColumns.contains( propName );
    }
  };

  private static final ConcurrentMap&lt;Class&lt;? extends LocaDbObj&gt;, String&gt; TABLE_NAME_LOOKUP = new ConcurrentHashMap&lt;Class&lt;? extends LocaDbObj&gt;, String&gt;();

  private static final Function&lt;String, String&gt; TO_COLUMN = new Function&lt;String, String&gt;() {
    public String apply( final String fieldName ) {
      return underscore( fieldName );
    }
  };

  private static String tableName( final Class&lt;? extends LocaDbObj&gt; clazz ) {
    return pluralOf( underscore( clazz.getSimpleName() ) );
  }

  protected static String underscore( final String camelCasedWord ) {
    return lowerCase( join( splitByCharacterTypeCamelCase( camelCasedWord ), '_' ) );
  }

  private Integer id;
  private final Collection&lt;String&gt; fieldNames;
  private final String tableName;
  private final Map&lt;String, Object&gt; map;
  private final Collection&lt;String&gt; columnNames;

  @SuppressWarnings( &quot;unchecked&quot; )
  protected AbstractLocaDbObj() {
    super();
    final Class&lt;? extends LocaDbObj&gt; clazz = getClass();
    String tblName = TABLE_NAME_LOOKUP.get( clazz );
    if ( tblName == null ) {
      tblName = tableName( clazz );
      TABLE_NAME_LOOKUP.put( clazz, tblName );
    }
    tableName = tblName;
    final BeanMap beanMap = new BeanMap( this );
    fieldNames = filter( beanMap.keySet(), IS_COLUMN );
    columnNames = transform( fieldNames, TO_COLUMN );
    map = beanMap;
}

  @SuppressWarnings( &quot;unchecked&quot; )
  protected AbstractLocaDbObj( final String tableName ) {
    super();
    this.tableName = tableName;
    final BeanMap beanMap = new BeanMap( this );
    fieldNames = filter( beanMap.keySet(), IS_COLUMN );
    columnNames = transform( fieldNames, TO_COLUMN );
    map = beanMap;
  }

  @Override
  public boolean equals( final Object obj ) {
    return EqualsBuilder.reflectionEquals( this, obj, EXCLUDED_FIELD_NAMES );
  }

  public String getColumnName( final String fieldName ) {
    return underscore( fieldName );
  }

  public Collection&lt;String&gt; getColumnNames() {
    return columnNames;
  }

  public Collection&lt;String&gt; getFieldNames() {
    return fieldNames;
  }

  public Integer getId() {
    return id;
  }

  public String getTableName() {
    return tableName;
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode( this, EXCLUDED_FIELD_NAMES );
  }

  public void setId( final Integer id ) {
    this.id = id;
  }

  public Map&lt;String, Object&gt; toMap() {
    return map;
  }

  @Override
  public String toString() {
    return new ReflectionToStringBuilder( this ).setExcludeFieldNames(EXCLUDED_FIELD_NAMES ).toString();
  }
}
</pre></p>
<p>A few things to point out. First, I&#8217;m making use of the <a href="http://code.google.com/p/google-collections/">Google Collections</a> <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Function.html">Function</a> and <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Predicate.html">Predicate</a>. For more info on that check out the <a href="http://www.developer.com/open/article.php/10930_3735441_5/The-Google-Collections-Library.htm">developer.com article</a>. Second, I&#8217;m using <a href="http://commons.apache.org/beanutils/">Apache BeanUtils</a> <a href="http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/bean-collections/org/apache/commons/beanutils/BeanMap.html">BeanMap</a> which uses reflection to easily create a property to value map of a JavaBean. Finally, I&#8217;m storing the table names in a lookup since the pluralization a very expensive operation. The rest should hopefully be somewhat straight forward.</p>
<h3>SimpleJdbcInsert</h3>
<p>Using Spring&#8217;s <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-simple-jdbc-insert-1">SimpleJdbcInsert</a>, combined with the heavy lifting in the abstract class, insertions are practically free as you can see in the following code.</p>
<p><pre class="brush: java;">
// Get Datasource from Spring Context
SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert( dataSource ).withTableName( dbObj.getTableName() ).usingGeneratedKeyColumns( &quot;id&quot; );
final int id = jdbcInsert.executeAndReturnKey( new BeanPropertySqlParameterSource( dbObj ) ).intValue();
dbObj.setId( id );
</pre></p>
<p>Note: you can reuse a SimpleJdbcInsert since it is thread safe, I created a new one in the above code just to show how it is done.</p>
<h3>SimpleJdbcTemplate</h3>
<p>After inserts, the other two pieces of required functionality are query and update, both of which are supplied by Spring&#8217;s <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-SimpleJdbcTemplate">SimpleJdbcTemplate</a>. The trick here is to make generous use of Spring&#8217;s <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/core/namedparam/BeanPropertySqlParameterSource.html">BeanPropertySqlParameterSource</a> and <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/core/BeanPropertyRowMapper.html">BeanPropertyRowManager</a> which effortlessly map between JavaBean properties and database columns.</p>
<p>Lets start with the simple case of deleting an object.</p>
<p><pre class="brush: java;">

simpleJdbcTemplate.update( &quot;DELETE FROM &quot; + dbObj.getTableName() + &quot; WHERE id = :id&quot;, new BeanPropertySqlParameterSource( dbObj ) );

</pre></p>
<p>You&#8217;ll notice the string &#8220;id = :id&#8221; in the query. This allows Spring to do value substitution, using the BeanPropertySqlParameterSource to replace :id with the actual value of .getId() from the dbObj. With this in mind we can step up to the more complicated update case.</p>
<p><pre class="brush: java;">
final Collection&lt;String&gt; parts = buildUpdateParts( dbObj );
if ( parts.isEmpty() ) { return; }
final String query = &quot;UPDATE &quot; + dbObj.getTableName() + &quot; SET &quot; + join( parts, &quot;, &quot; ) + &quot; WHERE id = :id&quot;;
simpleJdbcTemplate.update( query, new BeanPropertySqlParameterSource( dbObj ) );
</pre><br />
<pre class="brush: java;">
private static final Collection&lt;String&gt; buildUpdateParts( final LocaDbObj dbObj ) {
  return transform( filter( dbObj.getFieldNames(), not( IS_ID ) ), new BuildPart( dbObj ) );
}
</pre><br />
<pre class="brush: java;">
private static final class BuildPart implements Function&lt;String, String&gt; {
  private final LocaDbObj dbObj;
  public BuildPart( final LocaDbObj dbObj ) {
    this.dbObj = dbObj;
  }
  public String apply( final String propName ) {
    return dbObj.getColumnName( propName ) + &quot; = :&quot; + propName;
  }
}
</pre></p>
<p>This code again makes use of google collections but the point is that it takes the field names and the column names from the DbObj and creates snips of sql setting the column name to the field name identifier (i.e. the field name prefixed with &#8216;:&#8217;) just like was done in the deletion case with &#8220;id = :id&#8221;. Continuing to build on the previous examples we finally come to the query case. This one is a bit difference since we are interested in reading results from the database as well as building a query so we will have to deal with the BeanPropertyRowMapper.</p>
<p><pre class="brush: java;">
final Collection&lt;String&gt; parts = buildQueryParts( dbObj );
if ( parts.isEmpty() ) { return ImmutableList.of(); }
final String query = &quot;SELECT &quot; + columnNames + &quot; FROM &quot; + dbObj.getTableName() + &quot; WHERE &quot; + join( parts, &quot; AND &quot; );
return result = simpleJdbcTemplate.query( query, new BeanPropertyRowMapper&lt;DbObj&gt;( dbObj.getClass() ), new BeanPropertySqlParameterSource( dbObj ) );
</pre><br />
<pre class="brush: java;">
private static final Collection&lt;String&gt; buildQueryParts( final LocaDbObj dbObj ) {
  return transform( filter( dbObj.getFieldNames(), new HasValue( dbObj.toMap() ) ), new BuildPart( dbObj ) );
}
</pre><br />
<pre class="brush: java;">
private static final class HasValue implements Predicate&lt;String&gt; {
  private final Map&lt;String, Object&gt; map;
  public HasValue( final Map&lt;String, Object&gt; map ) {
    this.map = map;
  }
  public boolean apply( final String propName ) {
    return map.get( propName ) != null;
  }
}
</pre></p>
<p>Hopefully by this point the google collections syntax is becoming familiar. We want to include only object fields that are non null so we use the HasValue Predicate to check each property by name. From there on the rest is quite similar to the update method where we build parts setting each column name equal to its field name identifier. The only other difference is we use the BeanPropertyRowMapper so Spring can return us a list of the correct type of DbObj.</p>
<p>While certainly a bit heavy on the code I hope this will be useful to those trying to create a simple, straight forward database layer with Spring and ActiveRecord naming conventions. As a bit of a tease, the full code base that this is pulled from contains an genericized DAO object that contains basic annotation driven caching as well as the ability to execute CRUD commands via example objects, ids, sql snippets (i.e. where clauses), or full raw SQL all while keeping things nice and type safe as well as sql injection free using PreparedStatements and value substitution.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/328/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=328&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2010/07/12/activerecord-naming-conventions-with-spring-jdbc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://codedependents.files.wordpress.com/2010/07/java-versus-ruby-on-rails.jpg" medium="image">
			<media:title type="html">java ruby on rails</media:title>
		</media:content>
	</item>
		<item>
		<title>Approximating Java Case Objects without Project Lombok</title>
		<link>http://codedependents.com/2010/07/09/approximating-java-case-objects-without-project-lombok/</link>
		<comments>http://codedependents.com/2010/07/09/approximating-java-case-objects-without-project-lombok/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 14:20:55 +0000</pubDate>
		<dc:creator>Benjamin Darfler</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[apache commons]]></category>
		<category><![CDATA[case objects]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[project lombok]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=349</guid>
		<description><![CDATA[Over the past few months Dick Wall of the Java Posse has been talking up Project Lombok and for good reason. As he points out, its great for reducing boilerplate code in basic Java data objects. However, the more important point that Dick makes is that it prevents stupid errors, particularly when adding new object fields later on in the development cycle. The only unfortunate issue is that Project Lombok requires a compiler hack that can be rather confusing to those not using a supported IDE or for those coding outside of an IDE. However, Apache Commons provides an elegant solution to this issue.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=349&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://codedependents.files.wordpress.com/2010/07/800px-large_cayenne.jpg"><img class="alignleft size-thumbnail wp-image-374" title="Lombok" src="http://codedependents.files.wordpress.com/2010/07/800px-large_cayenne.jpg?w=150&#038;h=71" alt="Lombok" width="150" height="71" /></a>Over the past few months <a href="http://dickwallsblog.blogspot.com/">Dick Wall</a> of the <a href="http://javaposse.com/">Java Posse</a> has been talking up <a href="http://projectlombok.org/">Project Lombok</a> and for good reason. As he points out, its great for reducing boilerplate code in basic Java data objects. However, the more important point that Dick makes is that it prevents stupid errors, particularly when adding new object fields later on in the development cycle. The only unfortunate issue is that Project Lombok requires a compiler hack that can be rather confusing to those not using a supported IDE or for those coding outside of an IDE. However, Apache Commons provides an elegant solution to this issue.</p>
<h2>Project Lombok</h2>
<p>Lets start with a quick overview of Project Lombok. At its most basic level it provides a set of annotations (i.e. @Data) that you add to your Java data objects. You simply create private fields in an annotated object and the annotation will cause a full set of constructors, getters/setters, equals/hashCode and toString to be generated in the class file at compile time while keeping a simple and minimal source file. Its an elegant solution but it comes with a fair amount of magic surrounding it.</p>
<h2>Alternatives</h2>
<p>The simple alternative to Project Lombok is to have the IDE generate the code for you. Eclipse, for instance, is very good at doing this with auto generation of constructors, getters/setters, hashCode/equals and toString. This works wonders when creating the object for the first time but, as Dick points out, its too easy to add a new field and forget about updating toString and hashCode/equals leading to confusing and subtle bugs down the road.</p>
<p>The other alternative, and the one I am promoting, strikes a balance between Project Lombok and IDE generation by auto generating the hashCode/equals and toString and leaving the getters/setters and constructors up to the developer as these are necessary for the new field to be of any use.</p>
<h2>Apache Commons</h2>
<p>Buried deep in the Apache Commons is the <a href="http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/builder/package-summary.html">builder package</a> containing utilities such as EqualsBuilder, HashCodeBuilder, and ReflectionToStringBuilder. I don&#8217;t remember when I first discovered these gems but once I did the lightbulb went off; if used them in an Abstract base class they would ensure proper implementations of equals/hashCode and toString without having to mess with every data object I create.</p>
<p><pre class="brush: java;">
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public abstract class AbstractBaseObj {

    @Override
    public boolean equals( final Object obj ) {
        return EqualsBuilder.reflectionEquals( this, obj );
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode( this );
    }

    @Override
    public String toString() {
        return new ReflectionToStringBuilder( this).toString();
    }
}
</pre></p>
<h2>Drawbacks</h2>
<p>Of course this option is not with out its own drawbacks compared to Project Lombok. First off, as mentioned before, it does not deal with getters/setters or constructors. However, as I pointed out, I don&#8217;t believe this to be a huge imposition since creating a new field without creating getters/setters and/or a constructor using that field is fairly useless. Additionally, I like that this gives ease and flexibility over which constructors are created. This has been quite handy in the most recent project I&#8217;ve been working on.</p>
<p>Another draw back is that it requires an Abstract base class from which all objects must inherit. Annotations are certainly a more elegant solution since it doesn&#8217;t require a given object hierarchy. However there are often good reasons for a project specific base object for reasons beyond just equals/hashCode and toString. Additionally, the base class is fully owned by the project with just the few methods necessary being implemented. This is significantly better than having to inherit from a class provided by some 3rd party jar.</p>
<p>The important drawback, however,  is that the Apache Commons solution uses reflection instead of actually generating code to implement equals/hashCode and toString. Depending on how frequently these are used and how performant they need to be this might be a real consideration to use either Project Lombok or to write all the code directly. However, I have not found this to be the case in my project as it never seems to show up as a hot spot in any profiling I have done.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/349/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=349&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2010/07/09/approximating-java-case-objects-without-project-lombok/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e0132e931569bbd7d6ad6ef56a6f11db?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bdarfler</media:title>
		</media:content>

		<media:content url="http://codedependents.files.wordpress.com/2010/07/800px-large_cayenne.jpg?w=150" medium="image">
			<media:title type="html">Lombok</media:title>
		</media:content>
	</item>
		<item>
		<title>Typed variables in an untyped world</title>
		<link>http://codedependents.com/2010/05/03/typed-variables-in-an-untyped-world/</link>
		<comments>http://codedependents.com/2010/05/03/typed-variables-in-an-untyped-world/#comments</comments>
		<pubDate>Mon, 03 May 2010 20:40:43 +0000</pubDate>
		<dc:creator>John Russell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://codedependents.com/?p=355</guid>
		<description><![CDATA[First, off, I&#8217;m a static type person.  Sorry, but I like me some static typing. It makes me feel warm and squishy.  It makes my IDE more useful.  I can refactor an entire code base and go to sleep and not have nightmares. Moving on, I&#8217;ve been using Groovy for unit testing a lot lately [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=355&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>First, off, I&#8217;m a static type person.  Sorry, but I like me some static typing. It makes me feel warm and squishy.  It makes my IDE more useful.  I can refactor an entire code base and go to sleep and not have nightmares.</p>
<p>Moving on, I&#8217;ve been using Groovy for unit testing a lot lately and have decided  something. I like it. Its really nice to not have to deal with exceptions and be able to create lists and maps inline.  Closures kick butt.  Its really nice and easy. However, the lack of types that enable some of the cool things bug me, but they don&#8217;t have to.</p>
<p>Because groovy is a dynamic language you don&#8217;t have to provide the type of any given variable. This can be good and bad. Its sometimes convenient when you don&#8217;t know or don&#8217;t care what the type of a variable is.  However, in most cases, when you do know what the variable type is, it can be helpful to put type the variable.</p>
<p><code>// untyped, but it is really an ArrayList<br />
def someVariable = ["1","2","3"]</code></p>
<p><code>// same idea but now you know the type<br />
List someList = ["1","2","3"]<br />
</code><br />
Now, of course, you could have deduced the type of <strong>someVariable</strong> given its assignment, but apparently your IDE is not that smart, at least Eclipse isn&#8217;t. Especially if its one of my types and not a built in type.  One of the main reasons that I type variables is that if you explicitly type them, Eclipse can do auto completion on the method names of your variable.  If you use <strong>def</strong> to declare your variable Eclipse only gives you the default <strong>GroovyObject</strong> methods.  Not the best.</p>
<p>Also, providing types with method signatures makes it FAR FAR FAR more obvious what you are expecting of your caller.</p>
<p>Consider a method with the signature:</p>
<p><code>def someMethod(def thing1, def thing2, def thing3){<br />
...<br />
}<br />
</code><br />
Mmmmmmyeah. I have no idea what to pass to that thing. I also don&#8217;t know what its going to give back to me.</p>
<p>Don&#8217;t get me wrong, there are times when you don&#8217;t know some of these things or its more convenient not to declare them statically, but I have found that if you actually know at compile time what something is going to be, you may want to grease back your hair with your back pocket comb in your black leather jacket and stick it to the man with a quick <strong>def</strong>, but you from two weeks in the future and the next guy to read your code will be much happier if you throw a few types in there.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codedependents.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codedependents.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codedependents.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codedependents.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codedependents.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codedependents.wordpress.com/355/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codedependents.wordpress.com/355/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codedependents.wordpress.com/355/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codedependents.com&amp;blog=7973493&amp;post=355&amp;subd=codedependents&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codedependents.com/2010/05/03/typed-variables-in-an-untyped-world/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c75369f25cd6b34076875bdc308c2aca?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">johnjamesrussell</media:title>
		</media:content>
	</item>
	</channel>
</rss>
