<?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:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;Ck4BSHY5fyp7ImA9WhRWFkU.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539</id><updated>2012-01-04T14:42:39.827+02:00</updated><category term="craftmanship" /><category term="MailSender" /><category term="ant" /><category term="Feature-Flags" /><category term="dozer" /><category term="structure 101" /><category term="javabean" /><category term="immutable" /><category term="spring-integration" /><category term="OXM" /><category term="CI" /><category term="props" /><category term="value-object" /><category term="SimpleMailMessage" /><category term="mapping" /><category term="complexity" /><category term="SI" /><category term="properties" /><category term="agile" /><category term="Polymorphism" /><category term="spring" /><category term="design" /><category term="email" /><category term="quality" /><category term="JMS" /><category term="JavaMail" /><category term="architecture" /><title>&lt;Code/&gt; $lut</title><subtitle type="html">Java, Java EE, code samples, best practices, software design, etc...</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://codeslut.blogspot.com/" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/codeslut" /><feedburner:info uri="codeslut" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;Dk4MSXs5fyp7ImA9WhZaGEg.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-4203459817010119</id><published>2011-07-03T09:29:00.004+03:00</published><updated>2011-07-05T11:36:28.527+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-05T11:36:28.527+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="spring" /><category scheme="http://www.blogger.com/atom/ns#" term="design" /><category scheme="http://www.blogger.com/atom/ns#" term="Feature-Flags" /><category scheme="http://www.blogger.com/atom/ns#" term="Polymorphism" /><title>Feature Flags made easy</title><content type="html">&lt;p&gt;
I recently participated in the &lt;a href="http://www.iltt.org.il/"&gt;ILTechTalk&lt;/a&gt; week. Most of the talks discussed issues like Scalability, Software Quality, Company Culture, and Continues Deployment (CD). Since the talks were hosted at &lt;a href="http://www.outbrain.com"&gt;Outbrain&lt;/a&gt;, we got many direct questions about our concrete implementations. Some of the questions and statements claimed that Feature Flags complicate your code. What bothered most participants was that committing code directly to trunk requires addition of feature flags in some cases, and that it may make their code base more complex.
&lt;/p&gt;
&lt;p&gt;
While in some cases feature flags may make the code slightly more complicated, it shouldn't be so in most cases. The main idea I'm presenting here is that conditional logic can be easily replaced with polymorphic code. In fact conditional logic can &lt;b&gt;always&lt;/b&gt; be replaced by polymorphism.
&lt;/p&gt;
&lt;p&gt;
Enough with the abstract talk...
&lt;/p&gt;
&lt;p&gt;
Suppose we have an application that contains some imaginary feature, and we want to introduce a feature flag. Below is a code snippet that developers normally come up with:
&lt;/p&gt;
&lt;p&gt;
&lt;script src="https://gist.github.com/1060044.js?file=IfElseApplication.java"&gt;&lt;/script&gt;
&lt;/p&gt;
&lt;p&gt;
While this is a legitimate implementations in some cases, it does complicate your code base by increasing the &lt;a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity"&gt;cyclomatic complexity&lt;/a&gt; of your code. In some cases the test for activation of the feature may recur in many place in the code, so this approach can quickly turn into a maintenance nightmare.
&lt;/p&gt;
&lt;p&gt;
Luckily, implementing a feature flag using polymorphism is pretty easy. First lets define an interface for the imaginary feature, and two implementations (old and new):
&lt;/p&gt;
&lt;p&gt;
&lt;script src="https://gist.github.com/1060044.js?file=ImaginaryFeature.java"&gt;&lt;/script&gt;
&lt;/p&gt;
&lt;p&gt;
Now lets use the feature in our application, selecting the implementation at runtime:
&lt;/p&gt;
&lt;p&gt;
&lt;script src="https://gist.github.com/1060044.js?file=PolymorphicApplication.java"&gt;&lt;/script&gt;
&lt;/p&gt;
&lt;p&gt;
Here we initialized the imaginary feature member by reflection, using a class name specified as a system property. The &lt;i&gt;createImaginaryFeature()&lt;/i&gt; method above is usually abstracted into a factory, but kept as is here for brevity. But we're still not done. Most of the readers would probably say that the introduction of a factory and reflection makes the code less readable and less maintainable. I have to agree... And apart from that, adding dependencies to the concrete implementations will complicate the code even more. Luckily I have a secret weapon at my disposal. It is called &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;IoC&lt;/a&gt;, (or DI). When using an IoC container such as &lt;a href="http://www.springsource.org/"&gt;Spring&lt;/a&gt; or &lt;a href="http://code.google.com/p/google-guice/"&gt;Guice&lt;/a&gt;, your code can be made extremely flexible, and implementing feature flags is turned into a walk in the park.
&lt;/p&gt;
&lt;p&gt;
Below is a rewrite of the PolymorphicApplication using Spring dependency injection:
&lt;/p&gt;
&lt;p&gt;
&lt;script src="https://gist.github.com/1060044.js?file=SpringPolymorphicApplication.java"&gt;&lt;/script&gt;

&lt;script src="https://gist.github.com/1060044.js?file=ApplicationContext.xml"&gt;&lt;/script&gt;
&lt;/p&gt;
&lt;p&gt;
The spring code above defines a application and 2 imaginary feature implementations. By default the application is initialized with the &lt;i&gt;oldImaginaryFeature&lt;/i&gt;, but this behavior can be overridden by specifying a &lt;i&gt;-DimaginaryFeature.implementation.bean=newImaginaryFeature&lt;/i&gt; command line argument. Only a single feature implementation will be initialized by Spring, and the implementations may have dependencies.
&lt;/p&gt;
&lt;p&gt;
Bottom line is: with a bit of extra preparation, and correct design decisions, feature flags shouldn't be a burden on your code base. By extra preparation, I mean extracting interfaces for your domain objects, using an IoC container, etc, which is something we should be doing in most cases anyway.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-4203459817010119?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/EZE9052LMPc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/4203459817010119/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2011/07/feature-flags-made-easy.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/4203459817010119?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/4203459817010119?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/EZE9052LMPc/feature-flags-made-easy.html" title="Feature Flags made easy" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2011/07/feature-flags-made-easy.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkIDQHY4eSp7ImA9WxFQGEk.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-7937825736609085552</id><published>2010-05-14T16:19:00.002+03:00</published><updated>2010-05-14T16:42:51.831+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-05-14T16:42:51.831+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="OXM" /><category scheme="http://www.blogger.com/atom/ns#" term="mapping" /><category scheme="http://www.blogger.com/atom/ns#" term="javabean" /><category scheme="http://www.blogger.com/atom/ns#" term="immutable" /><category scheme="http://www.blogger.com/atom/ns#" term="value-object" /><category scheme="http://www.blogger.com/atom/ns#" term="dozer" /><title>Mapping Immutable Value-Objects with Dozer</title><content type="html">&lt;p&gt;
2 good things happened to me this week (well actually 3, but I will probably blog about the 3rd one later):
&lt;/p&gt;

