October 25, 2009

My first open source contribution

Few days ago popular Apache Commons Lang library gained new utility method in Validate class: notBlank(). This also happens to be my first contribution to the open source community (see: LANG-533 and commit 828310). Use notBlank when you want to check whether the given string is not null, not empty, and does contain anything apart from whitespaces:

Validate.notBlank("a");
Validate.notBlank(" a ");
Validate.notBlank(null); //will throw IllegalArgumentException
Validate.notBlank(""); //will throw IllegalArgumentException
Validate.notBlank(" ", "Should not be blank"); //will throw IllegalArgumentException
Validate.notBlank(" \n "); //will throw IllegalArgumentException


This handy method can be used to validate user input or configuration, where single space or newline characters should also be treated as invalid. It will be available in version 3.0 of Lang library.

I know this contribution is so tiny and not very significant, probably not even worth to mention, but I am still proud of it ;-).

Meanwhile I past the middle of "Open-Source ESBs in Action", which is a great book comparing Mule and ServiceMix. Sadly, after first two examples I found Mule to be much more intuitive and easy to use, so I skip ServiceMix chapters. Maybe I will go back to them one day, but for now expect few Mule ESB related articles in the future, as this is the topic I would like to focus now.

October 17, 2009

Compile- vs load-time weaving performance in Spring

Yesterday I had pleasure to participate in Java Developers’ Day in Kraków, Poland. It was a great experience to see Mark Richards (author of Java Message Service) and Scott Davis (Groovy Recipes) giving a talk. Also I really enjoyed Wojciech Seliga speak about code review. He works for Atlassian and shown a bit of Crucible, but his main point was that code review is not about looking for bugs made by other developers. It is rather an agile process of getting to know the code.

I could write much more about JDD, of course starting from "you should regret if you haven’t been there", but I am quite sure that you are already waiting for the main topic. Let’s just say, that there is a chance that JDD will take 2 days in the next year and I will do my best to be there.


After reading my previous post one of my friends asked about performance of creating objects marked as @Configurable. He wants to inject EntityManager or other custom dependencies to his JPA POJOs but is concerned about performance. Because many thousands of objects are created manually or by Hibernate during the application life, overhead introduced by Spring aspect injecting dependencies each time new operator is called may be significant. There is no sense in talking about performance, I will simply measure everything!

But before we start our performance comparison test: I haven’t yet explained how to enable compile-time weaving instead of load-time. First a word of explanation: CTW weaves aspect during compilation phase when building your application using Maven. LTW does that when the class is loaded within the JVM. Because both approaches should weave the same aspect and produce the same code, true performance of object creation should be the same despite the weaving method. But we will check that out too.

As you probably expect, switching from LTW to CTW is only a matter of configuration, no code must be changed. All you need to do is remove LTW Spring agent from pom.xml (surefire plugin configuration) and references to the agent when running JVM (on server and unit tests from your IDE). When you got rid of LTW, enable CTW in no time (pom.xml):

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<complianceLevel>1.6</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>



Nothing to be explained, maybe except the complianceLevel, which corresponds to your JDK version. You must also replace:

<context:load-time-weaver/>


with:

<context:spring-configured/>


In your Spring context configuration file.

Now, when we know how to switch back and forth from LTW to CTW, let’s run some performance tests. I used Reservation class introduced in previous posts for testing creation performance, but I added three dependencies via transient fields in this POJO:


@PersistenceContext
private transient EntityManager em;

private transient TicketService ticketService;

@Resource
private transient JavaMailSender javaMailSender;


The TicketService is being injected using the configuration below:

<bean class="com.blogspot.nurkiewicz.reservations.Reservation" scope="prototype" lazy-init="true">
<property name="ticketService" ref="ticketService"/>
</bean>


By testing three beans with different injection techniques I wanted to find out, whether the type of injection has different performance impact.

The test scenario was to first create 1000 instances of Reservation class to warm up JVM and then measure the time of creating 50 thousand instances and putting them in previously created array to partially prevent GC. Objects were created in the following weaving environment:

  • none – no weaving has been applied
  • CTW (no dependencies) – CTW enabled and @Configurable annotation added to Reservation class, but no dependencies injected (@PersistenceContext, @Resource and XML configuration removed)
  • CTW (EM) – CTW with EntityManager injected
  • CTW (<property>) – CTW with dependency configured in Spring XML context file being injected
  • CTW (@Resource) – CTW with dependency autowired using standard @Resource annotation
  • CTW (all) – CTW with all three dependencies listed above injected
  • LTW (no dependencies) – like above but using LTW
  • LTW (EM) – like above but using LTW
  • LTW (<property>) – like above but using LTW
  • LTW (@Resource) – like above but using LTW
  • LTW (all) – like above but using LTW

Each test creating 50K instances has been repeated 8 times with very low standard deviation. I measured the time it took JVM to create all the instances and scaled it to the number of instances being created per second. Bigger value is better.

Probably you start to feel bored so I skip detailed results and give you this nice chart:



