Jethro Borsje

personal website

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

2014-01-11 - Ice skating @ Rotterdam

On Saturday the 11th of January I went ice skating for the first in 2014. I felt pretty fit so decided to push it beyond the 50 laps of the last couple of times. Initially I aimed on 60 laps, but because it all went pretty fast I adjusted my goal towards 75. However when I reached the 71st lap I had to stop because of maintenance of the ice. Looking back I'm pretty satisfied with the 71 laps (28,4 km). Perhaps the 75 laps will be reached next time.

 

Spring @Async and transaction management

Introduction

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.

Caveats

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
 

2013-12-30 - Ice skating @ Rotterdam

On Monday the 30rd of December I went ice skating for the last time in 2013. Because we like the ice rink in Rotterdam we have decided to go there from now on instead of De Uithof in The Hague. It saves us a 35 minute drive one way each time we go! This time around I skated the 50 laps I wanted plus the one extra lap I missed last time making it a total of 20,4 kilometers.

Let's hope we get some cold weather the next two or three months so we can go ice skating on some real ice!

 

2013-10-19 - Hiking in National Park De Sallandse Heuvelrug

On Saturday the 19th of October we went hiking in National Park De Sallandse Heuvelrug. This is a national park in the eastern part of The Netherlands which consists of woods, meadows and small hills. The hills make it feel you're in Germany or Belgium instead of in The Netherlands, because of this we like to go there. This time around we didn't spot lots of wildlife apart from a beautiful European Robin. Autumn has come in The Netherlands so the trees are starting to lose their leaves and there are various kinds of mushrooms in the forest. We always love to hike through a forest in autumn because of all the different colors, so we had a great day. We ended up hiking 7,2 kilometers.

Bellow you'll find some pictures and a GPS track log:



View 2013-10-19 - Wandelen @ NP De Sallandse Heuvelrug in a larger map

 

2013-10-18 - Hiking in National Park Hoge Veluwe

On Frinday the 18th of October we went hiking and cycling in National Park the "Hoge Veluwe". We arrived there early in the afternoon and decided to use the free white bikes to cycle through the park. We were hoping to see some deer and maybe some other wild life. We also wanted to visit the Kröller-Müller Museum which houses lots of paintings from Vincent van Gogh and is situated in the middle of the National Park. We started our visit with a bicycle ride via a scenic trail through the park. Every now and then we stopped to do a short hike and enjoy the peace and quite around us. After doing a loop through the park we headed for the museum where we spent an hour or two. Up to this moment we didn't spot any interesting wild life, so we decided to cycle a bit more before we left the park. We ended up seeing one deer before heading back to the parking lot which is less than we hoped for. We ended up traversing 11 km through this nice National Park. It was very nice to visit this National Park for a change and we plan on going back there next summer.

Bellow you'll find some pictures and a GPS track log:



View 2013-10-18 - Wandelen Fietsen @ NP De Hoge Veluwe in a larger map

Last Updated on Thursday, 05 December 2013 15:51
 


Page 3 of 4

JoomlaGATor!

Latest Comments


Recently played

My apps

Who's Online

We have 31 guests online