&lt;p&gt;
The first thing is that I finally managed to convince &lt;a href="http://dozer.sourceforge.net"&gt;Dozer&lt;/a&gt; to map immutable objects, and the second one is that I found something interesting to blog about ;)
&lt;/p&gt;

&lt;p&gt;
I mentioned in a &lt;a href="http://codeslut.blogspot.com/2009/12/value-objects-mapping.html"&gt;previous post&lt;/a&gt; about Dozer’s lack of support for constructor arguments. In general Dozer is aimed at supporting JavaBean to JavaBean mapping, and other usage scenarios seem to be hard to implement. It turns out that the problem can be solved using design-patterns, and a little bit of trickery.
&lt;/p&gt;

&lt;p&gt;
The first step towards the solution, is introducing the Builder Pattern. Actually a form of the Builder Pattern that introduced by Joshua Bloch at Java One. The pattern solves the problem of too many constructors, too many constructor arguments, and the verbosity of object creation while using setters. The pattern is described in detail here: &lt;a href="http://ow.ly/1L2JV"&gt;http://ow.ly/1L2JV&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
Let’s suppose we are about to map an Address JavaBean to an Immutable Address object. Here are the Address classes:
&lt;pre class="codesnip"&gt;
public class Coordinate {
 private double longitude;
 private double latitude;
 // getters, setters, c'tors, equals(), hashCode(), toString(), etc...
}
&lt;/pre&gt;
&lt;pre class="codesnip"&gt;
public class Address {
 private String country;
 private String state;
 private String city;
 private String street;
 private String zipcode;
 private Coordinate coordinate;
 // getters, setters, c'tors, equals(), hashCode(), toString(), etc...
}
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;
And here’s the immutable address:
&lt;pre class="codesnip"&gt;
public class ImmutableCoordinate {
 private final double longitude;
 private final double latitude;
 
 private ImmutableCoordinate(Builder builder) {
  this.latitude = builder.latitude;
  this.longitude = builder.longitude;
 }

 public double getLongitude() {
  return longitude;
 }
 
 public double getLatitude() {
  return latitude;
 }
  
 public static class Builder {
  private double longitude;
  private double latitude;
  
  public Builder longitude(double longitude) {
   this.longitude = longitude;
   return this;
  }
  
  public Builder latitude(double latitude) {
   this.latitude = latitude;
   return this;
  }
  
  public ImmutableCoordinate build() {
   return new ImmutableCoordinate(this);
  }
 }
}
&lt;/pre&gt;
&lt;pre class="codesnip"&gt;
public class ImmutableAddress {
 private final String country;
 private final String state;
 private final String city;
 private final String street;
 private final String zipcode;
 private final ImmutableCoordinate coordinate;
 
 private ImmutableAddress(Builder builder) {
  this.country = builder.country;
  this.state = builder.state;
  this.city = builder.city;
  this.street = builder.street;
  this.zipcode = builder.zipcode;
  this.coordinate = builder.coordinate;  
 }

 public String getCountry() {
  return country;
 }
 
 public String getState() {
  return state;
 }
  
 public String getCity() {
  return city;
 }
 
 public String getStreet() {
  return street;
 }
  
 public String getZipcode() {
  return zipcode;
 }
 
 public ImmutableCoordinate getCoordinate() {
  return coordinate;
 }
  
 public static class Builder {
  private String country;
  private String state;
  private String city;
  private String street;
  private String zipcode;
  private ImmutableCoordinate coordinate;
    
  public Builder country(String country) {
   this.country = country;
   return this;
  }

  public Builder state(String state) {
   this.state = state;
   return this;
  }

  public Builder city(String city) {
   this.city = city;
   return this;
  }

  public Builder street(String street) {
   this.street = street;
   return this;
  }

  public Builder zipcode(String zipcode) {
   this.zipcode = zipcode;
   return this;
  }

  public Builder coordinate(ImmutableCoordinate coordinate) {
   this.coordinate = coordinate;
   return this;
  }
  
  public ImmutableCoordinate getCoordinate() {
   return coordinate;
  }