The results are a bit surprising for me for two reasons. First, creating objects marked as @Configurable is less than 4 times slower than creating ordinary objects using new operator. I assumed that creating new objects on heap is so greatly optimized that adding the overhead of Spring aspect scanning the class and trying to inject dependencies to completely unknown class would be tremendous. But even if single dependency is being injected using @Resource or XML configuration, creation time is reasonable. In fact, 4-5 times slower when the object is created by Hibernate is something that is almost impossible to measure and see in real environment – simply database and network connectivity brings much bigger overheads, making injection time insignificant. Also, just as I thought, there is no big difference between LTW and CTW when there comes to performance, so use whichever you like. Or at least do not take performance into account when deciding.

The second surprise, negative this time, was the time of creating objects with injected EntityManager. Somehow it is almost 30 times slower than normal object creation and 6 times slower than injecting other custom Spring beans. It is not the subject of this post to find out what happens behind the scenes with EntityManager (probably Spring does some additional magic with EntityManager proxy, maybe I will investigate this in the future), but the results are disturbing.

To summarize: using @Configurable annotation and injecting your Spring beans probably won’t be performance issue in your project. Of course my test isn’t definite and you should check this in your particular case, but the benefits of Spring injection into domain objects can’t be overestimated. But be careful when injecting EntityManager directly to your domain objects – the performance impact is somewhat significant and when creating thousands of objects your application might slow a little bit.

Test environment: Intel Core Duo T2050 1.6 GHz, 1 GiB RAM, Windows XP SP2, JDK 1.6.0_14


October 7, 2009

DDD in Spring made easy with AspectJ

Before I start the main topic, I would like you to think for a while about the best JEE application design you can imagine. No matter you use Spring or EJB3, as they are very similar, probably you would suggest similar approach. Starting from the back you have:

  • domain objects, which are simple POJOs mapped directly to database relations. POJOs are great because JavaBean-style properties are well understood be many frameworks.
  • data access layer – typically stateless services, which wrap up database access code (JDBC, Hibernate, JPA, iBatis or whatever you want) hiding its complexity and providing some level of (leaky) abstraction. DAOs are great because they hide nasty and awkward JDBC logic (that is why some question the need for DAOs when using JPA, but this is out of scope of this post), serving as a, more-or-less, translator between database and objects.
  • business services layer – another set of stateless services, which operate on domain objects. Typical design introduces a graph of objects that take or return domain objects and perform some logic on them, again, typically accessing database via data access layer. Service layer is great because it focuses on business logic, delegating technical details to DAO layer.
  • user interface – nowadays, typically via web browser. User interface is great because... just the fact it is.
Beautiful, isn’t it? Now open your eyes, it is time for a cold shower.

Both services and DAOs are stateless, because Spring and EJB3 favors such classes - so we learnt to live with it. On the other hand, POJOs are "logicless" – they only contain data, maintain their state without operating on it and introducing no logic. If we think about introducing "reservation" domain object to our application, we immediately think of Reservation POJOs mapped to RESERVATIONS database table, ReservationDao, ReservationService, ReservationController, etc.

Still don’t see the problem, Java, thus OOP programmer? How would you describe "object"? It is some virtual being having internal (encapsulation) state and some public operations, which have explicit access to the state. Most fundamental concept of object-based programming is to take data and procedures operating on that data together and close them tightly. Now take a look at your best design ever, do you really need objects? This is the dark secret of Spring, EJB3, Hibernate and other well established frameworks. The secret, which all of us subconsciously try to forget: we are not OOP programmers anymore!

POJOs are not objects, they are simply data structures, collections of data. Getters and setters are not true methods, actually, when was the last time you wrote them by hand? In fact, the need to autogenerate them (and refactor, add and remove when attributes change) sometimes happen to be very frustrating. Wouldn’t it be simpler just to use structures with public fields by default?

On the other hand, look at all those great stateless services. They do not have any state. Although they operate on domain objects, they are not part of them or not even aggregate them (low cohesion). All the data is passed explicitly through the method parameters. They aren’t objects as well – they are simply collection of procedures arbitrary gathered together on a common namespace, corresponding to class name. In contracts, methods in OOP are also procedures behind the scenes, but having implicit access to this reference, which points to the object instance. Whenever we call ReservationService or ReservationDao providing Reservation POJO reference explicitly as one of the arguments, we actually reinvent OOP and code it manually.

Let’s face it, we are not OOP programmers, as everything we need are structures and procedures, invented fifty years ago... How many Java programmers are using inheritance and polymorphism in a day-to-day basis? When was the last time you wrote object having private state without getters/setters with only few method having access to it? When was the last time you created object with non-default constructor?

Luckily, what Spring have taken, it brings back with even more power. The power is called AspcetJ.

In my last post I created Reservation entity having three business methods: accept(), charge() and cancel(). It looks really good to have business methods concerning domain object placed directly in that object. Instead of calling reservationService.accept(reservation), we simply run reservation.accept(), which is much more intuitive and less noisy. Even better, what about writing:

Reservation res = new Reservation()
//...
res.persist()


