下面是一个扩展ExecuteAndWaitInterceptor的例子.
这段代码的目的是允许在后台执行的同时可以访问到相同的打开的Hibernate Session对象
SessionFactory的依赖是通过Spring注射到OpenSessionExecuteAndWaitInterceptor里面的. 你也可以使用其它的依赖注入方法, 如果更适合的话. 通过覆写getNewBackgroundProcess()方法, 这个拦截器使用自定义OpenSessionBackgroundProcess而不是WebWork默认的.
覆写了OpenSessionBackgroundProcess中的beforeInvocation()和afterInvocation()方法来确保在后台处理过程中session一直处于打开状态, 任何Spring的事务管理都是可用的.
这个代码依赖于Spring和Hibernate, 所以你不可能在Webwork的发布包中看见它. 尽管如此, 这是个扩展misc:Execute and Wait Interceptor
的一个很有用的例子.
import net.sf.hibernate.SessionFactory;
import com.opensymphony.webwork.interceptor.BackgroundProcess;
import com.opensymphony.webwork.interceptor.ExecuteAndWaitInterceptor;
import com.opensymphony.xwork.ActionInvocation;
/**
* The OpenSessionExecuteAndWaitInterceptor will obtain a Hibernate
* Session Factory from a Spring.
*
* The session factory will then be passed to the BackgroundProcess,
* to open a session, enable Spring's transaction management
* capabilities, and bind the Session to the background thread.
*
*/
public class OpenSessionExecuteAndWaitInterceptor extends ExecuteAndWaitInterceptor {
SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {
return new OpenSessionBackgroundProcess(arg0, arg1, arg2, sessionFactory);
}
}
import net.sf.hibernate.FlushMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import org.springframework.orm.hibernate.SessionFactoryUtils;
import org.springframework.orm.hibernate.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.opensymphony.webwork.interceptor.BackgroundProcess;
import com.opensymphony.xwork.ActionInvocation;
/**
* The OpenSessionBackgroundProcess, when instantiated with a
* HibernateSessionFactory, will open a session, enable Spring's transaction
* management capabilities, and bind the Session to the background thread.
*
*/
public class OpenSessionBackgroundProcess extends BackgroundProcess {
SessionFactory sessionFactory;
Session openSession;
public OpenSessionBackgroundProcess(String name,
ActionInvocation invocation, int threadPriority,
SessionFactory factory) {
super(name, invocation, threadPriority);
this.sessionFactory = factory;
}
protected void beforeInvocation() throws Exception {
openSession = SessionFactoryUtils.getSession(sessionFactory, true);
openSession.setFlushMode(FlushMode.NEVER);
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(openSession));
super.beforeInvocation();
}
protected void afterInvocation() throws Exception {
super.afterInvocation();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils
.closeSessionIfNecessary(openSession, sessionFactory);
}
}