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.

How to configure your app for ASYNCHRONOUS TASKS

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

Step2: Activate @Async and Task executor for asynchronous calls

[gist https://gist.github.com/dad185825042efd0280f]

 

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…

Posted in Spring

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.

Usage:-

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).

 

Read more ›

Tagged with: ,
Posted in Eclipse

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:-

Read more ›

Tagged with: , , , ,
Posted in Best Practices