instead of calling DAO or EntityManager directly? I don’t know much about domain-driven design, but I found this fundamental refactoring to be the first gate you must walk through to enter the DDD world (and go back to OOP as well).

So, if having business methods directly on domain objects is so great, why not everybody’s doing it? The answer is very straightforward and down-to-earth – because they don’t know how! Reservations’ accept() method will eventually need to delegate some logic to external services, like accounting or sending e-mails. Naturally, this logic is not part of Reservation domain object and should be implemented elsewhere (high cohesion). But most Spring programmers don’t know how or are scared of injecting other services to domain objects. When all services are managed by Spring, everything is simple. But when Hibernate creates domain objects itself or the object is created using new operator, Spring has no knowledge of this instance and cannot handle dependency injection. So how would Reservation POJO obtain Spring beans or EntityManager encapsulating necessary logic?

...

First, add @Configurable annotation to your domain object:

@Configurable
@Entity
public class Reservation implements Serializable {
//...
}


This tells Spring that Reservation POJO should be managed by Spring. But, as mentioned above, Spring has no knowledge of Reservation instances being created, so it has no occasion to autowire and inject dependencies. This is where AspectJ comes in. All you need to do is to add:

<context:load-time-weaver/>


To your Spring XML descriptor. This extremely short XML snippet tells Spring that it should use AspectJ load-time weaving (LTW). Now, when you run you application:

java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar
at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:82)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 59 more

It will fail... Java is not magic, so before we proceed, few words of explanation – don’t be impatient. Adding XML snippet above does not solve anything. It simply tells Spring that we are using AspectJ LTW. But when application starts up, it does not find AspectJ and tells us about it decently. What happens if we add -javaagent:spring-agent.jar to our JVM command line parameters as suggested? This Java agent is simply a plugin to JVM that overrides loading of every class. When Reservation class is loaded for the first time, agent discovers @Configurable annotation and applies some special AspectJ aspect to this class.

To be more precise: bytecode of Reservation class is being modified, overriding all constructors and deserialization routines. Thanks to this modification, whenever new Reservation class is being instantiated, apart from normal initialization, those additional routines added by the Spring-provided aspect perform dependency injection. So since now enhanced Reservation class is Spring-aware. It does not matter whether reservation has been created by Hibernate, Struts2 or using new operator. Hidden aspect code always takes care of calling Spring ApplicationContext and ask it to inject all dependencies to domain object. Let us take it for a test drive:

@Configurable
@Entity
public class Reservation implements Serializable {

@PersistenceContext
private transient EntityManager em;

@Transactional
public void persist() {
em.persist(this);
}
//...
}


This is not a mistake – I injected EntityManger from JPA specification directly to domain object. I also put @Transactional annotation over persist() method. This is not possible in ordinary Spring, but since we used @Configurable annotation and AspectJ LTW, code below is completely valid and works as expected, issuing SQL and committing transaction against the database:

Reservation res = new Reservation()
//...
res.persist()


Of course, you can also inject regular dependencies (other Spring beans) to your domain objects. You have choice of using autowiring (@Autowire or even better @Resource annotations) or setting properties manually. The latter approach gives you more control, but forces you to add setter for Spring bean in domain object and define another bean corresponding to domain object:

<bean class=" com.blogspot.nurkiewicz.reservations.Reservation ">
<!-- ... -->
</bean>


Please note that I haven’t provided name/id for this bean. If I would, the same name should be passed to @Configurable annotation.

Everything works like a charms, but how to use this amazing feature in your real life work? First of all, you must setup your unit tests to use Java agent. In IntelliJ IDEA I simply added:

-javaagent:D:/my/maven/repository/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar

to VM parameters text field in JUnit run configuration. If you add this to default ("Edit defaults" button), this parameter will be applied to every new unit test you run. But configuring IDE is not as much important as configuring your build tool (hopefully maven). First of all you must ensure that Spring Java agent is downloaded and available. Thanks to maven dependency resolution, this can be easily achieved by adding dependency:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-agent</artifactId>
<version>2.5.6</version>
<scope>test</scope>
</dependency>


The JAR is not actually needed by test code, but by adding this dependency we guarantee that it is downloaded before tests run. Then, simple tweak in surefire plugin configuration:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>always</forkMode>
<argLine>
-javaagent:${settings.localRepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar
</argLine>
</configuration>
</plugin>


Really simple – location of spring-agent.jar can be safely constructed using maven repository path. Also forkMode must be set in order to reload classes (and cause LTW to happen) each test is executed. I think configuring your app server and/or startup scripts does not need any further explanation.

That is all about Spring and AspectJ integration via load-time weaving. Few simple configuration steps and a whole new world of domain-driven design welcomes. Our domain model is no longer weak, entities are "smart" and business code is more intuitive. And last but not least – your code would be back object-oriented, not procedural.

Of course, you might not like load-time weaving, as it interferes with JVM class loading. There is another approach, called compile-time weaving, which weaves aspects on compile time rather than class loading time. Both methods have pros and cons, I will try to compare both of them in the future.