Set Conversation timeout on CDI Beans

Conversation scope is a nice feature when you want to save values over request on different pages as long as you still interact with the same bean. Basically is a session scope where you have to start the conversation explicitly with a default timeout (unless you finish the conversation manually).

Default CDI Conversation scope timeout is 30 minutes. This means that, if you stop interacting with the GUI and there’s no requests to server side components, server saves conversation data during 30 minutes until it destroys it. That’s a lot of time.

You can define conversation timeout by using setTimeout() method of Conversation object by defining time in milliseconds. In the next example the timeout is set to 20 seconds. You can test timeout by pressing the button a couple of times to increase counter, then wait 20 seconds without doing anything and then pressing again a couple of times.

The environment that i’m using is:

  • Windows 8.1
  • JBoss 7.1.1
  • JDK 1.7.65
  • Eclipse Kepler X64 with JBoss Tools Plugin

And using the next dependencies:

  • MyFaces 2.2.5
  • Primefaces 5.0
  • Weld (included on JBoss 7.1.1)

We have a CDI Bean named ConversationTimeoutManagedBean which contain a @PostConstruct method (it executes after CDI creates the bean) where it sets conversation timeout for injected Conversation object:

As you can see, initConversation() evaluates if conversation is transient, if it is, it begins it. So, the first time that you press the button it will begin conversation.

Then, index.xhtml let us to increase count property value each time we press the button, and it submits the form which refresh the labels where it shows count value.

If you test it, you can press the button a couple of times, then wait 20 seconds, and after that when you press the button again, if you use Chrome DevTools or some tool to see action’s response you will see this:

As you can see, you get NonexistentConversationException. If you change ajax attribute of commandButton to false then you will see the error in your page.

You can download this Eclipse project here: Conversation Timeout example

  • Emerald Hieu

    Great article. I search many pages just for what called “30 minutes timeout”. I think you should write more about begin() and end() conversation. I still get confused with that.