  public ImmutableAddress build() {
   return new ImmutableAddress(this);
  }
 }
}
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;
Now, by we can map our mutable class to the Builder of the immutable class, and throw in a custom &lt;i&gt;DozerConverter&lt;/i&gt; where nested properties are involved. Below is the mapping for the Address classes:
&lt;pre class="codesnip"&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;
&amp;lt;mappings xmlns=&amp;quot;http://dozer.sourceforge.net&amp;quot;
          xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;
          xsi:schemaLocation=&amp;quot;http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd&amp;quot;&amp;gt;

 &amp;lt;configuration&amp;gt;
  &amp;lt;stop-on-errors&amp;gt;true&amp;lt;/stop-on-errors&amp;gt;
  &amp;lt;date-format&amp;gt;MM/dd/yyyy HH:mm&amp;lt;/date-format&amp;gt;
  &amp;lt;wildcard&amp;gt;true&amp;lt;/wildcard&amp;gt;
 &amp;lt;/configuration&amp;gt;
 
 &amp;lt;mapping&amp;gt;
  &amp;lt;class-a&amp;gt;location.Coordinate&amp;lt;/class-a&amp;gt;
  &amp;lt;class-b&amp;gt;location.ImmutableCoordinate$Builder&amp;lt;/class-b&amp;gt;
  
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;setLongitude&amp;quot;&amp;gt;longitude&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;longitude&amp;quot;&amp;gt;longitude&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;setLatitude&amp;quot;&amp;gt;latitude&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;latitude&amp;quot;&amp;gt;latitude&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
 &amp;lt;/mapping&amp;gt;

 &amp;lt;mapping&amp;gt;
  &amp;lt;class-a&amp;gt;location.Address&amp;lt;/class-a&amp;gt;
  &amp;lt;class-b&amp;gt;location.ImmutableAddress$Builder&amp;lt;/class-b&amp;gt;
  
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;setCountry&amp;quot;&amp;gt;country&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;country&amp;quot;&amp;gt;country&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;setState&amp;quot;&amp;gt;state&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;state&amp;quot;&amp;gt;state&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;setCity&amp;quot;&amp;gt;city&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;city&amp;quot;&amp;gt;city&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;street&amp;quot;&amp;gt;street&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;street&amp;quot;&amp;gt;street&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
  &amp;lt;field&amp;gt;
   &amp;lt;a set-method=&amp;quot;setZipcode&amp;quot;&amp;gt;zipcode&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;zipcode&amp;quot;&amp;gt;zipcode&amp;lt;/b&amp;gt;   
  &amp;lt;/field&amp;gt;
  &amp;lt;field custom-converter-id=&amp;quot;coordConverter&amp;quot;&amp;gt;
   &amp;lt;a set-method=&amp;quot;setCoordinate&amp;quot;&amp;gt;coordinate&amp;lt;/a&amp;gt;
   &amp;lt;b set-method=&amp;quot;coordinate&amp;quot;&amp;gt;coordinate&amp;lt;/b&amp;gt;
  &amp;lt;/field&amp;gt;
 &amp;lt;/mapping&amp;gt;
&amp;lt;/mappings&amp;gt;
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;
And the DozerConverter is a fairly straight forward implementation (I actually use Dozer to do its own job…):
&lt;pre class="codesnip"&gt;
public class CoordinateConverter extends DozerConverter&lt;Coordinate, ImmutableCoordinate&gt; {

 private final Mapper mapper;
  
 public CoordinateConverter(Mapper mapper) {
  super(Coordinate.class, ImmutableCoordinate.class);
  this.mapper = mapper;
 } 
 
 @Override
 public Coordinate convertFrom(ImmutableCoordinate source, Coordinate destination) {  
  return mapper.map(source, Coordinate.class);
 }
 
 @Override
 public ImmutableCoordinate convertTo(Coordinate source, ImmutableCoordinate destination) {
  return source == null ? null : mapper.map(source, ImmutableCoordinate.Builder.class).build();
 }
}
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;
Now mapping between the classes is a matter of a single line:
&lt;pre class="codesnip"&gt;
Address address = new Address();
// set set set...
  
ImmutableAddress immutableAddress = mapper.map(address, ImmutableAddress.Builder.class).build();
&lt;/pre&gt;

And it even works in the opposite direction :D 
&lt;/p&gt;

&lt;p&gt;
Although this solution is not as clean as it should have been – there’s still some over verbosity, and an obscure need for a getter in some cases, it is still preferable over the piles of code you get when messing with object mapping. This technique may also be easier to sneak into the Dozer code-base than constructor arguments support.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-7937825736609085552?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/TZI1L5CX8zE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/7937825736609085552/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2010/05/mapping-immutable-value-objects-with.html#comment-form" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/7937825736609085552?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/7937825736609085552?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/TZI1L5CX8zE/mapping-immutable-value-objects-with.html" title="Mapping Immutable Value-Objects with Dozer" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>6</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2010/05/mapping-immutable-value-objects-with.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0IARXc8fyp7ImA9WxFTF08.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-7247822767626998834</id><published>2010-04-08T14:10:00.002+03:00</published><updated>2010-04-08T14:19:04.977+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-04-08T14:19:04.977+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="MailSender" /><category scheme="http://www.blogger.com/atom/ns#" term="email" /><category scheme="http://www.blogger.com/atom/ns#" term="spring" /><category scheme="http://www.blogger.com/atom/ns#" term="SimpleMailMessage" /><category scheme="http://www.blogger.com/atom/ns#" term="JavaMail" /><title>Sending Emails using Spring-Mail</title><content type="html">&lt;p&gt;
Some applications are required to send emails. What can I say? These are the things we have to do for money…
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://java.sun.com/products/javamail/index.jsp"&gt;JavaMail&lt;/a&gt; API is pretty much boring and a little cumbersome to use. Once again you find yourself fiddling with connection management, exception handling, etc. And once again Spring comes to the rescue :)
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mail.html"&gt;Spring-Mail&lt;/a&gt; has neat and easy to use email API, including MIME messages support.
&lt;/p&gt;
&lt;p&gt;
Let’s imagine we need to implement “forgot my password” feature. So here goes.
&lt;br/&gt;
The EmailFacade:
&lt;pre class="codesnip"&gt;
package mail;

public interface EmailFacade {

    public void sendPasswordReminderEmailTemplate(String user, String password, String email);
}
&lt;/pre&gt;

The implementations:
&lt;pre class="codesnip"&gt;
package mail;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.util.Assert;


class EmailFacadeImpl implements EmailFacade {

    private final MailSender mailSender;
    private final SimpleMailMessage passwordReminderEmailTemplate;


    public EmailFacadeImpl(MailSender mailSender, SimpleMailMessage passwordReminderEmailTemplate) {
        Assert.notNull(mailSender, "mailSender may not be null");
        Assert.notNull(passwordReminderEmailTemplate,
                       "passwordReminderEmailTemplate may not be null");

        this.mailSender = mailSender;
        this.passwordReminderEmailTemplate = passwordReminderEmailTemplate;
    }

