4 Replies Latest reply on Sep 23, 2011 1:05 AM by John Okrasa

    org.jboss.weld.context.NonexistentConversationException: WELD-000321 No conversation found to restore for id 1

    John Okrasa Newbie

      Hello I'm trying to find a way to catch and redirect back to the login (restore a converstion or start a new one)

      in web.xml I have...




      so this will throw the

      org.jboss.weld.context.NonexistentConversationException: WELD-000321 No conversation found to restore for id 1  error

      I try to handle it...

      in faces-config I have...


      public class MyExceptionHandlerFactory extends ExceptionHandlerFactory {
        private ExceptionHandlerFactory parent;
        // this injection handles jsf
        public MyExceptionHandlerFactory(ExceptionHandlerFactory parent) {
          this.parent = parent;
        //create your own ExceptionHandler
        public ExceptionHandler getExceptionHandler() {
          ExceptionHandler result =
              new MyExceptionHandler(parent.getExceptionHandler());
          return result;
      public class MyExceptionHandler extends ExceptionHandlerWrapper {
        //private static Log log = LogFactory.getLog(MyExceptionHandler.class);
        private static final Logger LOG = LoggerFactory.getLogger(MyExceptionHandler.class);
        private ExceptionHandler wrapped;
        public MyExceptionHandler(ExceptionHandler wrapped) {
          this.wrapped = wrapped;
        public ExceptionHandler getWrapped() {
          return wrapped;
        public void handle() throws FacesException {
          //Iterate over all unhandeled exceptions
          Iterator i = getUnhandledExceptionQueuedEvents().iterator();
          while (i.hasNext()) {
            ExceptionQueuedEvent event = (ExceptionQueuedEvent)i.next();
            ExceptionQueuedEventContext context =
            //obtain throwable object
            Throwable t = context.getException();
            //here you do what ever you want with exception
            //log error
              LOG.error("Serious error happened!", t);
              //redirect to error view etc....
              //after exception is handeled, remove it from queue
          //let the parent handle the rest
      public class ExceptionHandlerUtil {
          private static final Logger LOG = LoggerFactory.getLogger(MyExceptionHandler.class);
          private static BeanManager mgr = null;
          private ExceptionHandlerUtil() {
          protected static void handleNavigationWithError(String errorNumber) {
              handleNavigationWithError(errorNumber, null, null);
          private static BeanManager getBeanManager() {
              if (mgr == null) {
                  ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
                  mgr = (BeanManager) ctx.getAttribute(BeanManager.class.getName());
              return mgr;
          protected static void handleNavigationWithError(String errorNumber, String parameterName, Object parameterValue) {
              FacesContext fc = FacesContext.getCurrentInstance();
              //check for conversation context - this doesn't work...
      /*        try {
                  BeanManager bm = getBeanManager();
                  //(BeanManager)new InitialContext().lookup("java:comp/BeanManager");
                  if (bm.isScope(ConversationScoped.class)) {
                      Set<Bean<?>> conversations =  bm.getBeans(Conversation.class);
                      //for (javax.enterprise.inject.spi.Bean b : conversations) {
      //                for(org.jboss.weld.bean.builtin.ConversationBean b : conversations){
      //                     Conversation c = (Conversation) b;
      //                     if (!c.isTransient()) {
      //                        c.end();
      //                    }
      //                }
              } catch (Exception ex) {
      //        ConversationContext conversationContext = fc.getExternalContext().instance()
      //                .deploymentManager().instance().select(Context.class)
      //                .select(HttpConversationContext.class).get();
      //        conversationContext.activate(null);
              Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
              NavigationHandler nav = fc.getApplication().getNavigationHandler();
              if (parameterName != null) {
                  requestMap.put(parameterName, parameterValue);
              fc.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, errorNumber, null));
                  nav.handleNavigation(fc, null, "go_to_login");
               catch (Exception e) {
      //           FacesContext ctx = FacesContext.getCurrentInstance();
      //           ExceptionQueuedEventContext eventContext = new ExceptionQueuedEventContext(ctx, e);
      //           eventContext.getAttributes().put("key", "org.jboss.weld.context.NonexistentConversationException");
      //           ctx.getApplication().publishEvent(ctx, ExceptionQueuedEvent.class, eventContext);
      /*              HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
                      response.addHeader("Pragma", "no-cache");
                      response.addHeader("Cache-Control", "no-cache");
                      // Stronger according to blog comment below that references HTTP spec
                      response.addHeader("Cache-Control", "no-store");
                      response.addHeader("Cache-Control", "must-revalidate");
                      // some date in the past
                      response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");
                   response.sendRedirect("/login/login.xhtml");       //jsf?faces-redirect=true");
                  catch(Exception ex){

      Surely there must be something simple to catch/handle a session timeout or lost conversation?

      I am using GlassFish 3.1.1 all updated...

      the session timeout is intentional but I want to redirect back to the login/landing...

      Any help/suggestions greatly appreciated..

      Best Regards,