Skip to content

Commit

Permalink
Save ContextState lookup on ManagerContext's initial activation
Browse files Browse the repository at this point in the history
Co-authored-by: [email protected]
  • Loading branch information
franz1981 committed Jul 14, 2023
1 parent ca75503 commit ccabce0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public ArcThreadSetupAction(ManagedContext managedContext) {

@Override
public ThreadState activateInitial() {
managedContext.activate();
InjectableContext.ContextState state = managedContext.getState();
return toThreadState(state);
return toThreadState(managedContext.activate());
}

private ThreadState toThreadState(InjectableContext.ContextState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ public ResteasyReactiveRequestContext get() {
public void set(ResteasyReactiveRequestContext set) {
if (set == null) {
try {
currentVertxRequest.setOtherHttpContextObject(null);
currentVertxRequest.setCurrent(null);
currentVertxRequest.setCurrent(null, null);
} catch (ContextNotActiveException ignored) {
// ignored because for HTTP pipelining it can already be closed
}
} else {
currentVertxRequest.setOtherHttpContextObject(set);
currentVertxRequest.setCurrent(set.unwrap(RoutingContext.class));
currentVertxRequest.setCurrent(set.unwrap(RoutingContext.class), set);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ public interface ManagedContext extends InjectableContext {

/**
* Activate the context with no initial state.
*
* @return the context state
*/
default void activate() {
activate(null);
default ContextState activate() {
return activate(null);
}

/**
* Activate the context.
* <p>
* If invoked with {@code null} parameter, a fresh {@link io.quarkus.arc.InjectableContext.ContextState} is
* automatically created.
*
* @param initialState The initial state, may be {@code null}
* @return the context state
*/
void activate(ContextState initialState);
ContextState activate(ContextState initialState);

/**
* Deactivate the context - do not destoy existing contextual instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,20 @@ public void destroy(Contextual<?> contextual) {
}

@Override
public void activate(ContextState initialState) {
public ContextState activate(ContextState initialState) {
if (LOG.isTraceEnabled()) {
traceActivate(initialState);
}
if (initialState == null) {
currentContext.set(new RequestContextState(new ConcurrentHashMap<>()));
RequestContextState state = new RequestContextState(new ConcurrentHashMap<>());
currentContext.set(state);
// Fire an event with qualifier @Initialized(RequestScoped.class) if there are any observers for it
fireIfNotEmpty(initializedNotifier);
return state;
} else {
if (initialState instanceof RequestContextState) {
currentContext.set((RequestContextState) initialState);
return initialState;
} else {
throw new IllegalArgumentException("Invalid initial state: " + initialState.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@ public class ContextsImpl implements Contexts<Context> {

@Override
public void setActive(Context context) {
// remove the context state we potentially stored, else use null to initiate fresh context
((ManagedContext) context).activate(contextStateMap.remove(context));
if (context.isActive()) {
return;
}
if (context instanceof ManagedContext) {
ManagedContext managed = (ManagedContext) context;
// remove the context state we potentially stored, else use null to initiate fresh context
managed.activate(contextStateMap.remove(context));
}
}

@Override
public void setInactive(Context context) {
// save the state of the context
contextStateMap.put(context, ((ManagedContext) context).getState());
((ManagedContext) context).deactivate();
if (!context.isActive()) {
return;
}
if (context instanceof ManagedContext) {
ManagedContext managed = (ManagedContext) context;
// save the state of the context
contextStateMap.put(context, (managed.getState()));
managed.deactivate();
}
}

@Override
Expand Down

0 comments on commit ccabce0

Please sign in to comment.