Skip to content

Commit

Permalink
Conversation stuff
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1538 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Feb 16, 2009
1 parent b4dc309 commit 871d648
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 59 deletions.
Expand Up @@ -25,6 +25,9 @@
import javax.inject.manager.Bean;

import org.jboss.webbeans.context.beanmap.BeanMap;
import org.jboss.webbeans.jsf.JSFHelper;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;

/**
* Base for the Context implementations. Delegates calls to the abstract
Expand All @@ -39,6 +42,8 @@
*/
public abstract class AbstractBeanMapContext extends AbstractContext
{
private static LogProvider log = Logging.getLogProvider(AbstractBeanMapContext.class);

/**
* Constructor
*
Expand Down Expand Up @@ -98,6 +103,7 @@ public <T> T get(Contextual<T> contextual)
*/
private <T> void destroy(Contextual<T> bean)
{
log.trace("Destroying " + bean);
bean.destroy(getBeanMap().get(bean));
}

Expand All @@ -106,6 +112,7 @@ private <T> void destroy(Contextual<T> bean)
*/
public void destroy()
{
log.trace("Destroying context");
for (Contextual<? extends Object> bean : getBeanMap().keySet())
{
destroy(bean);
Expand Down
Expand Up @@ -39,7 +39,7 @@
public abstract class AbstractBeanMap implements BeanMap
{
// The log provider
private static LogProvider log = Logging.getLogProvider(ApplicationBeanMap.class);
private static LogProvider log = Logging.getLogProvider(AbstractBeanMap.class);

/**
* Gets a bean from the map
Expand Down Expand Up @@ -108,8 +108,10 @@ public Iterable<Contextual<? extends Object>> keySet()
private List<String> getFilteredAttributeNames()
{
List<String> attributeNames = new ArrayList<String>();
for (String attributeName : new EnumerationIterable<String>(getAttributeNames()))
Enumeration<String> e = getAttributeNames();
while (e.hasMoreElements())
{
String attributeName = e.nextElement();
if (attributeName.startsWith(getKeyPrefix()))
{
attributeNames.add(attributeName);
Expand Down Expand Up @@ -148,7 +150,8 @@ public <T> void put(Contextual<? extends T> bean, T instance)
protected abstract void removeAttribute(String key);

/**
* Gets an enumeration of the attribute names present in the underlying storage
* Gets an enumeration of the attribute names present in the underlying
* storage
*
* @return The attribute names
*/
Expand Down
Expand Up @@ -103,6 +103,7 @@ public void destroy(HttpSession session)
ConversationContext terminationContext = new ConversationContext();
terminationContext.setBeanMap(new ConversationBeanMap(session, cid));
terminationContext.destroy();
log.trace("Conversation " + cid + " destroyed");
}

/**
Expand Down Expand Up @@ -133,7 +134,7 @@ public boolean unlock()
{
if (concurrencyLock.isHeldByCurrentThread())
{
log.debug("Unlocked conversation " + cid);
log.trace("Unlocked conversation " + cid);
concurrencyLock.unlock();
}
else
Expand Down
Expand Up @@ -24,6 +24,8 @@

import org.jboss.webbeans.WebBean;
import org.jboss.webbeans.conversation.bindings.ConversationInactivityTimeout;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;

/**
* The current conversation implementation
Expand All @@ -36,6 +38,9 @@
@WebBean
public class ConversationImpl implements Conversation
{

private static LogProvider log = Logging.getLogProvider(ConversationImpl.class);

// The conversation ID
private String cid;
// Is the conversation long-running?
Expand All @@ -61,21 +66,24 @@ public void init(ConversationIdGenerator conversationIdGenerator, @ConversationI
{
this.cid = conversationIdGenerator.nextId();
this.timeoutInMilliseconds = timeoutInMilliseconds;
log.debug("Created a new conversation " + this);
}

public void begin()
{
log.debug("Promoted conversation " + cid + " to long-running");
longRunning = true;
}

public void begin(String id)
{
longRunning = true;
cid = id;
begin();
}

public void end()
{
log.debug("Demoted conversation " + cid + " to transient");
longRunning = false;
}

Expand All @@ -96,6 +104,7 @@ public boolean isLongRunning()

public void setTimeout(long timeout)
{
log.debug("Set timeout of conversation " + cid + " to " + timeout);
this.timeoutInMilliseconds = timeout;
}

Expand All @@ -108,9 +117,11 @@ public void setTimeout(long timeout)
*/
public void switchTo(String cid, boolean longRunning, long timeoutInMilliseconds)
{
log.debug("Switched conversation from " + this);
this.cid = cid;
this.longRunning = longRunning;
this.timeoutInMilliseconds = timeoutInMilliseconds;
log.debug("to " + this);
}

@Override
Expand All @@ -121,6 +132,7 @@ public String toString()

public void setLongRunning(boolean longRunning)
{
log.debug("Set conversation " + cid + " to long-running: " + longRunning);
this.longRunning = longRunning;
}
}
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.Future;

import javax.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Current;
import javax.inject.Produces;
import javax.servlet.http.HttpSession;
Expand All @@ -46,9 +47,9 @@ public class ServletConversationManager implements ConversationManager, Serializ
{
private static LogProvider log = Logging.getLogProvider(ServletConversationManager.class);

// FIXME short temp
private static final long CONVERSATION_TIMEOUT_IN_MS = 10 * 30 * 1000;
private static final long CONVERSATION_TIMEOUT_IN_MS = 10 * 60 * 1000;
private static final long CONVERSATION_CONCURRENT_ACCESS_TIMEOUT_IN_MS = 1 * 1000;
private static final String CONVERSATION_ID_NAME = "cid";

// The conversation terminator
@Current
Expand Down Expand Up @@ -106,7 +107,8 @@ public static long getConversationConcurrentAccessTimeout()
@WebBean
public static String getConversationIdName()
{
return "cid";
log.trace("Produced conversation id name " + CONVERSATION_ID_NAME);
return CONVERSATION_ID_NAME;
}

public void beginOrRestoreConversation(String cid)
Expand Down Expand Up @@ -158,9 +160,11 @@ public void beginOrRestoreConversation(String cid)
}
}

// TODO: check that stuff gets terminated when you flip between several long-running conversations
public void cleanupConversation()
{
String cid = currentConversation.getId();
log.trace("Cleaning up conversations for cid " + cid);
if (currentConversation.isLongRunning())
{
Future<?> terminationHandle = scheduleForTermination(cid);
Expand All @@ -180,7 +184,7 @@ public void cleanupConversation()
ConversationEntry conversationEntry = ConversationEntry.of(cid, terminationHandle);
longRunningConversations.put(cid, conversationEntry);
}
log.trace("Scheduled " + currentConversation + " for termination");
log.trace("Scheduled " + currentConversation + " for termination, there are now " + longRunningConversations.size() + " long-running conversations");
}
else
{
Expand All @@ -205,7 +209,7 @@ public void cleanupConversation()
*/
private Future<?> scheduleForTermination(String cid)
{
Runnable terminationTask = new TerminationTask(cid);
Runnable terminationTask = new TerminationTask(cid, (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true));
return conversationTerminator.scheduleForTermination(terminationTask, inactivityTimeout);
}

Expand All @@ -219,30 +223,33 @@ private class TerminationTask implements Runnable
{
// The conversation ID to terminate
private String cid;
private HttpSession session;

/**
* Creates a new termination task
*
* @param cid The conversation ID
*/
public TerminationTask(String cid)
public TerminationTask(String cid, HttpSession session)
{
this.cid = cid;
this.session = session;
}

/**
* Executes the termination
*/
public void run()
{
log.trace("Conversation " + cid + " timed out. Destroying it");
log.debug("Conversation " + cid + " timed out. Destroying it");
longRunningConversations.remove(cid).destroy(session);
log.trace("There are now " + longRunningConversations.size() + " long-running conversations");
}
}

public void destroyAllConversations()
{
log.trace("Destroying " + longRunningConversations.size() + " long-running conversations in session " + session.getId());
log.debug("Destroying " + longRunningConversations.size() + " long-running conversations in session " + session.getId());
for (ConversationEntry conversationEntry : longRunningConversations.values())
{
conversationEntry.destroy(session);
Expand Down

0 comments on commit 871d648

Please sign in to comment.