Welcome to JoshLong.com, home of my personal blog
<< Last | Next >>
September 23, 2011

Libraries and Component Models and APIs (in Spring), Oh My!

One of the most importants about a framework is that it is pluggable, that it is extensible. This is very much a core promise of a good framework, and separates it from mere libraries. Most of the Spring frameworks are exposed as libraries and component models. These are the two levels most people interact with Spring: they reuse the numerous and powerful libraries as-is, or - when the libraries aren't able to read your mind, Spring provides some surface-level API against which to write pluggable code for certain responsibilities. For example, Spring Integration comes with batteries included, many, many batteries, actually. In this case, the batteries I refer to are the integration technologies supported in terms of the various modules that are provided: web services, databases, message brokers (JMS, AMQP), file systems (FTP, FTPS, SFTP, and regular filesystem mounts), etc., etc. The same is true of Spring Batch and indeed of all the other Spring frameworks. If the batteries included aren't quite what you want, and you'd like to plug in your own logic, the 80% cases are accounted for in terms of the component models that the projects expose.

In Spring Integration, the 80% case is to reuse the libraries. It is possible to not write a single line of Java code and solve complex problems using Spring's declarative XML namespaces. When this isn't enough, the next tier of support is the Spring Integration component model that works with annotations. The only time you need to use these component models is to help the framework fill in the gaps, to understand the parts of your application that it could'nt possibly already know or infer. For example, if you want to provide conditional routing based on your business logic - the integration equivalent of an if/else statement - then you need to work in terms of a router. Here's how you write the rough skeleton of a custom router, one that's specific to your application, in Spring Integration:

