Skip to content

Commit 12d8460

Browse files
committed
Added better logging and details
1 parent 47655f0 commit 12d8460

2 files changed

Lines changed: 30 additions & 24 deletions

File tree

lib/src/main/java/com/codeheadsystems/microbus/ContextFactory.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
import java.util.Optional;
55
import java.util.Stack;
66
import java.util.UUID;
7-
import java.util.function.Consumer;
87
import java.util.function.Supplier;
98
import javax.inject.Inject;
109
import javax.inject.Singleton;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1112

1213
@Singleton
1314
public class ContextFactory {
1415

16+
private static final Logger LOGGER = LoggerFactory.getLogger(ContextFactory.class);
17+
1518
private final ThreadLocal<Stack<Context>> threadLocalContext;
16-
private final Supplier<UUID> uuidSupplier;
19+
private final Supplier<Context> contextSupplier;
1720

1821
public ContextFactory() {
1922
this(new DefaultUUIDSupplier());
2023
}
2124

2225
@Inject
2326
public ContextFactory(Supplier<UUID> uuidSupplier) {
24-
this.uuidSupplier = Objects.requireNonNull(uuidSupplier);
25-
threadLocalContext = ThreadLocal.withInitial(Stack::new);
27+
Objects.requireNonNull(uuidSupplier);
28+
this.contextSupplier = () -> new Context(uuidSupplier.get());
29+
this.threadLocalContext = ThreadLocal.withInitial(Stack::new);
2630
}
2731

2832
/**
@@ -54,35 +58,37 @@ public UUID contextUuid() {
5458
* Removes the full stack of context.
5559
*/
5660
public void clearAllContexts() {
61+
LOGGER.info("Clearing all contexts"); // warn folks because this means something is wonky, or about to be.
5762
threadLocalContext.remove();
5863
}
5964

6065
public <T> T withSupplier(Supplier<T> supplier) {
61-
try {
62-
threadLocalContext.get().push(generateContext());
63-
return supplier.get();
64-
} finally {
65-
ifNotEmpty(Stack::pop);
66-
}
66+
return withContextSet(supplier);
6767
}
6868

6969
public void withRunnable(Runnable runnable) {
70-
try {
71-
threadLocalContext.get().push(generateContext());
70+
withContextSet(() -> {
7271
runnable.run();
73-
} finally {
74-
ifNotEmpty(Stack::pop);
75-
}
72+
return null;
73+
});
7674
}
7775

78-
private Context generateContext() {
79-
return new Context(uuidSupplier.get());
80-
}
81-
82-
private void ifNotEmpty(Consumer<Stack<Context>> consumer) {
83-
Stack<Context> stack = threadLocalContext.get();
84-
if (!stack.isEmpty()) {
85-
consumer.accept(stack);
76+
private <T> T withContextSet(Supplier<T> supplier) {
77+
final Context context = contextSupplier.get();
78+
final Stack<Context> stack = threadLocalContext.get();
79+
stack.push(context);
80+
try {
81+
return supplier.get();
82+
} finally {
83+
if (stack.isEmpty()) { // someone cleared the stack?
84+
LOGGER.warn("Context stack is not empty after supplier execution, possible leak: {}", stack);
85+
} else { // stack isn't empty
86+
final Context old = stack.pop();
87+
if (old != context) { // Who's been messing with the stack?
88+
LOGGER.warn("Incorrect context removed desired:{} removed:{}", context, old);
89+
}
90+
}
8691
}
8792
}
93+
8894
}

lib/src/main/java/com/codeheadsystems/microbus/MicroBus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public MicroBus(ContextFactory contextFactory) {
2525

2626
public MicroBus(ContextFactory contextFactory,
2727
ExecutorService executorService) {
28-
LOGGER.debug("MicroBus()");
28+
LOGGER.info("MicroBus()");
2929
this.contextFactory = Objects.requireNonNull(contextFactory);
3030
this.executorService = Objects.requireNonNull(executorService);
3131
}

0 commit comments

Comments
 (0)