Jea*_*ana 10 java tomcat fitnesse
我想在Tomcat中使所有会话失效.我们在Fitnesse下测试我们的产品,并且一些会话仍然存在,并且会话结束导致测试之间的依赖性.我使用以下代码手动执行,但仍保留一些会话(我可以使用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);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有剩余会话的情况下更高效,更直接地做到这一点?
我假设您的应用程序确实是独立的上下文.我已经做了一些类似于你要求的HttpSessionListener用于每个上下文的东西.这里棘手的部分是你需要一个由根类加载器加载的Session Collection,而不是上下文类加载器.这就是我记忆中的方式:
创建一个包含每个上下文的活动会话的类.此类必须驻留在tomcat/lib目录中,以便每个上下文都可以访问它.它不能成为任何环境的一部分.
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();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
为每个上下文创建一个监听器.
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());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在相应的上下文中注册每个监听器web.xml.
<listener>
<listener-class>ApplicationContextListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
然后,您可以调用以下行以使任何上下文中的所有内容无效.
SessionMap.invalidate();
Run Code Online (Sandbox Code Playgroud)
我在地图上同步只是为了安全起见.
| 归档时间: |
|
| 查看次数: |
11393 次 |
| 最近记录: |