
br.com.anteros.spring.web.support.AsyncRequestInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Anteros-Spring-Web Show documentation
Show all versions of Anteros-Spring-Web Show documentation
Anteros Spring Web for Java.
The newest version!
/*******************************************************************************
* Copyright 2012 Anteros Tecnologia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package br.com.anteros.spring.web.support;
import java.util.concurrent.Callable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
import br.com.anteros.persistence.session.SQLSessionFactory;
import br.com.anteros.spring.transaction.SQLSessionFactoryUtils;
import br.com.anteros.spring.transaction.SQLSessionHolder;
/**
*
* @author Edson Martins [email protected]
*
*/
class AsyncRequestInterceptor extends CallableProcessingInterceptorAdapter
implements DeferredResultProcessingInterceptor {
private static final Log logger = LogFactory.getLog(AsyncRequestInterceptor.class);
private final SQLSessionFactory sessionFactory;
private final SQLSessionHolder sessionHolder;
private volatile boolean timeoutInProgress;
public AsyncRequestInterceptor(SQLSessionFactory sessionFactory, SQLSessionHolder sessionHolder) {
this.sessionFactory = sessionFactory;
this.sessionHolder = sessionHolder;
}
@Override
public void preProcess(NativeWebRequest request, Callable task) {
bindSession();
}
public void bindSession() {
this.timeoutInProgress = false;
TransactionSynchronizationManager.bindResource(this.sessionFactory, this.sessionHolder);
}
@Override
public void postProcess(NativeWebRequest request, Callable task, Object concurrentResult) {
TransactionSynchronizationManager.unbindResource(this.sessionFactory);
}
@Override
public Object handleTimeout(NativeWebRequest request, Callable task) {
this.timeoutInProgress = true;
return RESULT_NONE;
}
@Override
public void afterCompletion(NativeWebRequest request, Callable task) throws Exception {
closeAfterTimeout();
}
private void closeAfterTimeout() {
if (this.timeoutInProgress) {
logger.debug("Closing Anteros SQLSession after async request timeout");
SQLSessionFactoryUtils.closeSession(sessionHolder.getSession());
}
}
public void beforeConcurrentHandling(NativeWebRequest request, DeferredResult deferredResult) {}
public void preProcess(NativeWebRequest request, DeferredResult deferredResult) {}
public void postProcess(NativeWebRequest request, DeferredResult deferredResult, Object result) {}
@Override
public boolean handleTimeout(NativeWebRequest request, DeferredResult deferredResult) {
this.timeoutInProgress = true;
return true;
}
@Override
public void afterCompletion(NativeWebRequest request, DeferredResult deferredResult) {
closeAfterTimeout();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy