Dear reader,
While working with Apache HttpClient you may face this error:
java.lang.IllegalArgumentException: host parameter is null
at org.apache.commons.httpclient.HttpConnection.<init>(HttpConnection.java:206)
at org.apache.commons.httpclient.HttpConnection.<init>(HttpConnection.java:155)
at org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnectionWithTimeout(SimpleHttpConnectionManager.java:175)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:153)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
This comes in a multi-threaded stress test, when under heavy load concurrently opening many HttpClient connections,
intermittently receives an IllegalArgumentException with the following stack trace:
java.lang.IllegalArgumentException: host parameter is null
at org.apache.commons.httpclient.HttpConnection.<init>(HttpConnection.java:206)
at org.apache.commons.httpclient.HttpConnection.<init>(HttpConnection.java:155)
at org.apache.commons.httpclient.SimpleHttpConnectionManager.getConnectionWithTimeout(SimpleHttpConnectionManager.java:175)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:153)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
Again, this issue comes only intermittently under heavy connect load. There appears some sort of timing-related error in HttpClient.
This comes in a “multi threaded environment when there are too many instances of HttpClient” created and system is not able to resolve HostName quickly.
Solution: Set explicitly the HostConfiguration before creating the GetMethod:
PostMethod method = null;
HttpClient httpclient = new HttpClient();
String theURL = YOUR_PREPARED_URL;
HostConfiguration hf=new HostConfiguration();
hf.setHost("http://localhost", 22);
method = new PostMethod(theURL);
method.setHostConfiguration(hf);
LogHelper.logMessage("Before sending SMS Message: "+message);
int respCode = httpclient.executeMethod(method);
System.out.println("Response code:"+respCode); //200 means successful.
So far this will eliminate this intermittent failure.
//Code is tested and works fine..
Tuesday, May 31, 2011
java.lang.IllegalArgumentException: host parameter is null
Subscribe to:
Post Comments (Atom)
I found your blog very interested.
ReplyDeleteAnyway, Java hosting is no more expensive. Even without their discounts www.jvmhost.com seems to be the cheapest Java provider on the market
I just want to thanks you for the great tips on how to deal with it. you are such a great blogger!
ReplyDeleteThis code does not make any sense; plus it would result in a nullpointerexception.
ReplyDeleteyou are calling method.setHostConfiguration(hf); before initializing method object??? Haven't tried it... but doesn't make sense, or i missed something?
ReplyDeleteAsad,
ReplyDeleteYou are correct, the order is swapped while writing as I have given just a code snippet. By the way I am rectifying now.
Thanks.
Great tutorial and I leaned few new points here, Thanks a lot for sharing this great knowledge here. cheap and best web hosting in India
ReplyDelete