HttpClient 4.x Timeout

HttpClient is one of the most versatile Java libraries. Unfortunately, it comes with a lot of configuration options that may be way too cryptic or difficult. While the API for 4.x series has been significantly improved there are still some sharp edges.

The deprecated or 3.x way of setting the timeout.

This is done using params. Note that this is still 4.x code but a deprecated one.

DefaultHttpClient httpClient = ...;
HttpParams httpParams = httpClient.getParams();
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000L);
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000L);
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, 1000L);

Now the part httpClient.getParams() is deprecated since obviously this is a nasty API. You need to keep track of parameters with Enums/constants and their type as well.

The right 4.x way aka the builder way

HttpClient 4 is full of builder for everything. While I often love the Builder patterns in some cases the testing part is really difficult but I guess it is always a tradeoff. This would be the "right way".

public RequestConfig requestConfigWithTimeout(int timeoutInMilliseconds) {
        return RequestConfig.copy(RequestConfig.DEFAULT)

The meaning of the parameters

Notice that we are actually setting 3 different timeouts.
  • Socket Timeout - this is the time of inactivity to wait for packets to arrive
  • Connection Timeout - the time to establish a connection with the remote host
  • Connection Manager Timeout - the time to fetch a connection from the connection pool
The third one costed me dearly, it wasn't until the client was under a high load that issues started happening. The connection pool I was using had 10 connections per route limitation set and the scenario was quite common. As you can see these settings do not provide a mechanism for making an N millisecond hard timeout.

If we were to setup the timeout to 10 seconds on each of this we could end up with a request that lasts 9(sec to get a connection) + 9( sec to open connection ) + 9( sec of inactivity ) = 27 sec.

More info

A bug when setting the ConnectionRequestTimeout
The RequestConfig Javadoc
Apache HttpClient examples
Published on Feb 4, 2015 by Mite Mitreski
Updated on 2/04/2015 12:51:00 AM

JavaDay 2014 a leap forward

Java Day is an event that organizes for the past 5 years. From the small group of enthusiastic presenters in EU-info center in Skopje to a full 7h filled with lectures at a cinema hall JavaDay has grown to be one of the leading technical events in the area.

What has changed over the years and what the future holds

We started out as a small event with only local speakers in 2009 happening each year since then. In 2013, we had 1 person outside of Macedonia. The need for diversity was becoming more apparent. 2014 was the year we went fully international with 6 speakers coming from Switzerland, Serbia, and Bulgaria. We had participants in the audience from all the neighboring countries.
The Bulgarian JUG presence was a great addition and an awesome connection between our JUGS.

The keynote 

Corsin Decurtins, CTO of Netcetera gave an awesome talk on "how to become a great developer".
To quote the abstract:
At the core, the job of a software developer is and has always been the same: writing good, elegant, sustainable and bug-free software that exceeds the expectations of your clients. But the context in which we do our job is changing and with it the skills required to be a great software developer. In this talk, I want to go through a couple of things that I think make the difference between a developer and a great developer. This includes some technical skills and practices, but also non-technical things that you might not consider relevant for a developer at first.
The talk was extremely well perceived by the audience. This is very visible by the survey where he got the best reviews also by the fact the we were approached multiple times by persons telling us we need more of this.

Why this type of events are important to the community 

Conferences help you get in touch with other like-minded persons. They keep you up to date with modern technologies and interesting concepts.  Also it is great for the companies where it enables them to reach narrowly selected audience, they get to promote himself and be recognizable.

Female participation @ JavaDay

This year we also had our first female speaker Raluca Breaur one of the organizers or the Agile meetups and Java User Group in Iasi, Romania. She had an awesome talk "Caching – oldie but goldie"  with great analogies for caching systems.
It is unfortunate that after so many JavaDay this was our first female speaker especially since the from the side of the attendees we had more than 30%. 
Few weeks later a nice initiative was started by forming a local chapter of JDutchess. Few of them were also involved in the organization and we extremely grateful.
I am just hoping that next years event will have lot more of this group.

What's next 

Hopefully sometime in the near future we are gonna get the video recordings processed and uploaded to Parleys.
We continue next year and try to make the event better. Going bigger is also an option but we are not sure if that is the right direction. We definitely wanna raise the organization level and the quality of the event.
Special thanks to the 7 sponsors that supported up and without them the event could not be free.

We had a little competition about choosing the next name for JavaDay so next year we might be called Javalicious.  
The organizing  team

The survey

Notice the Gaussian distribution for the speaker reviews :) 

Published on Jan 31, 2015 by Mite Mitreski
Updated on 1/31/2015 10:49:00 PM

A New Try-with-resources Improvement in JDK 9

The JEP 213 - Milling Project Coin is a follow up to the Project Coin additions to Java 7. Note, as the JEP states, this isn't a Project Coin 2.0. It's more of an attempt at smoothing the "rough edges" that came along with these additions. Just as a reminder - with the original Project Coin, among other things, we got:
  • Strings in switch
  • Binary integral literals and underscores in numeric literals
  • Multi-catch and more precise rethrow
  • Improved type inference for generic instance creation (diamond)
  • Try-with-resources statement
  • Simplified varargs method invocation
Try with resources has been a great simplification, especially when working with I/O code. The addition in JDK 9 is an improvement of the standard try-with-resources way of writing code. Previously in Java 7 and 8 we had;

BufferedReader reader = new BufferedReader(new InputStreamReader(;
// Original try-with-resources statement from JDK 7 or 8
try (BufferedReader r1 = reader) {
// use buffered reader
} catch (IOException e) {
// ignoring exceptions because that is how I roll

which works just fine, but we still needed to declare a special variable in the try-with section. The cleaner and simpler way in JDK 9 is now;

// The JDK 9 way
try (reader) {
// use the reader
}catch (IOException e){
// ignoring exceptions because that is how I roll

This is more "natural" way of writing even though it most use cases we don't need the resource outside the scope of the try block. The restriction is that the reader variable should be effectively final or just final. You can play around with the new way already in the latest JDK 9 snapshot. More info available in Joe Darcy's Oracle Weblog post and the related  JDK bug report.

 Originally published on
Published on Jan 21, 2015 by Mite Mitreski
Updated on 1/21/2015 04:40:00 PM