4

In my app more than one IntentService connect/communicate with a Tomcat server at random intervals depending on user interaction with the application.

I am using the following Singleton for Http

public class CustomHttpClient { 
    private static HttpClient customHttpClient; 
    /** A private Constructor prevents instantiation */ 
    private CustomHttpClient() { 
    } 

    public static synchronized HttpClient getHttpClient() { 
        if (customHttpClient == null) { 
            HttpParams params = new BasicHttpParams(); 
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
            HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
            HttpProtocolParams.setUseExpectContinue(params, true); 
            HttpProtocolParams.setUserAgent(params,"Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83)" +
                    " AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); 
            ConnManagerParams.setTimeout(params, 1000); 
            HttpConnectionParams.setConnectionTimeout(params, 5000); 
            HttpConnectionParams.setSoTimeout(params, 10000); 
            SchemeRegistry schReg = new SchemeRegistry(); 
            schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
            schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
            ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params,schReg); 
            customHttpClient = new DefaultHttpClient(conMgr, params); 
        } 
        return customHttpClient; 
    } 
    public Object clone() throws CloneNotSupportedException { 
        throw new CloneNotSupportedException(); 
    } 
}

The class works fine except it gives connectTimeOut whenever two IntentService try to Http at the same time or when one connection is in progress and another connection is established.

One obvious improvement in the code is to replace deprecated ThreadSafeClientConnManager with PoolingClientConnectionManager.

Should I synchronize here -> public static synchronized HttpClient getHttpClient()

What error in the Singleton is causing the connectTimeOut?

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • Android does not include the pooled connector if I'm not mistaken. Also what "object" do you mean? Btw http://android-developers.blogspot.de/2011/09/androids-http-clients.html recommends `HttpUrlConnection` now. – zapl Aug 19 '12 at 22:12
  • @zapl I quote from the blog _Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.For Gingerbread and better, HttpURLConnection is the best choice._ – Gaurav Agarwal Aug 19 '12 at 22:23
  • @zapl ThreadSafeClientConnManager in Android since API 1 - http://developer.android.com/reference/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html – Gaurav Agarwal Aug 19 '12 at 22:27
  • [~80% of all devices](http://developer.android.com/about/dashboards/index.html) are Gingerbread or better, so unless you develop exclusively for old phones you could consider switching to the other implementation in case you don't get it to work. I use apache http myself but have not seen timeout issues for parallel connections. Your singleton is correct so Idk what the problem is :( Might even be a misconfigured server. - `PoolingClientConnectionManager` seems to be not part of Android so you may not be able to switch to that. – zapl Aug 19 '12 at 22:38
  • If you leave out the `synchronized` in `public static synchronized HttpClient getHttpClient()` you can end up with multiple instances of your `HttpClient` which should not cause problems besides using more memory. – zapl Aug 19 '12 at 22:43
  • @zapl Thanks. Your comments are close to what an answer should be for this question. `misconfigured server`, can you explain a little more or direct me towards a pointer; what possible misconfiguration on the server-side could lead to `connectTimeOut`? – Gaurav Agarwal Aug 20 '12 at 18:05
  • Don't synchronize just for the singleton effect! use an enum instead (http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) – Eran Medan Dec 07 '12 at 03:28

1 Answers1

4

Synchronize the PoolingClientConnectionManager or not?

Yes and no. If you want getHttpClient() to be a singleton (IMO a good idea) you have to make it thread safe. That requires the synchronized. Without synchronized two thread can enter the if block at the same time (because customHttpClient == null is true for both threads) and create two instances.

But there are better (as in faster) ways without synchronized to create thread-safe singletons. I like the Singleton Holder approach for example.

But regardless of the singleton you use here you should not get connection timeouts. It should also work if you use new HttpClient instances for each thread.


If a connection timeout happens then one of your threads did not manage to connect to the server within the time limit you have set with HttpConnectionParams.setConnectionTimeout(params, 5000); = 5 seconds.

That can have several reasons. For example if you are on a slow & instable connection it may take longer because it can happen that your connection is dead for several seconds. Or if your server can't handle more connections either due to the configuration (e.g. a connection limit per IP) or because the hardware can't handle more you can see this problem too. Basically anything that prevents the HttpClient to send and receive packets to / from the server can trigger that problem. Means your problem is either on the device or in the network or on the server.

I don't know your network setup but you could try to increase the timeout and see if that has any effect. The AndroidHttpClient for example sets those timeouts to 60 seconds. That is much more than required if you are on WiFi and have a stable connection but is good when the connection is really weak.

You could also check if the other settings that AndroidHttpClient applies help.

Setup of AndroidHttpClient below

// Default connection and socket timeout of 60 seconds.  Tweak to taste.
private static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;

// ---------------------------------------------------------------------- //
HttpParams params = new BasicHttpParams();

// Turn off stale checking.  Our connections break all the time anyway,
// and it's not worth it to pay the penalty of checking every time.
HttpConnectionParams.setStaleCheckingEnabled(params, false);

HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
HttpConnectionParams.setSocketBufferSize(params, 8192);

// Don't handle redirects -- return them to the caller.  Our code
// often wants to re-POST after a redirect, which we must do ourselves.
HttpClientParams.setRedirecting(params, false);

// Use a session cache for SSL sockets
SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);

// Set the specified user agent and register standard protocols.
HttpProtocolParams.setUserAgent(params, userAgent);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http",
        PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https",
        SSLCertificateSocketFactory.getHttpSocketFactory(
        SOCKET_OPERATION_TIMEOUT, sessionCache), 443));

ClientConnectionManager manager =
        new ThreadSafeClientConnManager(params, schemeRegistry);

return new DefaultHttpClient(manager, params);

To determine where your problem lies you could have a look into your server connection logs and check if the device tries to initiate a connection and if so if there are reasons why the connection is not established. If you don't see any connection attempts when the timeout happens it is more likely a device or network problem.

If you have access to the network the device is connected to (=WiFi) you could check the network traffic with e.g. Wireshark for reasons the connection attempt times out. You can see there if the device sends a connection request or not.

And last but not least it can be caused by some of your code. If you find no other solution try to implement it with HttpURLConnection and see if that helps.

Community
  • 1
  • 1
zapl
  • 63,179
  • 10
  • 123
  • 154
  • When we create a custom `HttpClient` using Singleton pattern only one object of the class is created. Can this single object create multiple concurrent connections to the server? – Gaurav Agarwal Aug 20 '12 at 21:58
  • More efficient way to singleton in Java: http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – Eran Medan Dec 07 '12 at 03:28