全局设置CDI会话超时

sta*_*sal 6 jsf weblogic cdi weld

注入@Named bean的所有会话对象是否可以全局设置会话超时?

我有几个@ConversationScoped bean,例如:

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();
        }
    }
}        
Run Code Online (Sandbox Code Playgroud)

这些对话的默认超时为600000毫秒.我想知道是否有任何方法可以在全局设置会话的超时,或者我需要在每个bean中设置它

if (!conversation.isTrainsient()) {
    conversation.setTimeout(MY_CUSTOM_TIMEOUT);
}
Run Code Online (Sandbox Code Playgroud)

(问题是有很多CDI bean并且每个人手动设置超时都不是最佳解决方案)

sta*_*sal 2

因此,这是我使用的解决方案(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) {}
}
Run Code Online (Sandbox Code Playgroud)

上下文被注入到侦听器中,并在用户启动会话时设置超时。