Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow shutdown of executors, to clean up resources. #62

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/net/jodah/expiringmap/ExpiringMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -1388,4 +1388,19 @@ private void initListenerService() {
}
}
}

/**
* Free up {@link Executors} used by <code>ExpiringMap</code> to manage expiration and listeners.
* This will stop the expiration process for all running instances of this class, but it is useful before unloading this class from the classloader in order not to leave classes running (e.g. in a {@link javax.servlet.ServletContextListener#contextDestroyed(ServletContextEvent)}).
*/
public synchronized static void shutdown() {
Copy link
Owner

@jhalterman jhalterman Apr 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some javadoc here indicating what this method does? It should mention when and why you might want to use it, and that it affects all ExpiringMap instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will add it as soon as I'm back in the office.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(sorry, I was on other projects and time has flown by)

if (EXPIRER != null) {
EXPIRER.shutdownNow();
EXPIRER = null;
}
if (LISTENER_SERVICE != null) {
LISTENER_SERVICE.shutdownNow();
LISTENER_SERVICE = null;
}
}
}
49 changes: 49 additions & 0 deletions src/test/java/net/jodah/expiringmap/ShutdownTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.jodah.expiringmap;

import net.jodah.expiringmap.internal.NamedThreadFactory;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertEquals;

/**
* Tests ExpiringMap shutdown procedure.
*/
@Test
public class ShutdownTest {
private static final String BASE_NAME = "TestThread-";

private static int expirationThreads() {
int n = 0;
for (Thread t : Thread.getAllStackTraces().keySet())
if (t.getName().startsWith(BASE_NAME))
++n;
return n;
}

/**
* Ensures that the service thread are properly shutdown.
*/
public void shouldCloseThreads() throws Exception {
// Initial cleanup
ExpiringMap.setThreadFactory(new NamedThreadFactory(BASE_NAME + "%s"));
ExpiringMap.shutdown();

// Given
ExpiringMap<String, String> map = ExpiringMap.builder().expiration(100, TimeUnit.MILLISECONDS).build();

// When
for (int i = 0; i < 100; i++)
map.put("John" + i, "Joe");

// Then
assertEquals(expirationThreads(), 1);
ExpiringMap.shutdown();
Thread.sleep(100);
assertEquals(expirationThreads(), 0);

// Final cleanup
ExpiringMap.THREAD_FACTORY = null;
}
}