@Router ( ... )
public class MyBlogRouter  { 
 public String chooseWhichChannelBasedOnTheInputMessage ( Message<String> msg) { 
   if (msg.getPayload ().equals ( "a") { 
     return "channelA"; 
   }
   ...
 }
}

In Spring MVC, the 80% case is creating a controller that can handle requests as they come in from the web, and naturally, there's an easy, declarative way of telling Spring MVC about controllers, like this:

@Controller 
public class MyBlogController { 
  @RequestMapping ( ...)
  public String handleRequest ( @PathVariable ("id") long id) { 
   ... 
  }
} 

In both of these cases, the component model lives side by side with the framework itself. There's an interplay, an unspoken contract: the framework provides all the machinery to automate as much as possible. Ideally, when you do come to the point of needing to plug in code, it's because you're trying to express something quintessentially related to your business problem, and not focusing on the machinery of writing an integration flow, or handling HTTP requests. Small focused hooks provide a lot of power, and these two layers go a long way for most users.

What a lot of people don't know is that all of the Spring frameworks (notably, Spring core itself) provide a third echelon of support, of functionality. To me, this third echelon is the APIs that it provides against which you can code. I like to think that, when the 80% cases aren't enough, you should look for the 20% answer in the APIs.

You deal with these APIs a lot, and sometimes they overlap with the component models, so the line can be a bit blurry. One blurry example is if you register a bean that implements InitializingBean, then Spring will automatically invoke the InitializingBean#afterPropertiesSet () method when the bean has had its dependencies satisfied and is about to be made available in the context. This, in effect, gives you a construction hook, like a second constructor that gets called after the properties have been satisfied. This is an API. You get power by implementing an interface. However, from the component model perspective, there's an alternative: you can use the JSR 250 annotations, e.g., @javax.annotation.PostConstruct to trigger that any arbitrary method be invoked. Here's an example:

@Component
public class MyClass { 
  @javax.inject.Inject DataSource dataSource ;
  @PostConstruct 
  public void setup () { ... }
}

Either one's fine, and at this level it's largely a matter of personal style.

Taking the examples further, and moving from the ambiguous to the clearly API-centric, Spring provides convenient interfaces that give you access to the lower level workings of the Spring framework. Examples of these include the BeanPostProcessor and the BeanFactoryPostProcessor. At this level, you start tapping a lot of power. Admittedly, a lot of people aren't going to need or consume the framework in this way, but it's nice to know it's there. These APIs are the force multipliers that the other Spring frameworks leverage to provide the powerful abstractions for you. Ever wondered how Spring does its magic when you use declarative transaction annotation, @Transactional? You might be surprised to learn there's a BeanPostProcessor involved. Nothing in the Spring framework is magic -- you can build infrastructure or framework code just like the other Spring projects do for your users by tapping into these APIs.

Exposing your frameworks and common abstractions in terms of these well known, idiomatic Spring APIs provides a powerful, familiar surface to your API that consumers of your abstraction can leverage. There are extension points all over the place in Spring - places where you can provide your own implementation that fits into the machinery - an example of the strategy pattern. In Spring MVC, ViewResolvers help map abstract view names (Strings) into concrete view resources (that might mean a .jsp, a Velocity or ThymeLeaf template, or any of a zillion other implementations. In core Spring's REST support there exists an interface, HttpMessageConverter<T>, that Spring uses to figure out how to convert HTTP request and response payloads into domain-centric objects: a POST request with bytes might becomes a java.io.InputStream, a response with a JAXB object might need to be marshalled into XML and then sent as the response, etc. Those strategies are all codified as implementations of this interface. Adding new ones is quite easy. In Spring Integration, outbound adapters implement the MessageHandler interface, and provide the interface between the messaging code and an external system. You can add new ones and teach Spring Integration new tricks by implementing this interface.

Anyway, I could go on, but - for my own good - I won't, since I wouldn't have anything left for my talk at SpringOne!

What talk at SpringOne, you say? Oh Yeah! About that: if you want to hear more about these extensibility hooks and the powerful, intricate layering that's possible using the Spring frameworks, come check out my talk at SpringOne on "Tailoring Spring for Custom Usage" in Chicago, in October 2011!

September 17, 2011

Trip Roundup and The Dallas Spring User Group

Hey sports fans! What a crazy few weeks! September started off with a bang as I found myself in sunny Las Vegas for VMWorld 2011. I really enjoyed the time there to connect with my ops- and system-centric brethren. What a great crowd, and the excitement surrounding the exodus to the cloud was palatable. People are excited about vFabric, and about connecting their applications with a truly integrated stack, RabbitMQ, GemFire, tcServer, etc., and they're doubly excited that they have in the Spring portfolion a unified programming model for all of these technologies. So, exciting stuff, definitely. If you missed all the excitement, don't fret: lots of was recorded and made available here!

The week after that, I found myself en-route for Oslo, Norway, for the 10th annual JavaZone conference, which rocked. This was my second time speaking there and I can tell you, in no uncertain terms, that vikings rock! What a great community. This conference seems like the conference for the thinking developer, and featured plenty of content on Spring and Scala this year.

I also had the pleasure of taking a tour of the Norwegian fjords with colleague and great guy Chris Richardson. If I get around to it I'll try to post some incriminating photos later :-)

The best part? Even if you weren't lucky enough to be there, you can still partake in the fun by watching the recorded videos which JavaZone's generously put online on Vimeo for free. There's of course my talk on Spring configuration, Costin Leau's the Spring 3.1 caching abstraction, Mark Fisher's talk on Spring Integration with CloudFoundry (bonus: the talk, "Event Driven Architecture with the Spring framework" from last year is also available!), Costin's other talk on Spring Data, and Chris Richardson's talk on Polyglot Persistence, all available in English. If you speak Norwegian, then there's ample content for you, as well, including a talk on Spring Java configuration in Scala, and on performance gains with Spring Batch. I think I saw some others, too, but as I don't speak Norwegian, I can't be too sure! Enjoy!

As if that weren't enough excitement, next week I have the honor of speaking at the Dallas Spring User Group. I'll be giving two related talks, one on the SPIs exposed to the interested Java developer in core Spring and, one on the SPIs exposed in Spring Integration. Both talks are done with an eye towards extending the framework to accomodate advanced use cases.

Of course, some of it may be old news for the veteran and whip-smart Spring users at that particular group, but we'll see if I can't surprise a few of 'em! ;-) If you're in the Dallas, Texas area and want to talk Spring, come on down or ping me on Twitter at @starbuxman.

August 31, 2011

Happiness is...

Happiness is having RabbitMQ and PostgreSQL in the output of my vmc services invocation ;-)

