Question

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)

Was it helpful?

Solution

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.

OTHER TIPS

With Seam/Weld you should be able to do something like the following:

@Inject
private HttpConversationContext conversationContext;

public void observePreload(@Observes PreloadCompleteEvent event) {
    //Set global conversation timout to 60000 ms
    conversationContext.setDefaultTimeout(60000);
}

Otherwise I believe you will have to set it for each conversation.

EDIT: Note I used a custom event, the same can be accomplished with:

public void observePreload(@Observes @Started WebApplication webapp) {

This can easily be done in a portable manner with CDI 1.1.

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.servlet.ServletRequest;

public class ConversationTimeoutDefaultSetter {

    @Inject
    private Conversation conversation;

    public void conversationInitialized(
            @Observes @Initialized(ConversationScoped.class)
            ServletRequest payload) {
        conversation.setTimeout(1800000L); // 30 minutes
    }

}

Another portable option would be to decorate conversation. (Warning: untested.)

import static javax.interceptor.Interceptor.Priority.APPLICATION;

import javax.annotation.Priority;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.context.Conversation;
import javax.inject.Inject;

@Decorator
@Priority(APPLICATION)
public class ConversationTimeoutDefaultSetter implements Conversation, Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    @Delegate
    private Conversation delegate;

    private void setDefaultTimeout() {
        delegate.setTimeout(1800000L); // 30 minutes
    }

    @Override
    public void begin() {
        setDefaultTimeout();
        delegate.begin();
    }

    @Override
    public void begin(String id) {
        setDefaultTimeout();
        delegate.begin(id);
    }

    @Override
    public void end() {
        delegate.end();
    }

    @Override
    public String getId() {
        return delegate.getId();
    }

    @Override
    public long getTimeout() {
        return delegate.getTimeout();
    }

    @Override
    public void setTimeout(long milliseconds) {
        delegate.setTimeout(milliseconds);
    }

    @Override
    public boolean isTransient() {
        return delegate.isTransient();
    }

}

You can also synchronize the timeout of your conversation bean with the session timeout of your current http request:

if (conversation.isTransient()) {
        conversation.setTimeout(((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext()
                .getRequest()).getSession().getMaxInactiveInterval()*1000);
        conversation.begin();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top