    public void sendPasswordReminderEmailTemplate(String user, String password, String email) {
        // Create a thread safe instance of the template message and customize it
        SimpleMailMessage msg = new SimpleMailMessage(passwordReminderEmailTemplate);
        String formatedText = String.format(passwordReminderEmailTemplate.getText(), user, password);
        msg.setText(formatedText);
        msg.setTo(email);
        mailSender.send(msg);
    }
}
&lt;/pre&gt;

And the Spring Configuration:
&lt;pre class="codesnip"&gt;
  &amp;lt;bean id=&amp;quot;emailFacade&amp;quot; class=&amp;quot;mail.EmailFacadeImpl&amp;quot;&amp;gt;
        &amp;lt;constructor-arg&amp;gt;
     &amp;lt;bean class=&amp;quot;org.springframework.mail.javamail.JavaMailSenderImpl&amp;quot;&amp;gt;
   &amp;lt;property name=&amp;quot;host&amp;quot; value=&amp;quot;${emailServerURL}&amp;quot;/&amp;gt;
   &amp;lt;property name=&amp;quot;username&amp;quot; value=&amp;quot;${emailPrincipal}&amp;quot;/&amp;gt;
   &amp;lt;property name=&amp;quot;password&amp;quot; value=&amp;quot;${emailPassword}&amp;quot;/&amp;gt;
     &amp;lt;/bean&amp;gt;
        &amp;lt;/constructor-arg&amp;gt;
        &amp;lt;constructor-arg ref=&amp;quot;passwordReminderEmailTemplate&amp;quot;/&amp;gt;
  &amp;lt;/bean&amp;gt; 
 
  &amp;lt;bean id=&amp;quot;passwordReminderEmailTemplate&amp;quot; class=&amp;quot;org.springframework.mail.SimpleMailMessage&amp;quot;&amp;gt;
    &amp;lt;property name=&amp;quot;from&amp;quot; value=&amp;quot;me@mycompany.com&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;subject&amp;quot; value=&amp;quot;Password Reminder&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;text&amp;quot;&amp;gt;
      &amp;lt;!-- Text template to be used with String.format() --&amp;gt;
      &amp;lt;value&amp;gt;&amp;lt;![CDATA[Hi %s,
Your password is
%s]]&amp;gt;
      &amp;lt;/value&amp;gt;
    &amp;lt;/property&amp;gt;
  &amp;lt;/bean&amp;gt;
&lt;/pre&gt;

TADA!
&lt;p&gt;
For some use cases it would be better to replace the String.format() call with some templating engine such as &lt;a href="http://velocity.apache.org/"&gt;Velocity&lt;/a&gt;. I left it here for brevity.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-7247822767626998834?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/h4xwiuJIwAA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/7247822767626998834/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2010/04/sending-emails-using-spring-mail.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/7247822767626998834?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/7247822767626998834?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/h4xwiuJIwAA/sending-emails-using-spring-mail.html" title="Sending Emails using Spring-Mail" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2010/04/sending-emails-using-spring-mail.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UEQH04fSp7ImA9WxBVFU0.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-2188102178828634523</id><published>2010-02-18T17:00:00.000+02:00</published><updated>2010-02-18T17:00:01.335+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-18T17:00:01.335+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="quality" /><category scheme="http://www.blogger.com/atom/ns#" term="craftmanship" /><title>Software Craftsmanship</title><content type="html">&lt;p&gt;
Uncle Bob talks about Software Craftsmanship and agile: &lt;a href="http://java.dzone.com/videos/object-mentors-bob-martin"&gt;http://java.dzone.com/videos/object-mentors-bob-martin&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
I couldn't agree more. 
&lt;/p&gt;
&lt;p&gt;
In my opinion software development should become a closed guild. Anyone can write code, but we should strive to make quality code, and we should make our occupation  a respectable one!
&lt;p&gt;
Ignore the rules, and you’re out ;)
&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;
We will not ship shit. Well put Bob.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-2188102178828634523?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/89CwCLcmR2w" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/2188102178828634523/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2010/02/software-craftsmanship.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/2188102178828634523?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/2188102178828634523?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/89CwCLcmR2w/software-craftsmanship.html" title="Software Craftsmanship" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2010/02/software-craftsmanship.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUIGQn8ycCp7ImA9WxBWGUQ.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-6250598388870204389</id><published>2010-02-12T17:05:00.003+02:00</published><updated>2010-02-12T18:52:03.198+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-12T18:52:03.198+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CI" /><category scheme="http://www.blogger.com/atom/ns#" term="agile" /><category scheme="http://www.blogger.com/atom/ns#" term="complexity" /><category scheme="http://www.blogger.com/atom/ns#" term="ant" /><category scheme="http://www.blogger.com/atom/ns#" term="architecture" /><category scheme="http://www.blogger.com/atom/ns#" term="structure 101" /><title>Event Horizon</title><content type="html">When working in an agile environment, being able to control parts of the architecture like layers, conventions, etc, is usually desired, while letting the teams make most of the design decisions. Easier said than done. In this model the architect envisions the initial architecture and, communicates the architecture to the team. The architecture evolves over time according to the needs, while the architect shapes it, to keep things simple and “right”.
&lt;br /&gt;&lt;br /&gt;

Keeping track of what’s going on in a large code base is virtually impossible. Even the most skilled developers might miss architecture violations, while performing code reviews for a big chunk of code.
&lt;br /&gt;&lt;br /&gt;

In order to solve this I use a powerful tool called &lt;a href="http://www.headwaysoftware.com/products/structure101/index.php"&gt;Structure-101&lt;/a&gt; (I call it s101). The s101 client on its own is brilliant for analyzing the code base and defines / communicate the desired architecture. However having to manually check out the latest code, check for new violations, see what has changed and notify the developer, who created new violations, is tedious and time consuming. Luckily s101 comes with a command line utility called S101Headless, which can be integrated into your nightly build. The S101Headless utility allows you to test for new / existing structural violations / increased code complexity, and publish a new snapshot.
&lt;br /&gt;&lt;br /&gt;

