Search code examples
java-metcppersistentgprshttpconnection

J2ME Persistent HttpConnection


Sorry, this is along one to document..

OLD SETUP

I have a J2ME client app which connects to a server at 3 second intervals. The connections are handled in a separate thread. The thread runs in a while loop and does as follows:

  1. Open HTTP Connection
  2. Receive Response & send to Main UI Thread for processing
  3. Sleep for 3 seconds
    Repeat

Server is Apache and KeepAlive is off. HTTP Requests from the client have a Timeout of 10 seconds. Each loop takes about 4.5 seconds (1.5 seconds total latency).

Every so often (once every 1 or 2 hours), under low coverage (poor 3G/GPRS signal) the thread receives an IO block and hangs at step 1 above. I think what is happening is the connection somewhere along the line has died, but the app does not know it and is waiting for a response. The network (O2 in this case) is to blame I think. The connection stays alive for maybe 30 minutes and eventually dies and returns -1 response length to the app and the thread eventually continues. To combat this relatively rare issue I simply abandoned the thread if the response took longer than 60 seconds, and created a new one.

Up until recently this was not a major issue, however we changed our network setup as follows:


NEW SETUP

  1. Due to lower bandwidth restrictions we slowed down the interval to 5 seconds.
  2. We now route all connections through an SSL Tunnel to our server.
  3. HTTP requests from the client still have a timeout of 10 seconds on App*

The loop was taking about 7.5 seconds (2.5 seconds total latency) to complete with these changes. The reason the latency increased was the handshake over SSL was required for each single connection (due to KeepAlive off). We were advised to switch KeepAlive ON on the apache server which would mean one handshake and then a persistent http connection for subsequent requests. We did this, and for the most part it has considerably improved the connection speed. The loop is down to 6.5 seconds.

So we can now add this to our config changes for the new setup:

  1. HTTP KeepAlive was switched to ON on the server (to enable to the persistent TCP connection)

However, in poor 3G/GPRS areas, the thread blocking issue is much more frequent and has become a major issue - it happens as much as 50% of the time in really poor coverage areas. I have received a Java out of memory exception a few times telling me the app could not fork a new thread.. In addition, in good 3G areas we also see this thread blocking issue now which had not happened before.

I have clearly done something wrong in this setup, as persistent http connections and the underlying TCP is quite reliable

*We managed all these changes without an app re-write, but the http timeout of 10 seconds will require an app re-write. Perhaps this timeout is causing the issue?

Thanks in advance for any assistance in this.


Solution

  • In case someone has a similar issue, KeepAliveTimeout was key here. It was set at 5 seconds rendering KeepAlive useless.

    I've increased it to 30 seconds which effectively creates a persistent connection and the app is flying along now.