 vmc target api.cloudfoundry.com 
 vmc login 
 ...
 vmc services 

What a wild month for CloudFoundry! RabbitMQ! MicroCloud! and now, PostgreSQL (this was one of the biggest pieces of news to come out of VMWorld 2011 in Las Vegas, where I am right now)! And, for the Python and PHP inclined among us, there's now the CloudFoundry partner providers that are now supporting these languages on their CloudFoundry clouds. It's all coming together, quicker and better than ever. Stay tuned for more!

August 27, 2011

WTF! Linked Lists patented

Tim Fox retweeted this tweet from Hacker News's Hacker News Bot that linked lists had apparently been patented and responded as aptly as I could've: WTF.

August 12, 2011

RabbitMQ is now officially available on CloudFoundry!

For you code lurkers out there, the news that RabbitMQ is now available in CloudFoundry should come as no surprise. But, it's finally here! This is very exciting. One thing I've always loved about RabbitMQ is its ubiquity. It is more widely used than any other message broker on the cloud, and on Amazon Web Services in particular. The AWS tidbit's a bit more awesome when you realize that AWS already has a message broker service called SQS! ;-) Anyway, it's now available on CloudFoundry. You should definitely check it out. The news overall seems good, too! Messaging middleware is the critical component for scale. Cloud-scale is a way of life, not just a switch to be turned on and off. Specifically, an architecure that scales is going to enjoy temporally decoupling, or asynchronism, and RabbitMQ is the perfect vehicle to enable that asynchronous coordination.

If you're a Spring developer, and haven't had a chance to look at CloudFoundry, then I strongly recommend you see Mark Fisher's entry on the SpringSource blog introducing the wide world of the cloud for Spring developers from a few months ago. There's also great content on the SpringSource YouTube channel.

Getting Started with Spring Roo

Steve Mayzak and I have just finished a book for O'REILLY, Getting Started with Spring Roo. The book's a very quick introduction to Spring Roo, and includes information about the many useful addons (including support for Vaadin - a powerful, alternative web framework, and Neo4j - an alternative, powerful NOSQL data-store), as well as the in-built Spring Roo addons. If you're interested in Spring Roo, I hope you'll check this out.

August 7, 2011

Speaking at JavaZone 2011

I'm excited to announce that I'll be speaking at JavaZone again this year. I must confess, I was a little worried that I would not be accepted this year! My talk last year - on business process management and Spring Integration - seemed to garner puzzled looks. Perhaps I just misread the amazing Norwegian audience? The conference (and the technical presentations, to boot) were both wonderful.

I had the wonderful experience of stopping at a booth and having somebody try to sell me training in Spring (which, I mean, sure... more training's always good!), but the kicker came when he showed me the training manual! It was the book that Gary Mak and Daniel Rubio and I co-authored, Spring Recipes!

I submitted on other topics, as well, last year, and those were not accepted. No matter, I gave my talk, and indeed the people that did get it, really got it! So, while I'm not complaining, it still wasn't exactly my best day. I'm thus thrilled to be invited back and - as if fate wasn't already shining on me - my 3-hour university talk's been accepted. My talk's called, "Spring, a Walking Tour," which is a bit of misnomer, as I'm usually the only one walking at all! ;-) I'll be talking about the Spring landscape - everything, from the cloud, mobile, integration, batch processing, big-data, and more! Check it out. This talk was well received when I gave it at Geecon earlier this year, and I've already updated it to reflect a lot of the new stuff in the Spring 3.1 milestones.

The city of Oslo, as you no doubt have heard, recently experienced a terrorist attack resulting in the massacre of innocents, both adult and children. It broke my heart to see this. What a disgusting, horrible incident, obviously. It's beyond comprehension, no matter where it happens, of course. It was particularly surprising to me that it happened in Oslo, however, as it seemed like such a beautiful, clean, ideal city when I was there. I will take more time, this year, to explore Oslo, and to take in more of Norway's beauty, and to walk among these strong people. I really have a lot of respect for the way the people of the city responded to the incident. They are inspirational.

I hope you'll join me at JavaZone, and in Oslo, in general, to be inspired.

July 23, 2011

Spring Corner: Beyond the FactoryBean

I've decided I'm going to try to write up a series of small posts on things about Spring that are perhaps common knowledge, but helpful to know, anyway. These posts are geared decidedly towards newcomers, though old hats might learn a thing or two.

I looked at what a basic FactoryBean is in my previous post. While FactoryBeans are important - and knowing what they do can help you navigate the framework - they're by and large no longer the recommended approach to the task as of Spring 3.0 and the imminent Spring 3.1.

The whole point of a FactoryBean is to hide the construction of an object - either because it's very complex or because it can't simply be instantiated using that typical constructor-centric approach used by the Spring container (Maybe it needs to be looked up? Maybe it needs a static registry method?)

Spring 3.0 saw the introduction of Java configuration which lets you define beans using Java. For instance, to register a regular javax.sql.DataSource with Spring in XML, you will more than likely delegate to a properties file for the sensitive configuration information (like a database password) and use Spring to instantiate the javax.sql.DataSource, like this:

 
<beans ...>
	<context:property-placeholder location = "ds.properties" />
	
	<bean id = "ds" class = "a.b.c.MySqlDataSource">
	  <property name = "user" value = "${ds.user}"/>
	  <property name = "password" value = "${ds.password}"/>
	</bean>
</beans>

This is a simple bean, and translates naturally into Java configuration. It would look like this:

import a.b.c.* ;
	
@Configuration 
@PropertySource ("ds.properties") 
public class MyConfiguration { 
    @Inject private Environment env ; 
	
    @Bean public MySqlDataSource ds (){ 
        MySqlDataSource ds = new MySqlDataSource () ; 
        ds.setUser ( env.getProperty ("ds.user") );
        ds.setPassword ( env.getProperty ("ds.password"));
        return ds; 
    }
}

The beauty of this is that you're free to do anything you'd like inside the method. The return value is what's registered in the Spring container. Anything you do in service of properly constructing this object is up to you - you're no longer bound by the limits of what Spring can instantiate based on the XML specified. And, this is more natural - it's far easier to guarantee there's no typos using Java than XML. So, take away from this that Java configuration is - about equal in terms of lines of code, but a lot more powerful, and conceptually more natural.

With those limitations lifted, the value of the FactoryBean is starting to lessen. After all, if all a FactoryBean does is encode construction logic in a novel or unique way, then there's no reason that couldn't be done inside a Java configuration method, is there? Let's revisit the example from the last blog post, a custom FactoryBean designed to factory cars.

public class MyCarFactoryBean implements FactoryBean<Car> {
  private String mark; 
  private int year ;
  public void setMark (String m){ this.mark =m ; }
  public void setYear (int y){ this.year = y; }
  public Car getObject (){ 
    // wouldn't be a very useful FactoryBean 
    // if we could simply instantiate the object ;-)
    CarBuilder cb = CarBuilder.car ();
	
    if (year!=0) cb.setYear (this.year);
    if (StringUtils.hasText (this.mark)) cb.setMark ( this.mark); 
    return cb.factory (); 
  }
  public Class getObjectType () { return Car.class ; } 
  public boolean isSingleton () { return false; }
} 

In this example, we only conditionally set values if there are values to be set. So, we do some dancing around to ensure that we have values. This code is ugly, because it has a lot of different execution paths, but it's not particularly novel. We're adults, we can do this sort of thing ourselves. Let's dispose of the FactoryBean and simply use Java configuration to replace it for a definition of a Car. Again, we happen to know what configuratoin we need, so we don't have to duplicate the null checks in our code.

@Bean public Car honda (){ 
	return CarBuilder.car ()
	.setYear ( 1984 )
	.setMark ("Honda")
	.factory (); 
}

Not bad! We no longer need the complex FactoryBean, and we have a usable bean definition. If we wanted to make this reusable, we could, as well, by simply creating a factory method, like this:

// presumably exposed from some place where other configuration classes can reuse it.
public Car buildCar (int year, String make){ 
    CarBuilder cb = CarBuilder.car ();
    if (year!=0) cb.setYear ( year);
    if (StringUtils.hasText ( mark)) cb.setMark ( mark); 
    return cb.factory ();
}
...
// now the Spring definition itself is even simpler, and it's reusable!
@Bean public Car honda () {
	return car (1984, "Honda") ;
}

In Spring 3.1, there are many places where Spring also provides a Builder alternative to a FactoryBean. A Builder, as a pattern, is conceptually simialar to a FactoryBean. In practice, however, they are usually exposed like the CarBuilder demonstrated above. They are typically chainable - a method returns this and so subsequent invocations don't need to dereference the object, they can continue chaining invocations. Additionally, a Builder usually does the null pointer checks that I forced in the previous code. So, a properly rewritten CarBuilder object usage might look like this:

@Bean public Car honda (){ 
 return CarBuilder.car ()
  // doesn't matter if the parameters are null - 
  // it'll validate in the factory () method
  .setYear ( 1984 )  
  .setMark ( "Honda" )
  .factory ();
}

A great example of a builder providing a much smoother experience in 3.1 over Spring's FactoryBeans is the new Hibernate 3 SessionFactoryBuilder, whose usage looks like this:

		
@Configuration  
@EnableTransactionManagment 
public class ServiceConfiguration {  
	
  @Bean public javax.sql.DataSource dataSource (){ ... }
  @Bean public SessionFactory hibernate3SessionFactory (){ 
     return new AnnotationSessionFactoryBuilder ()
 	     .setDataSource (dataSource ())
 	     // you could do this:
 	     //.setAnnotatedClasses ( Customer.class, LineItem.class, Order.class )
 	     // or simply scan a package where your entities live
 	     .setAnnotatedPackages ( Customer.class.getPackage ().getName ()) 
 	     .buildSessionFactory ();
 }
}

The equivalent FactoryBeans now delegate to these builder classes, in fact!

July 22, 2011

Spring Corner: What's a Factory Bean?

I've decided I'm going to try to write up a series of small posts on things about Spring that are perhaps common knowledge, but helpful to know, anyway. These posts are geared decidedly towards newcomers, though old hats might learn a thing or two.

In this post, I'll look at Spring's org.springframework.beans.factory.FactoryBean<T> interface. The definition of this interface is:

public interface FactoryBean<T> {
  T getObject () throws Exception;
  Class<?> getObjectType ();
  boolean isSingleton ();
}

A FactoryBean is a pattern to encapsulate interesting object construction logic in a class. It might be used, for example, to encode the construction of a complex object graph in a reusable way. Often this is used to construct complex objects that have many dependencies. It might also be used when the construction logic itself is highly volatile and depends on the configuration. A FactoryBean is also useful to help Spring construct objects that it couldn't easily construct itself. For example, in order to inject a reference to a bean that was obtained from JNDI, the reference must first be obtained. You can use the JndiFactoryBean to obtain this reference in a consistent way. You may inject the result of a FactoryBean's getObject () method into any other property.

Suppose you have a Person class whose definition is thus:

public class Person { 
 private Car car ;
 private void setCar (Car car){ this.car = car;  }	
}
and a FactoryBean whose definition is thus:
public class MyCarFactoryBean implements FactoryBean<Car> {
  private String mark; 
  private int year ;
  public void setMark (String m){ this.mark =m ; }
  public void setYear (int y){ this.year = y; }
  public Car getObject (){ 
    // wouldn't be a very useful FactoryBean 
    // if we could simply instantiate the object ;-)
    CarBuilder cb = CarBuilder.car ();
	
    if (year!=0) cb.setYear (this.year);
    if (StringUtils.hasText (this.mark)) cb.setMark ( this.mark); 
    return cb.factory (); 
  }
  public Class getObjectType () { return Car.class ; } 
  public boolean isSingleton () { return false; }
}

You could wire up a Car instance using a hypothetical CarFactoryBean like this:

<bean class = "a.b.c.MyCarFactoryBean" id = "car">
	<property name = "mark" value ="Honda"/>
	<property name = "year" value ="1984"/>
</bean>
<bean class = "a.b.c.Person" id = "josh">
	<property name = "car" ref = "car"/>
</bean>

In this example, the result of the FactoryBean's getObject method will be passed, not the actual FactoryBean itself. Spring knows that the result can be injected into the target property because it'll consult the FactoryBean's getObjectType () type and check whether the target property's type is assignable. Spring reserves - but in practice doesn't always exercise - the right to cache the returned bean if the FactoryBean's isSingleton () method returns true.

If you are using Spring's newer (and far more elegant, in my humble opinion) Java based configuration, then you will find this doesn't work quite as you'd expect, but it can still be made to work, but you must dereference the FactoryBean explicitly and call getObject () yourself, like this:

// identical configuration in Java to the XML above			
@Configuration 
public class CarConfiguration { 
  @Bean public MyCarFactoryBean carFactoryBean (){ 
	MyCarFactoryBean cfb = new MyCarFactoryBean ();
	cfb.setMark ("Honda");
	cfb.setYear (1984);
	return cfb;
  }
  @Bean public Person josh  (){ 
	Person p = new Person ();
	p.setCar ( carFactoryBean ().getObject () );
	return p; 
  }	
}

Spring FactoryBeans have all the other characteristics of any other Spring bean, including the lifecycle hooks and services (like AOP) that all beans in the Spring container enjoy.

So, if you'd like a chance to perform construction logic after the properties on the FactoryBean have been set, but before the getObject () method has been called, then you can avail your FactoryBean of a method, annotated with @PostConstruct (or simply implement InitializingBean). This method will be called, in this case, after both the mark and the year properties have been set. You might use this callback to do sanity checks before the object construction's started.

 @PostConstruct 
 public void setup  () throws Throwable { 
   // these methods throw an exception that 
   // will arrest construction if the assertions aren't met
   Assert.notNull (this.mark, "the 'mark' must not be null")	;
   Assert.isTrue (this.year > 0, "the 'year' must be a valid value"); 
 }

One important takeaway here is that it is the FactoryBean, not the factoried object itself, that lives in the Spring container and enjoys the lifecycle hooks and container services. The returned instance is transient - Spring knows nothing about what you've returned from getObject (), and will make no attempt to excercise any lifecycle hooks or anything else on it. This point often confused people.

July 7, 2011

OSCON Java 2011

Hi guys. I'll be speaking at OSCON Java. I'm giving two talks, one on Spring Roo and on using Spring to bring your application to your customers (through a web application, through Flex, through GWT, and through mobile clients like Android). I'm also working with my colleague and friend Steve Mayzak to write an ebook on Spring Roo called "Getting Started with Spring Roo," which will be available from O'REILLY as an ebook in time for OSCON. I hope to see you there. I heard, but can't confirm, that if you register tomorrow (07/07/2011 - the release of Java 7), you'll get a giant discount (77%?). There are also a few other great talks from SpringSource and VMWare there, and I look forward to seeing you there!

<< Last | Next >>