The strategy that I use with s101 is as follows. First I analyze the code base, define the architecture diagrams, and extract recommendations for refactoring. Later by integrating s101 into the nightly build, I control the evolution of the architecture. Legacy code bases usually contain many design tangles which are hard to get rid of, and, usually you won’t get the resources for refactoring… Still it is easy to seal the complexity and isolate it from the “happy code”. S101 allows you to break the build only on when new architectural violations show up.
&lt;br /&gt;&lt;br /&gt;

The current version of S101Headless is somewhat awkward to use with Ant, even though  it is much better than the previous version. The utility consumes an XML file containing the operations you want executed. But hey, the arguments are usually dynamic, especially in a build environment. The documentation suggests utilizing the echo task in order to write the XML file to disk. While this approach allows you to use the Ant variables, it is makes your XML file less readable. Here’s how I do it:
&lt;br /&gt;&lt;br /&gt;

First you need a template file (s101headless-template.xml):
&lt;br /&gt;
&lt;pre class="codesnip"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;headless version="1.0"&amp;gt;

    &amp;lt;operations&amp;gt;
        &amp;lt;operation type="check-architecture"&amp;gt;
            &amp;lt;argument name="output-file" value="@REPORTS_DIR@/arch-violations.csv"/&amp;gt;
            &amp;lt;argument name="onlyNew" value="true"/&amp;gt;
        &amp;lt;/operation&amp;gt;

        &amp;lt;operation type="publish"&amp;gt;
            &amp;lt;argument name="rpkey" value="c0d3s1ut"/&amp;gt;
        &amp;lt;/operation&amp;gt;
    &amp;lt;/operations&amp;gt;


    &amp;lt;arguments&amp;gt;
        &amp;lt;argument name="local-project" value="@S101_LOCAL_PROJECT@"&amp;gt;
            &amp;lt;override attribute="classpath" value="@CLASSPATH_OVERRIDE@"/&amp;gt;
        &amp;lt;/argument&amp;gt;
        &amp;lt;argument name="repository" value="@S101_REPOSITORY@"/&amp;gt;
        &amp;lt;argument name="project" value="@S101_PROJECT@-snapshots"/&amp;gt;
    &amp;lt;/arguments&amp;gt;

&amp;lt;/headless&amp;gt;
&lt;/pre&gt;
In the Ant build file I use copy with a filter chain to render the XML for the S101Headless utility:
&lt;br /&gt;
&lt;pre class="codesnip"&gt;&amp;lt;target name="prepareHeadlessFile" depends="setup_classpath"&amp;gt;
                &amp;lt;copy file="s101headless-template.xml" tofile="s101headless.xml" overwrite="true"&amp;gt;
                        &amp;lt;filterchain&amp;gt;
                                &amp;lt;replacestring from="@CLASSPATH_OVERRIDE@" to="${my-jars-path}"/&amp;gt;
                                &amp;lt;replacestring from="@REPORTS_DIR@" to="${s101.reports.dir}"/&amp;gt;
                                &amp;lt;replacestring from="@S101_PROJECT@" to="${s101.project.name}"/&amp;gt;
                                &amp;lt;replacestring from="@S101_REPOSITORY@" to="${s101.repository}"/&amp;gt;
                                &amp;lt;replacestring from="@S101_LOCAL_PROJECT@" to="${s101.local.project}"/&amp;gt;
                        &amp;lt;/filterchain&amp;gt;
                &amp;lt;/copy&amp;gt;
        &amp;lt;/target&amp;gt;

        &amp;lt;target name="s101" depends="prepareHeadlessFile"&amp;gt;
                &amp;lt;mkdir dir="${s101.reports.dir}"/&amp;gt;
                &amp;lt;java classname="com.headway.assemblies.seaview.headless.S101Headless" fork="true" errorproperty="s101.failure" resultproperty="s101.result.code" maxmemory="512m" dir="${s101.java.home}"&amp;gt;
                        &amp;lt;classpath&amp;gt;
                                &amp;lt;pathelement path="${s101.java.home}/structure101-java-b586.jar"/&amp;gt;
                                &amp;lt;pathelement path="${basedir}"/&amp;gt;
                        &amp;lt;/classpath&amp;gt;
                        &amp;lt;arg value="${basedir}/s101headless.xml"/&amp;gt;
                &amp;lt;/java&amp;gt;
                &amp;lt;!-- fail if there were errors in the S101Headless execution --&amp;gt;
                &amp;lt;condition property="s101.violations.found"&amp;gt;
                        &amp;lt;not&amp;gt;
                                &amp;lt;equals arg1="${s101.result.code}" arg2="0" /&amp;gt;
                        &amp;lt;/not&amp;gt;
                &amp;lt;/condition&amp;gt;
                &amp;lt;fail if="s101.violations.found" message="${s101.failure}"/&amp;gt;
        &amp;lt;/target&amp;gt;
&lt;/pre&gt;
I leave the Ant classpath and properties setup as an exercise to the reader ;)
&lt;br /&gt;&lt;br /&gt;

As a complementary to the nightly build analysis, s101 also offers an IDE plugin, which allows connects to the structure 101 repository, and can display real time errors / warnings when new violations are detected in the code. IMHO the Eclipse plugin is still in its infancy, and requires some improvements. In the near future (I hope), it will provide a real time, 24/7, protection against evil code :D
&lt;br /&gt;&lt;br /&gt;
BTW, can anyone guess why I called this post “Event Horizon”?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-6250598388870204389?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/w3g0rrj8NRY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/6250598388870204389/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2010/02/event-horizon.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/6250598388870204389?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/6250598388870204389?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/w3g0rrj8NRY/event-horizon.html" title="Event Horizon" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2010/02/event-horizon.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQDQnwyeCp7ImA9WxBSGUU.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-1366684893861874739</id><published>2009-12-24T10:40:00.005+02:00</published><updated>2009-12-28T08:06:13.290+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-28T08:06:13.290+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="props" /><category scheme="http://www.blogger.com/atom/ns#" term="spring" /><category scheme="http://www.blogger.com/atom/ns#" term="properties" /><title>Referencing Spring beans in Properties collections</title><content type="html">It is some times desired to reference Spring beans when injecting properties into some bean. The problem is that the &amp;lt;props&amp;gt; tag doesn’t support references – it supports plain string values only.
&lt;br /&gt;
While trying to resolve such issue I stumbled onto a discussion in the Spring-Source community forums: &lt;a href="http://forum.springsource.org/showthread.php?t=63939"&gt;http://forum.springsource.org/showthread.php?t=63939&lt;/a&gt;.
&lt;br /&gt;
While adding non-string values into Properties is problematic, adding string values that originate from a bean-value, or a bean’s-property-value, or something similar is a valid usecase. I posted my solution in the thread, but just for your convinience, here goes:
&lt;br /&gt;
In general, as long as you are using string values, you can can safely replace the &amp;lt;props&amp;gt; tag with &amp;lt;map&amp;gt;:

