如何使Tomcat中的所有会话失效? [英] How can I expire all my sessions in Tomcat?
问题描述
我想在Tomcat中使我的所有会话失效。我们在Fitnesse下测试我们的产品,并且一些会话仍然存在,并且会话结束导致测试之间的依赖性。我使用以下代码手动完成,但仍保留一些会话(我可以使用 http://进行检查) localhost:8080 / manager / html / list url)
I would like to expire all my sessions in Tomcat. We test our product under Fitnesse and some sessions remain and the end of the session causes dependency between the tests. I do it by hand with the following code, but some sessions remain (I can check it with the http://localhost:8080/manager/html/list url)
public static void expireAllSessions() {
String[] applications = { "a", "b", "c", "d", "e" };
for (String application : applications) {
try {
expireAllSessions(application);
} catch (Exception e) {
logger.error(e);
}
}
}
private static void expireAllSessions(final String application) throws Exception {
// cf doc http://hc.apache.org/httpclient-3.x/authentication.html
HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
Credentials userPassword = new UsernamePasswordCredentials("tomcat", "tomcat");
client.getState().setCredentials(AuthScope.ANY, userPassword);
String url = "http://localhost:8080/manager/html/expire";
NameValuePair[] parametres = new NameValuePair[] {
new NameValuePair("path", "/" + application),
new NameValuePair("idle", "0")
};
HttpMethod method = new GetMethod(url);
method.setQueryString(parametres);
client.executeMethod(method);
}
有没有办法在没有剩余会话的情况下更高效,更直接地做到这一点?
Is there a way to do it more efficiently and immediate with no remaining session ?
推荐答案
我假设您的应用程序确实是独立的上下文。我已经为每个上下文使用 HttpSessionListener
做了类似的事情。这里棘手的部分是你需要一个由根类加载器加载的Session Collection,而不是上下文类加载器。这就是我记忆中的方法:
I'm assuming your applications are really independent contexts. I have done something similar to what you are asking using an HttpSessionListener
for every context. The tricky part here is that you need to have a Session Collection that is loaded by the root class loader as opposed to the context class loader. This is how I remember it:
创建一个包含每个上下文的活动会话的类。此类必须驻留在tomcat / lib目录中,以便每个上下文都可以访问它。它不能是任何上下文的一部分。
Create a class which holds the active sessions for each context. This class must reside in the tomcat /lib directory so that it becomes accessible by every context. It can't be part of any of the contexts.
public class SessionMap {
private static Map<ServletContext, Set<HttpSession>> map =
new HashMap<ServletContext, Set<HttpSession>>();
private SessionMap() {
}
public static Map<ServletContext, Set<HttpSession>> getInstance() {
return map;
}
public static void invalidate(String[] contexts) {
synchronized (map) {
List<String> l = Arrays.asList(contexts);
for (Map.Entry<ServletContext, Set<HttpSession>> e : map.entrySet()) {
// context name without the leading slash
String c = e.getKey().getContextPath().substring(1);
if (l.contains(c)) {
for (HttpSession s : e.getValue())
s.invalidate();
}
}
}
}
}
为每个上下文创建一个监听器。
Create a listener for every context.
public class ApplicationContextListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
ConcurrentMap<ServletContext, Set<HttpSession>> instance = SessionMap.getInstance();
synchronized (instance) {
ServletContext c = event.getSession().getServletContext();
Set<HttpSession> set = instance.get(c);
if (c == null) {
set = new HashSet<HttpSession>();
instance.put(c, set);
}
set.add(event.getSession());
}
}
public void sessionDestroyed(HttpSessionEvent event) {
ConcurrentMap<ServletContext, Set<HttpSession>> instance = SessionMap.getInstance();
synchronized (map) {
ServletContext c = event.getSession().getServletContext();
Set<HttpSession> set = instance.get(c);
if (c != null) {
set.remove(event.getSession());
}
}
}
}
在相应的上下文 web.xml
中注册每个侦听器。
Registered each listener in the corresponding context's web.xml
.
<listener>
<listener-class>ApplicationContextListener</listener-class>
</listener>
然后,您可以调用以下行以使任何上下文中的所有内容无效。
You can then call the following line to invalidate everything from any context.
SessionMap.invalidate();
我在地图上同步只是为了安全起见。
I'm synchronizing on the map just to be on the safe side.
这篇关于如何使Tomcat中的所有会话失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!