Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting CDI conversation timeout globally

Is it possible to set conversation timeout globally for all conversation objects injected into @Named beans?

I have several @ConversationScoped beans, e.g.:

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class SomeBean1 {

    @Inject
    private Conversation conversation;

    @PostConstruct
    private void init() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
}

@Named
@ConversationScoped
public class SomeBean2 {

    @Inject
    private Conversation conversation;

    @PostConstruct
    private void init() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
}        

The default timeout for these conversations is 600000 ms. I want to know if there is any way to set conversations' timeouts globally or I need to set it in each bean by

if (!conversation.isTrainsient()) {
    conversation.setTimeout(MY_CUSTOM_TIMEOUT);
}

(the problem is that there is a lot of CDI beans and setting timeout manually in each of them is not the best solution)

like image 974
stasal Avatar asked Oct 01 '13 06:10

stasal


1 Answers

So, here is the solution I used (Oracle WebLogic 12c, WELD 1.1.Final):

import org.jboss.weld.context.http.HttpConversationContext;

import javax.inject.Inject;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class SessionListener implements HttpSessionListener {

    @Inject
    private HttpConversationContext conversationContext;

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        if (conversationContext != null) {
            final long DEFAULT_TIMEOUT = 2 * 60 * 60 * 1000;
            if (conversationContext.getDefaultTimeout() < DEFAULT_TIMEOUT){
                conversationContext.setDefaultTimeout(DEFAULT_TIMEOUT);
            }
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {}
}

Context is injected into the listener and the timeout is set when user starts the session.

like image 148
stasal Avatar answered Sep 21 '22 02:09

stasal