&lt;br /&gt;
&lt;pre class="codesnip"&gt;  &amp;lt;props&amp;gt;
    &amp;lt;prop key="foo"&amp;gt;blahhhh&amp;lt;/prop&amp;gt;
    &amp;lt;prop key="bar"&amp;gt;arrrrgh&amp;lt;/prop&amp;gt;
  &amp;lt;/props&amp;gt;
&lt;/pre&gt;
Is the same as

&lt;br /&gt;
&lt;pre class="codesnip"&gt;  &amp;lt;map&amp;gt;
    &amp;lt;entry key="foo" value="blahhhh"/&amp;gt;
    &amp;lt;entry key="bar"&amp;gt;
      &amp;lt;bean class="java.lang.String"&amp;gt;
       &amp;lt;constructor-arg value="arrrrgh"/&amp;gt;
      &amp;lt;/bean&amp;gt;
    &amp;lt;/entry&amp;gt;

    &amp;lt;!-- and you can even do --&amp;gt;
    &amp;lt;entry key="baz" value-ref="someBean"/&amp;gt;
  &amp;lt;/map&amp;gt;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-1366684893861874739?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/iYMrZccB-2c" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/1366684893861874739/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2009/12/referencing-spring-beans-in-properties.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/1366684893861874739?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/1366684893861874739?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/iYMrZccB-2c/referencing-spring-beans-in-properties.html" title="Referencing Spring beans in Properties collections" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2009/12/referencing-spring-beans-in-properties.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUcGQHc4eyp7ImA9WxBRF0U.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-7603073349345404855</id><published>2009-12-12T12:48:00.015+02:00</published><updated>2010-01-06T15:03:41.933+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-01-06T15:03:41.933+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="SI" /><category scheme="http://www.blogger.com/atom/ns#" term="JMS" /><category scheme="http://www.blogger.com/atom/ns#" term="spring" /><category scheme="http://www.blogger.com/atom/ns#" term="spring-integration" /><title>JMS Request-Response with Spring-Integration</title><content type="html">&lt;div dir="ltr" style="text-align: left;"&gt;
&lt;div style="text-align: left;"&gt;
A colleague of mine asked me “why would Spring need to write a framework to send / receive JMS messages”. &lt;br /&gt;
&lt;br /&gt;
So here is why:&lt;br /&gt;
&lt;br /&gt;
In short, avoiding frameworks usually leads to breaking the &lt;a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;DRY&lt;/a&gt; principle.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
In order to send messages you need to&lt;br /&gt;
&lt;/div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Fetch the connection factory from JNDI.&lt;/li&gt;
&lt;li&gt;Create a JMS connection.&lt;/li&gt;
&lt;li&gt;Create a JMS session.&lt;/li&gt;
&lt;li&gt;Fetch / create the JMS destination.&lt;/li&gt;
&lt;li&gt;Create and configure a Producer.&lt;/li&gt;
&lt;li&gt;Create your JMS message.&lt;/li&gt;
&lt;li&gt;Remember to start() the connection.&lt;/li&gt;
&lt;li&gt;Remember to close / cache the JMS resources, and handle errors properly.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;
In order to receive messages you need to&lt;br /&gt;
&lt;/div&gt;
&lt;ol dir="ltr" style="text-align: left;"&gt;
&lt;li&gt;Fetch the connection factory from JNDI.&lt;/li&gt;
&lt;li&gt;Create a JMS connection.&lt;/li&gt;
&lt;li&gt;Create a JMS session.&lt;/li&gt;
&lt;li&gt;Fetch / create the JMS destination.&lt;/li&gt;
&lt;li&gt;Create and configure a consumer - either add a MessageListener, or receive() messages in a loop.&lt;/li&gt;
&lt;li&gt;Remember to close / cache the JMS resources, and handle errors properly.&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;
If you’d also like to get a response you need to&lt;br /&gt;
&lt;/div&gt;
&lt;ol dir="ltr" style="text-align: left;"&gt;
&lt;li&gt;Set the message reply
to channel. This can be a temp destination, or an existing one. If this
is an existing destination, you will also need a message selector and
use the JMSCorrelationID.&lt;/li&gt;
&lt;li&gt;Create a consumer for the reply message and receive() the message.&lt;/li&gt;
&lt;li&gt;On the receiver side you need to create a response message, set the
JMSCorrelationID, and send back to the JMSReplyTo destination that was
specified on the incoming message.&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;
Writing all this code is
naturally prone to errors. Now throw in handling of connections errors
(reconnects) as well, and what you get is a whole lot of messy code :P&lt;br /&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;
When using Spring JMS
integration framework you can avoid most of the boiler plate code.&amp;nbsp;
When using spring-integration on top of that you can actually decouple
your (Java) code from JMS and Spring all together. That is you can
avoid having any compile time dependency on JMS or Spring!&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;div dir="ltr" style="text-align: left;"&gt;
The client code:&lt;br /&gt;
&lt;/div&gt;
&lt;pre class="codesnip" name="code"&gt;
  &amp;lt;integration:channel id="publishChannel"/&amp;gt;
  &amp;lt;integration:channel id="jmsReplyToChannel"/&amp;gt;
 
  &amp;lt;integration:gateway service-interface="MyPublisher" id="publisher" default-request-channel="publishChannel" default-reply-channel="jmsReplyToChannel"/&amp;gt;

  &amp;lt;jms:outbound-gateway id="jmsOutGateway"
                       request-destination="topic"
                       request-channel="publishChannel"
                       reply-channel="jmsReplyToChannel"
                       connection-factory="jmsConnectionFactory"/&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
