jetty-如何序列化会话
jetty中文文档:www.jettycn.com如何序列化会话(Session)
Jetty
>> howto
>> 如何序列化会话(Session)
介绍
在重启Jetty后维护现有的会话有些时候是很有用的。HashSessionManager
支持此功能。如果启用了持久化特性,HashSessionManager将在关闭前保存所有现存的有效的会话到磁盘。在重新启动时,Jetty将恢复保存的会话。
启用持久化特性
SessionManager的作用正如其名称所示,它代表web应用程序管理会话的生命周期和状态。每个web应用程序必须有自己唯一的SessionManager实例。启用持久化特性和把HashSessionManager配置为web应用的SessionManager一样简单,只需告诉HashSessionManager在磁盘的那个位置存储会话:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
.
.
.
<Set name="sessionHandler">
<New class="org.eclipse.jetty.servlet.SessionHandler">
<Arg>
<New class="org.eclipse.jetty.servlet.HashSessionManager">
<Set name="storeDirectory">your/chosen/directory/goes/here</Set>
</New>
</Arg>
</New>
</Set>
.
.
.
</Configure>
提示
如果你想为多个web应用持久化会话:
[*]为每个web应用配置单独的HashSessionManager。
[*]为每个web应用指定不同的"storeDirectory"。
上面的例子使用了一个适用于org.eclipse.jetty.deploy.ContextDeployer
的配置文件,因此,你可能想看看Context
Deployer
的功能指南。
延迟会话加载
你可能需要确保在servlet环境启动后才加载会话(默认情况下,Jetty急切的加载会话,这是做为容器启动的一部分的,但在此之前,它会初始化servlet环境)。例如,Wicket
web框架要求servlet环境在会话被激活时可用。
设置SessionManager.setLazyLoad(true),Jetty将延迟加载会话,当它接收到要求会话的第一个请求时,或者会话回收线程第一次运行时,才会触发会话加载,以先发生的事件为准。下面是xml配置示例:
<Set name="sessionHandler">
<New class="org.eclipse.jetty.servlet.SessionHandler">
<Arg>
<New class="org.eclipse.jetty.servlet.HashSessionManager">
<Set name="lazyLoad">true</Set>
</New>
</Arg>
</New>
</Set>
为Maven Jetty插件启用会话持久性
要为Maven Jetty插件启用会话持久性,像下面这样在<configuration>部分配置HashSessionManager:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.6</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<webAppConfig implementation="org.eclipse.jetty.plugin.Jetty6PluginWebAppContext">
<contextPath>/foo</contextPath>
.
.
.
<sessionHandler implementation="org.eclipse.jetty.servlet.SessionHandler">
<sessionManager implementation="org.eclipse.jetty.servlet.HashSessionManager">
<storeDirectory>${basedir}/target/your/sessions/go/here</storeDirectory>
</sessionManager>
</sessionHandler>
.
.
.
</webAppConfig>
</configuration>
</plugin>
其他资源
有关详细信息,请参阅基于数据库的Session集群配置教程
教程。
页:
[1]