catch dead session in spring webflow

catch dead session in spring webflow  using -'java,spring,session,exception-handling,spring-webflow'

I want to catch the exception thrown when I loose my session, not only because a session timeout (for reports for example).
Also I want that this handler will handle only that specific exception and will not be a global exceptions handler or any thing like this.
Basically, I want to catch exception org.springframework.web.HttpSessionRequiredException.

asked Oct 11, 2015 by ukohale
0 votes

1 Answer

0 votes

With any of the proposed solutions below you should be able to handle the exceptions and perform logic on them.

Possible Solutions:

1. You can add a FlowExecutionListenerAdapter that will listen to all exceptions thrown and then you can filter by instanceof.

import org.springframework.webflow.execution.FlowExecutionListenerAdapter;
import org.springframework.binding.mapping.MappingResult;
import org.springframework.webflow.engine.FlowAttributeMappingException;
import org.springframework.webflow.execution.ActionExecutionException;
import org.springframework.webflow.execution.FlowExecutionException;
import org.springframework.webflow.execution.RequestContext;

public class LoggingExceptionFlowExecutionListenerAdapter extends FlowExecutionListenerAdapter {

    public void exceptionThrown(RequestContext context, FlowExecutionException exception) {
         if (exception instanceof HttpSessionRequiredException) {
                // do something
         } else if(exception instanceof FlowAttributeMappingException) {
                // do something else 

and you need to add it in your webflow executor config:


2. Another solution is to implement your own FlowExecutionExceptionHandler. Although it will be a global exception handler The method

public void handle(FlowExecutionException exception,
            RequestControlContext context) { } 

will allow you to have access to the context which will allow you to filter on many different variables. See also, How implement the interface FlowExecutionExceptionHandler

3. Another possible solution if you are using Spring Security project I believe Spring security will handle the exception for you automatically if you have the follow config:


and if you want to catch the exception and perform logic on it see the following answer: Logout/Session timeout catching with spring security

I would recommend the 3rd solution as it is the least intrusive. In the 3rd solution's link there are several really elegent solutions to handling sessions.

answered Oct 11, 2015 by badhwar.rohit