Increase Max Connections per Route With Maven

The Problem

We’re doing a lot of stuff within our build infrastructure which is based on Java and Maven. For example for creating a release of our product we have to change all snapshot versions of all Maven POMs (>60) to a tagged version, then compile and deploy the artifacts and afterwards commit the changed POMs and projects back to Subversion. When building the artifacts we use some self written Java tools which access Nexus (the Maven repository) in order to retrieve POM information. For whatever reason we encountered problems during the last runs because the maximum amount of connections was obviously exceeded. The log file showed something like this:

2479 [DEBUG] []: Get connection: HttpRoute[{}->], timeout = 02480 [DEBUG] []: [HttpRoute[{}->]] total kept alive: 0, total issued: 0, total allocated: 0 out of 40
2481 [DEBUG] []: No free connections [HttpRoute[{}->]][null]
2482 [DEBUG] []: Available capacity: 20 out of 20 [HttpRoute[{}->]][null]
2483 [DEBUG] []: Creating new connection [HttpRoute[{}->]]
2484 [DEBUG] []: Connecting to
2485 [DEBUG] []: CookieSpec selected: compatibility
2486 [DEBUG] []: Re-using cached 'basic' auth scheme for
2487 [DEBUG] []: Attempt 1 to execute request
2488 [DEBUG] []: Sending request: GET /nexus/content/groups/path/to/some/artifact/master/1.1.38/master-1.1.38.pom HTTP/1.1

Line 2 and 3 showed that there were no more connections left. The classes mentioned in the log file are all part of Apache’s HTTP Components project. And, in addition, our tool was also using classes from Maven’s Embedder and Maven’s Wagon project. So the challenge was to find out if and how it is possible to increase the default number of allowed connections per route. It was not obvious from the beginning who actually instantiates the class org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager (line 1 above) which tries to establish a new connection to the web server.

The Cascade

After analyzing our own code and checking out all the above mentioned projects from Apache, I found the cascade that actually triggers the connection. I’ll show only the components, not the actual classes or code. Basically it looks like this:

  1. 1. our code (not allowed to show)
  2. 2. Maven Embedder
  3. 3. Maven Wagon

Within Maven Embedder, there’s a class org.apache.maven.wagon.shared.http4.AbstractHttpClientWagon and there I found the code that actually solved the problem:

ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
int maxPerRoute =
      Integer.parseInt( System.getProperty( "maven.wagon.httpconnectionManager.maxPerRoute", "20" ) );
threadSafeClientConnManager.setDefaultMaxPerRoute( maxPerRoute );
int maxTotal = Integer.parseInt( System.getProperty( "maven.wagon.httpconnectionManager.maxTotal", "40" ) );
threadSafeClientConnManager.setDefaultMaxPerRoute( maxPerRoute );
threadSafeClientConnManager.setMaxTotal( maxTotal );

This code instantiates class org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager and the values for max connections per route and max total are set by retrieving the appropriate system properties and, if not set, will use the defaults which caused our problems.

The Solution

So the solution was quite obvious. Just set these properties on the command line like this

java -jar our-magic-tool-1.2.3.jar -Dmaven.wagon.httpconnectionManager.maxPerRoute=40

or set it in your code using a call to

System.setProperty("maven.wagon.httpconnectionManager.maxPerRoute", "40")

I found not a single hint while googling a solution so I thought it might be worth a post here. You may want to leave a comment if this post has helped you out of a similar problem.

, , , , , , , ,

No comments yet.

Leave a Reply

* Copy This Password *

* Type Or Paste Password Here *