1

Searching memcached java in google, the first result is Using Memcached with Java.

The guy (who calls himself Just some Random Asshole in the Internet!) proposes a Singleton based on net.spy.memcached. It basically creates 20 threads and connections by creating 20 instances of MemcachedClient. For every request it chooses one at random.

However those threads and connections are never closed and they pile up every time I hot swap the application during development (with warnings from Tomcat 7).

SEVERE: The web application [/MyAppName] appears to have started a thread named
[...] but has failed to stop it. This is very likely to create a memory leak.

By looking at MemcachedClient JavaDoc, I see a method called shutdown with the only description being "Shut down immediately." Shut down what? The client? The server? I suppose is the client, since it's in MemcachedClient and I suppose that this method would close the connection and terminate the thread. EDIT: yes, it shuts down the client.

Question 1 How to force the execution of cleanup code in Tomcat 7, before the application is hot swapped?

Question 2 Is this approach of using memcached (with cleanup code), correct or is better I start over in a different way?

stivlo
  • 83,644
  • 31
  • 142
  • 199

3 Answers3

2

I think creating 20 memcache clients is silly - that's like creating 20 separate copies of your DB connection pool. The idea with that client is that it multiplexes a variety of requests with asynch IO. http://code.google.com/p/spymemcached/wiki/Optimizations

As far as shutting it down, simply call: yourClient.shutdown() to shutdown immediately, or yourClient.shutdown(3, TimeUnit.SECONDS) for example, to allow some time for a more graceful shutdown.

That could be called from your Servlet's .destroy method, or a context listener for your whole WAR.

pdxleif
  • 1,750
  • 16
  • 15
  • Oh, and that yourClient.shutdown() only shuts down your spymemcached client (so it can cleaup / stop its background thread) - the server is unaffected. – pdxleif Jul 30 '12 at 23:24
1

I don't know anything about memcached, but you could probably write a custom context listener and put some kind of shutdown hook in the context listener so that when the context shutdown you could loop through the items in your singleton and shut them down.

ScArcher2
  • 85,501
  • 44
  • 121
  • 160
  • OK I found a question explaining it http://stackoverflow.com/questions/1549924/shutdown-hook-for-java-web-application - now I try. Thank you. – stivlo May 18 '11 at 17:59
  • I implemented it and it works, seems like a good idea. I heard about shutdown hooks, but never tried before. +1 because it was a good advice, but I keep the question open. Now I hope someone will answer if this whole approach together with this fix is a good way to use memcached or there are better ways. – stivlo May 18 '11 at 18:10
0

It turned out that it was a bug of Java AWS SDK and was not related to memcached. Version 1.2.2 of Java AWS SDK has this bug fixed.

stivlo
  • 83,644
  • 31
  • 142
  • 199