24 Feb: A best Spring RestTemplate!

The RestTemplate is the central class for client-side HTTP access. It simplifies communication with HTTP servers, and enforces RESTful principles. It handles HTTP connections, leaving application code to provide URLs (with possible template variables) and extract results. The template uses a ClientHttpRequestFactory for creating HTTP connections. The default ClientHttpRequestFactory is a SimpleClientHttpRequestFactory that uses standard J2SE classes like java.net.HttpURLConnection. And this is not really production ready… (why?) So, the solution is to use the org.springframework.http.client.HttpComponentsClientHttpRequestFactory, which is…

24 Feb: Use Cloudfoundry in Spring without the dedicated XML namespace

Spring allows you to configure a dead simple connection on a provisioned service, like mysql, redis, rabbitmq and many others. It’s really simple, since you don’t even need to configure explicit credentials and connection strings. Instead, you can retrieve a reference to this service from the cloud itself using the CloudFoundry “cloud” namespace. Problem is, you have to create an XML file, and we have more and more projects without XML anymore. So, how to use the…

24 Mar: Automatic retry on failure

What the need? To make processing more robust and less prone to failure, sometimes it helps to automatically retry a failed operation in case it might succeed on a subsequent attempt. For example a call to a web service that fails because of a network glitch or a DeadLockLoserException in a database update may resolve themselves after a short wait. So, how to…? To automate the retry of such operations the Spring team developped a retry module…

15 Mar: Using the new Spring Cache Abstraction in Spring Security

What the need? Caching is handled by storing the UserDetails object being placed in the UserCache. This ensures that subsequent requests with the same username can be validated without needing to query the UserDetailsService. It should be noted that if a user appears to present an incorrect password, the UserDetailsService will be queried to confirm the most up-to-date password was used for comparison. Caching is only likely to be required for stateless applications. In a normal web…

26 Sep: String – Best Pratices

Never, never, never use the String constructor Whenever you want to instantiate a String object, never use its constructor but always instantiate it directly. For example: //don’t: String slow = new String(“Yet another string object”); //do String fast = “Yet another string object”; From the Javadoc: Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable. Pattern and regular expressions For performance reasons, instances of pattern (java.util.regex.Pattern) should be…

08 Sep: HttpClient – Best Pratices

URLConnection -> HttpClient Except to manage specific resources other than http / https (ftp, gopher, file …), use the HttpClient and not the HttpURLConnection, and avoid to use both at the same time. The HttpClient is more mature and has a richer feature set as multi-threaded management, cookies, pipelining, connection pools … Instantiation Instantiating a HttpClient is costly in resources, we must focus on reuse, even singletons, as described in the doc: « Generally it is recommended…