The publisher is merely an interface :D&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="codesnip" name="code"&gt;
public interface MyPublisher {
    public long publish(String msg);
}
&lt;/pre&gt;
&lt;br /&gt;
The server side code:&lt;br /&gt;
 &lt;br /&gt;
&lt;pre class="codesnip" name="code"&gt;
&amp;lt;jms:inbound-gateway id=&amp;quot;jmsInGateway&amp;quot;
                      request-destination=&amp;quot;topic&amp;quot;
                      request-channel=&amp;quot;serviceInboundChannel&amp;quot;
                      connection-factory=&amp;quot;jmsConnectionFactory&amp;quot;/&amp;gt;
 
  &amp;lt;integration:service-activator input-channel=&amp;quot;serviceInboundChannel&amp;quot; ref=&amp;quot;myService&amp;quot; method=&amp;quot;print&amp;quot;/&amp;gt;

  &amp;lt;integration:channel id=&amp;quot;serviceInboundChannel&amp;quot;/&amp;gt;

  &amp;lt;bean id=&amp;quot;myService&amp;quot; class=&amp;quot;MyServiceImpl&amp;quot;/&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
The called service implementation can be anything you like. I only print the message to stdout and return the reception time – this can be later used to test that we are actually performing a synchronised request response style messaging, which is not the case when no JMS response is involved.
&lt;br /&gt;
&lt;pre class="codesnip" name="c0de"&gt;
public class MyServiceImpl implements MyService {
    public long print(String msg) {
        long now = System.currentTimeMillis();
        System.out.format("%d print: %s\n", now, msg);
        return now;
    }
}
&lt;/pre&gt;
&lt;br/&gt;
We also need some boring JMS Spring beans definitions to get this working.  The code below assumes you have a running ActiveMQ server, but you may replace the provider url to use an embeded ActiveMQ broker (see the commented-out line).
&lt;br/&gt;
&lt;pre class="codesnip" name="code"&gt;
&amp;lt;bean id=&amp;quot;jmsJndiTemplate&amp;quot; class=&amp;quot;org.springframework.jndi.JndiTemplate&amp;quot;&amp;gt;
    &amp;lt;constructor-arg&amp;gt;
      &amp;lt;props&amp;gt;
        &amp;lt;prop key=&amp;quot;java.naming.factory.initial&amp;quot;&amp;gt;org.apache.activemq.jndi.ActiveMQInitialContextFactory&amp;lt;/prop&amp;gt;
        &amp;lt;prop key=&amp;quot;java.naming.provider.url&amp;quot;&amp;gt;tcp://localhost:61616&amp;lt;/prop&amp;gt;
        &amp;lt;!-- prop key=&amp;quot;java.naming.provider.url&amp;quot;&amp;gt;vm://localhost?broker.persistent=false&amp;lt;/prop --&amp;gt;
      &amp;lt;/props&amp;gt;
    &amp;lt;/constructor-arg&amp;gt;
  &amp;lt;/bean&amp;gt;

  &amp;lt;bean id=&amp;quot;jmsConnectionFactory&amp;quot; class=&amp;quot;org.springframework.jms.connection.CachingConnectionFactory&amp;quot;&amp;gt;
    &amp;lt;constructor-arg&amp;gt;
      &amp;lt;bean class=&amp;quot;org.springframework.jndi.JndiObjectFactoryBean&amp;quot;&amp;gt;
        &amp;lt;property name=&amp;quot;jndiName&amp;quot; value=&amp;quot;ConnectionFactory&amp;quot;/&amp;gt;
            &amp;lt;property name=&amp;quot;jndiTemplate&amp;quot; ref=&amp;quot;jmsJndiTemplate&amp;quot;/&amp;gt;
          &amp;lt;/bean&amp;gt;
    &amp;lt;/constructor-arg&amp;gt;
    &amp;lt;property name=&amp;quot;reconnectOnException&amp;quot; value=&amp;quot;true&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;cacheProducers&amp;quot; value=&amp;quot;true&amp;quot;/&amp;gt;
  &amp;lt;/bean&amp;gt;

  &amp;lt;bean id=&amp;quot;topic&amp;quot; class=&amp;quot;org.springframework.jndi.JndiObjectFactoryBean&amp;quot;&amp;gt;
        &amp;lt;property name=&amp;quot;jndiName&amp;quot; value=&amp;quot;dynamicTopics/eran-test&amp;quot;/&amp;gt;
        &amp;lt;property name=&amp;quot;jndiTemplate&amp;quot; ref=&amp;quot;jmsJndiTemplate&amp;quot;/&amp;gt; 
  &amp;lt;/bean&amp;gt;
&lt;/pre&gt;
&lt;br/&gt;
The diagram below depicts our creation:
&lt;a href="http://4.bp.blogspot.com/_soQjtBWQkLw/SyNwRyrX1nI/AAAAAAAABH0/qqTv8vWmHX4/s1600-h/spring-integration.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img src="http://4.bp.blogspot.com/_soQjtBWQkLw/SyNwRyrX1nI/AAAAAAAABH0/qqTv8vWmHX4/s640/spring-integration.png" border="0"&gt;&lt;/a&gt;
&lt;br/&gt;

