Extending Smack XMPPConnection API to allow for complete resource reclaimation


I’m using Tomcat 6.0.26, and it is complaining that the keep alive thread is hanging around. This is true, the thread will last up to 15 seconds. It isn’t a huge deal, but it is causing me issues while tracking down other PermGen problems.

Ultimately, I’d like to add a way to wait block until the keep alive thread dies. I’m not exactly sure how to handle this situation cleanly. I can extend the API to allow a boolean to be passed in. This would likely imply going from 2 shutdown implementations to 4, and the two new ones would require throwing InterruptedException (or eating it, which I’d prefer not to do). I also have to figure out if that is only part of XMPPConnection or if that interface should be extended to the Connection classes also.

So my next thought was to add a “XMPPConnection.waitForKeepAliveJoin()”, so after shutting down, it would invoke the same named method on PacketWriter, which would do exactly what you think it would. The problem there is that after shutting down, the PacketWriter in XMPPConnection is nulled out. I’d prefer not to add more complexity to the shutdown code, but I’m not sure how to do this cleanly in a way that would be accepted as a patch.

I also found a bug that would could cause live lock due to how the Java Memory Model works. The “done” field must be declared as volatile, and it’d likely be best if after setting done to false, the keepAliveThread.interrupt() were invoked to bypass whatever is left of the wait time.

I’ve attached the patch of what I have so far. It has an NPE bug due to the writer being set to null. Any ideas on how to progress? It would greatly ease integration into Tomcat if there were a way to ensure all the resources are released prior to allowing the shutdown to happen.

(This patch applies cleanly to the 3.1.0 codebase, but should also apply to trunk).

Finally, it would be much appreciated if the 3.1.0 source code were uploaded into the public maven repo. It would make debugging quite a bit easier.
patch.txt.zip (934 Bytes)