Allow nesting propagated request scopes on same thread

ReceiveCommits invokes Runnables on the calling thread whenever
there is no background thread pool, or the thread pool is busy
and has started to reject new tasks. Permit nesting the context
by saving the old one onto the thread stack, and restoring it
after the call finishes.

Change-Id: I524aa65981417080d6a5d3a2c090e52e2e64351d
This commit is contained in:
Shawn O. Pearce
2012-08-06 08:11:01 -07:00
parent 513e86debe
commit cbf3eea58c

View File

@@ -45,19 +45,16 @@ public abstract class ThreadLocalRequestScopePropagator<C>
return new Callable<T>() {
@Override
public T call() throws Exception {
if (threadLocal.get() != null) {
// This is consistent with the Guice ServletScopes.continueRequest()
// behavior.
throw new IllegalStateException("Cannot continue request, "
+ "thread already has request in progress. A new thread must "
+ "be used to propagate the request scope context.");
}
C old = threadLocal.get();
threadLocal.set(ctx);
try {
return callable.call();
} finally {
threadLocal.remove();
if (old != null) {
threadLocal.set(old);
} else {
threadLocal.remove();
}
}
}
};