Jethro Borsje

personal website

  • Increase font size
  • Default font size
  • Decrease font size

2014-01-18 - Hiking @ Hitland

In January we went hiking in Hitland a couple of times, one of those times was Saturday the 18th of January. The weather here in The Netherlands is very warm for this time of year, so instead of ice skating we plan to go hiking regularly. During our visits to Hitland we saw various types of birds. The most notable one was the Great Egret also known as the White Heron. Although the Grey Heron is very common in The Netherlands the White Heron is seen here less often (although it is not rare). We didn't spot the White Heron before in Hitland, but this year we've seen a few of them on several different occasions. It is nice to see that the diversity of birds is increasing in Hitland. Other birds we saw this year include the Gadwell, the Common Pochard and the Red-crested Pochard.

Bellow are some pictures of the 18th:


2014-01-12 - Hiking @ Meijendel

On Sunday the 12th of January we went hiking with the three of us for one of the first times. We decided to go to the Meijendel which is a coastal area near The Hague. The area is comprised of dunes and woods and is a nice place to go for a hike. In the weekends it can be a bid crowded, especially if the weather is nice like it was on the 12th. When we arrived it was less crowded than we expected and we ended up hiking about 7km without to much crowds.

Bellow are some pictures of this hike:

Last Updated on Tuesday, 25 February 2014 15:24

Spring @Async and exception handling


When using Spring to asynchronously execute pieces of your code you typically use the @Async annotation on your Spring @Components methods. When using the @Async annotation Spring will use AOP (Aspect Oriented Programming) to wrap your call in a Runnable. This Runnable will then be scheduled for execution on a TaskExecutor. More information about asynchronous execution and its transactional context can be found here.

Exception handling

Spring comes with a number of pre-packaged TaskExecutors which are documented in the Spring documentation here. In practice you will likely use the ThreadPoolTaskExecutor or the SimpleAsyncTaskExecutor. When doing so you might at times wonder why a task has not been executed. This happens when an exception occurs inside the method you are trying to execute asynchronous. The aforementioned task executors do not handle the exceptions so the execution fails silently. This problem can be solved by implementing your own AsyncTaskExecutor which handles the exceptions by logging them (or in any other way you wish). Bellow you'll find an example implementation of such a AsyncTaskExecutor.


import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class ExceptionHandlingAsyncTaskExecutor implements AsyncTaskExecutor {
    private AsyncTaskExecutor executor;

    public ExceptionHandlingAsyncTaskExecutor(AsyncTaskExecutor executor) {
        this.executor = executor;

    public void execute(Runnable task) {

    public void execute(Runnable task, long startTimeout) {
        executor.execute(createWrappedRunnable(task), startTimeout);

    public Future submit(Runnable task) {
        return executor.submit(createWrappedRunnable(task));

    public  Future submit(final Callable task) {
        return executor.submit(createCallable(task));

    private  Callable createCallable(final Callable task) {
        return new Callable() {
            public T call() throws Exception {
                try {
                } catch (Exception ex) {
                    throw ex;

    private Runnable createWrappedRunnable(final Runnable task) {
        return new Runnable() {
            public void run() {
                try {
                } catch (Exception ex) {

    private void handle(Exception ex) {
        System.err.println("Error during @Async execution: " + ex);

This task executor decorates another task executor and wraps all Runnables and Callables in versions which handle the exception. In order to be able to use this task executor it has to be configured in the Spring application context as a managed bean and it has to be passed the real executor (the one that does the actual work) in its constructor. An example of such a configuration can be found bellow.


JUnit testing

In the application context used for JUnit testing Spring beans the asynchronous execution of methods can be cumbersome. You typically want to validate the inner workings of your methods not Springs asynchronicity. To achieve this you can use the SyncTaskExecutor provided by Spring. This task executor runs the tasks it is given immediately on the thread that adds the task instead of using a thread pool and running the tasks asynchronous. This task executor is configured in the test scope application context as follows.

Last Updated on Sunday, 26 January 2014 09:12

Spring @Async and transaction management


There are cases in which it is necessary to execute pieces of code asynchronous. An example is the sending of a (JMS) message from your system to another system. If it is not important for the database transaction whether or not the message has been sent successfully you can send the message asynchronous. The advantage is that the user does not have to wait in the front-end while the message is being send. Another example of possible asynchronous execution is the case where messages have a clear ordering. In order to try to prevent message A from being overtaken by message B you might want to schedule the sending of message B asynchronous with a delay.

Spring Framework

Spring supports the asynchronous execution of methods on your @Components by means of the @Async annotation. When using this annotation on a method of your @Component Spring will always execute this method asynchronous. The Spring framework will use AOP (Aspect Oriented Programming) on the Spring proxy of the @Component to wrap calls to this method in a Runnable and schedule this Runnable on a task executor. This task executor has a thread pool and when a thread in the pool becomes available the Runnable will be executed on this thread. The caller of the method annotated with @Async does not wait untill the Runnable has been executed but terminates after the Runnable has been scheduled.

Return values

If the method to be executed asynchronously has a return value it should return a Future in its signature. This Future can be used by the caller to track the progress of the asynchronous task and to retrieve the result of the task once it has been completely executed. The @Async method should return an ASyncResult (which implements the Future interface) containing the actual result.

Transaction management

If the @Async annotation is being used extra care should be taken with respect to transactions. In normal circumstances (without @Async) a transaction gets propagated through the call hierarchy from one Spring @Component to the other.

However, when a @Transactional Spring @Component calls a method annotated with @Async this does not happen. The call to the asynchronous method is being scheduled and executed at a later time by a task executor and is thus handled as a 'fresh' call, i.e. without a transactional context. If the @Async method (or the @Component in which it is declared) is not @Transactional by itself Spring will not manage any needed transactions.

In case the method sends a message to an ESB (Enterprise Service Bus) using JMS (or another protocol) this can lead to problems, because you probably want to use the transaction manager declared in your Spring application context (typically a JTA transaction manager). In order to make Spring manage the transaction of the @Async method either the @Component or the method itself should declare the @Transactional annotation, this way Spring will manage the transaction even if a method is being executed asynchronous.


A final reminder: both @Transactional and @Async work with AOP an proxying. The methods to which these annotation are applied should be public, otherwise the annotations are not picked up by Spring. When JUnit testing methods annotated with @Async in combination with transactions and automatic rollbacks extra care should be taken, this will be the topic of another blog post in the near future.

Last Updated on Monday, 13 January 2014 23:02

Page 3 of 3


Latest Comments

Recently played

My apps

Who's Online

We have 21 guests online