Spring Task Execution API to speeding up asynchronous calls

Is your app running slow… how about making some part of your code asynchronous. Its really easy and 5 minutes task to make it so using Spring Task Execution API. Why to bother about creating your own thread objects, implementing interfaces.

Try this out, a 5 minutes recipe…..

Spring 3 provides a Task Execution API that allows us to wrap beans with a proxy that calls @Async annotated methods asynchronously. The easiest approach would be simply to annotate your desired service method with @Async, however you need to limit the scope of such method on the forked thread, which means no db operations,  no user interaction, etc can go inside such method.


Just adding the @Async annotation isn’t enough. We need to do little configuration in applicationContext.xml and all done…

Step 1: Add Task namespace and Task namespace schema location

<beans xmlns="http://www.springframework.org/schema/beans"</span>
xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jbpm="http://drools.org/schema/drools-spring" xmlns:cache="http://www.springframework.org/schema/cache"
 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

Step2: Activate @Async and Task executor for asynchronous calls

<task:annotation-driven />

<task:executor id="ekramExecutor" pool-size="5-15" queue-capacity="50" keep-alive="500" />

Details of task:executor

Attribute Description
id Bean ID
pool-size Number of threads in the pool. Either a single integer or a range such as "5-15".
queue-capacity Task queue capacity.
keep-alive time in seconds for threads in excess of the core pool size.
rejection-policy Policy when threads are rejected (queue exhausted and pool size at maximum). Options are ABORT (default), CALLER_RUNS, DISCARD and DISCARD_OLDEST.

For complete project, check out https://github.com/ekramalikazi/spring-async-cache

Try this out, improve end-user response time and shine... :) Enjoy...

Eclipse – Unlocking Scrap Page, really powerful…

A scrap page is a small file in which one can try out Java expressions. Yes of-course we can do any crap thing on this page…  :)

To create a scrap page choose File –> New –> Other –> Java –> Java Run/Debug –> Scrapbook Page , give it a name and select a destination folder and we have our own empty scrap page with .jpage extension.

OR alternatively, New –> Other –> type scrap in the <type filter text> wizard and select Scrapbook Page.


Lets start with traditional Hello World example. Open up scrap page and try Hello World thing. To execute, select the statements and right click and Execute (or Ctrl+U).

String text = "Hello World!! I am a scrap page!!";

Read more of this post

Java Best Practices : Part I

Many a times we override default behavior of Object class. we need to take utmost care while overriding those methods by ourselves about their contracts. Moreover, we need to write unit tests for our implementation. Consider using Apache and other libraries. They are here to rescue us and save our development time…. 

1. Do you wish to override toString() method? Don’t do it yourself….

toString() method returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
Instead of writing one on your own, try using apache commons class as:-

 public String toString() {
 return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);

Apache library gives you more control, like excluding certain attributes like password, can use

 public String toString() {

final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(this);
 new String[] {"contentStreamLength", "password"});
 return reflectionToStringBuilder.toString();


2. Have you manipulated Strings, like comparing String for not null and not empty?

if (str == null || "".equals(str)){


Imagine you want to compare 2 strings with equals, checking both should not be null, and its undoubtedly big statement.

We have StringUtils from apache to help us with all such string manipulations:-

Some usage examples:-

if (StringUtils.isEmpty(toPhone)) {


if (StringUtils.equalsIgnoreCase(userAnswer1, userObj.getSecurityAnswer1())
&& StringUtils.equalsIgnoreCase(userAnswer2, userObj.getSecurityAnswer2())
&& StringUtils.equalsIgnoreCase(userAnswer3, userObj.getSecurityAnswer3())){


String emptyString = StringUtils.EMPTY;

StringUtils.substringAfter(url, "/account/user/reset/");

These are only few benefits of using StringUtils. Explore more on this, when you need to manipulate Strings.

3. Do you wish to override equals and hashcode methods? Don't do it yourself....

import org.apache.commons.lang.builder.EqualsBuilder;

import org.apache.commons.lang.builder.HashCodeBuilder;

public class Emp{

private String name;
 private int sal;
 private List<Emp> subordinates;

 public int hashCode(){

 return new HashCodeBuilder()


 public boolean equals(final Object obj){

if(obj instanceof Emp){

final Emp other = (Emp) obj;
 return new EqualsBuilder().append(name, other.name)
 .append(sal, other.sal)
 .append(subordinates, other. subordinates)

} else{

return false;



4. Do you wish to override clone method for your POJO? Don't do it yourself....

Writing correct clone method is a challenging task and that performing all sort of unit testing is much time consuming. Also if we modify our POJO, the clone method needs to modified and tested accordingly. The most important thing is about deep cloning.

Cloning can be potentially dangerous. Cloning files, streams can make the JVM crash. Also cloning proxies (i.e. objects returned by ORM libraries) means a big graph of objects might be cloned which can lead to performance issues and potential crashes.

Here, is a quick solution for cloning and no need to implement and override any method. No coding means no testing… :)



Get every new post delivered to your Inbox.