&lt;p&gt;Notes:&lt;/p&gt;
&lt;ul style="text-align: left;"&gt;&lt;li&gt;I used a topic here, but it would have been more appropriate to use a queue in most cases…&lt;/li&gt;
&lt;li&gt;Spring-Integration has it’s own cost. Although the code could be slightly improved performance wise, the internal message format, and the temp queue / channels creation impact performance. So don’t say I didn’t warn you… a plain async message passing style application should probably be written with plain Spring JMSteamplate, and message containers.&lt;/li&gt;
&lt;li&gt;The code above as is causes Spring to create a lot of temporary resources like queues, consumers, producers, channels. I left it this way for brevity, but most of the temp resources creation can be avoided, thus reducing the load on the message broker.&lt;/li&gt;
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-7603073349345404855?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/70SNchpN9-Q" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/7603073349345404855/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2009/12/jms-request-response-with-spring.html#comment-form" title="9 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/7603073349345404855?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/7603073349345404855?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/70SNchpN9-Q/jms-request-response-with-spring.html" title="JMS Request-Response with Spring-Integration" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_soQjtBWQkLw/SyNwRyrX1nI/AAAAAAAABH0/qqTv8vWmHX4/s72-c/spring-integration.png" height="72" width="72" /><thr:total>9</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2009/12/jms-request-response-with-spring.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkIFSX06eip7ImA9WxBSGUU.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-2256501190339428469</id><published>2009-12-04T18:17:00.002+02:00</published><updated>2009-12-28T08:08:38.312+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-28T08:08:38.312+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="OXM" /><category scheme="http://www.blogger.com/atom/ns#" term="mapping" /><category scheme="http://www.blogger.com/atom/ns#" term="javabean" /><category scheme="http://www.blogger.com/atom/ns#" term="value-object" /><category scheme="http://www.blogger.com/atom/ns#" term="dozer" /><title>Value Objects Mapping</title><content type="html">&lt;div dir="rtl" style="text-align: right;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;"&gt;My company decided to break its monolithic build and make our life easier.&amp;nbsp; The main idea was to move from &lt;a href="http://www.jini.org/wiki/Main_Page"&gt;Jini &lt;/a&gt;services to &lt;a href="http://code.google.com/p/protobuf/"&gt;Protocol-Buffers&lt;/a&gt; over &lt;a href="http://en.wikipedia.org/wiki/Restful"&gt;RESTful &lt;/a&gt;API.&lt;br /&gt;
&lt;br /&gt;
The main reasons were to introduce loose coupling between our services, adopt open standards, and become cool ;-). &lt;br /&gt;
&lt;br /&gt;
I will probably discuss all this in a later post. The reason I am mentioning all this is that it raised the issue of transforming value objects back and forth.&amp;nbsp; We are all faced with this issue when developing enterprise software. For example, you create objects in your DAO layer. These are passed to domain objects that massage them a little. Later these objects are passed to your service layer, controller, and presentation layer. &lt;br /&gt;
&lt;br /&gt;
Some of you are probably saying right now, “Are you really coupling all these layers”?&amp;nbsp; Well, no… Most of us will go and transform the Value Objects (VOs, or DTOs) to another form when passed between the layers. All this copying and transforming is extremely repetitive and time consuming.&lt;br /&gt;
&lt;br /&gt;
When we started working with protobuf, I quickly noticed that transforming protobuf messages into our domain model is going to be an issue. To avoid decoupling between services, each service has to write its own transformation code. This requirement poses a high risk of code duplication (I know some will argue you can’t really call it duplication). My idea was to introduce XML object to object mapping rules.&lt;br /&gt;
&lt;br /&gt;
In comes &lt;a href="http://dozer.sourceforge.net/"&gt;Dozer&lt;/a&gt;. Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. It has many cool features, but lacks the ability to transform your objects into immutable objects – Dozer has no support for constructor arguments. Making your VO classes immutable is desired when you are not administrating them, and when these objects are going to be placed in caches.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, I don’t have a solution to the immutability issue (but more will come - I promise). I have started a private project that will either extend Dozer to support constructor args (hard…), or utilize for its features while extending its abilities to do so (somewhat easier).&lt;br /&gt;
&lt;br /&gt;
I just wonder how many of you stumbled into this…&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-2256501190339428469?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/lEwPDhnUWBQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/2256501190339428469/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2009/12/value-objects-mapping.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/2256501190339428469?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/2256501190339428469?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/lEwPDhnUWBQ/value-objects-mapping.html" title="Value Objects Mapping" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2009/12/value-objects-mapping.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUEMRng7fip7ImA9WxBTEE4.&quot;"><id>tag:blogger.com,1999:blog-8554477485013857539.post-1130993027549563796</id><published>2009-12-04T16:19:00.003+02:00</published><updated>2009-12-05T20:28:07.606+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-05T20:28:07.606+02:00</app:edited><title>My Blog</title><content type="html">&lt;div dir="rtl" style="text-align: right;" trbidi="on"&gt;&lt;div dir="ltr" style="text-align: left;"&gt;For a long time I've been reading blog post, using tech forums, and open-source framework.&lt;br /&gt;
&lt;/div&gt;&lt;div dir="ltr" style="text-align: left;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div dir="ltr" style="text-align: left;"&gt;I decided it is time to return the investment.&lt;br /&gt;
&lt;/div&gt;&lt;div dir="ltr" style="text-align: left;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div dir="ltr" style="text-align: left;"&gt;I intend to post my thoughts, knowledge, and ideas here in this blog.&lt;br /&gt;
&lt;br /&gt;
I'm hoping someone, or the lots of you would find this blog useful.&lt;br /&gt;
&lt;br /&gt;
Enjoy.&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8554477485013857539-1130993027549563796?l=codeslut.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codeslut/~4/wEPqvwiTZug" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codeslut.blogspot.com/feeds/1130993027549563796/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codeslut.blogspot.com/2009/12/for-long-time-ive-been-reading-blog.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/1130993027549563796?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8554477485013857539/posts/default/1130993027549563796?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/codeslut/~3/wEPqvwiTZug/for-long-time-ive-been-reading-blog.html" title="My Blog" /><author><name>Eran</name><uri>http://www.blogger.com/profile/09711979155619868885</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="21" src="http://1.bp.blogspot.com/_soQjtBWQkLw/SxtRkm8Z68I/AAAAAAAABG4/PrlBsA-SBcU/S220/eye-of-horus-small.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://codeslut.blogspot.com/2009/12/for-long-time-ive-been-reading-blog.html</feedburner:origLink></entry></feed>

