Skip to content

Commit

Permalink
add beforeExecute/afterExecute callback
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 16, 2015
1 parent 29e3b14 commit b963d6c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/alibaba/mtc/MtContextThreadLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ protected T copy(T parentValue) {
return parentValue;
}

protected void beforeExecute() {
}

protected void afterExecute() {
}

@Override
public final T get() {
T value = super.get();
Expand Down Expand Up @@ -105,7 +111,7 @@ static Map<MtContextThreadLocal<?>, Object> backupAndSet(Map<MtContextThreadLoca
threadLocal.superRemove();
}
}
setMtContexts(copied);
setMtContexts(copied, true);
return backup;
}

Expand All @@ -120,14 +126,19 @@ static void restore(Map<MtContextThreadLocal<?>, Object> backup) {
threadLocal.superRemove();
}
}
setMtContexts(backup);
setMtContexts(backup, false);
}

static void setMtContexts(Map<MtContextThreadLocal<?>, Object> set) {
static void setMtContexts(Map<MtContextThreadLocal<?>, Object> set, boolean isStore) {
for (Map.Entry<MtContextThreadLocal<?>, Object> entry : set.entrySet()) {
@SuppressWarnings("unchecked")
MtContextThreadLocal<Object> threadLocal = (MtContextThreadLocal<Object>) entry.getKey();
threadLocal.set(entry.getValue());
if (isStore) {
threadLocal.beforeExecute();
} else {
threadLocal.afterExecute();
}
}
}
}

0 comments on commit b963d6c

Please sign in to comment.