Skip to content

Commit

Permalink
ArC - do not activate request context for Inialized/Destroyed events
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed May 31, 2021
1 parent 0bc477f commit 73f47a0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void init() {
Set<Annotation> qualifiers = new HashSet<>(4);
qualifiers.add(Initialized.Literal.APPLICATION);
qualifiers.add(Any.Literal.INSTANCE);
EventImpl.createNotifier(Object.class, Object.class, qualifiers, this).notify(toString());
EventImpl.createNotifier(Object.class, Object.class, qualifiers, this, false).notify(toString());
// Configure CDIProvider used for CDI.current()
CDI.setCDIProvider(new ArcCDIProvider());
LOGGER.debugf("ArC DI container initialized [beans=%s, observers=%s]", beans.size(), observers.size());
Expand Down Expand Up @@ -352,7 +352,7 @@ public synchronized void shutdown() {
beforeDestroyQualifiers.add(BeforeDestroyed.Literal.APPLICATION);
beforeDestroyQualifiers.add(Any.Literal.INSTANCE);
try {
EventImpl.createNotifier(Object.class, Object.class, beforeDestroyQualifiers, this).notify(toString());
EventImpl.createNotifier(Object.class, Object.class, beforeDestroyQualifiers, this, false).notify(toString());
} catch (Exception e) {
LOGGER.warn("An error occurred during delivery of the @BeforeDestroyed(ApplicationScoped.class) event", e);
}
Expand All @@ -363,7 +363,7 @@ public synchronized void shutdown() {
destroyQualifiers.add(Destroyed.Literal.APPLICATION);
destroyQualifiers.add(Any.Literal.INSTANCE);
try {
EventImpl.createNotifier(Object.class, Object.class, destroyQualifiers, this).notify(toString());
EventImpl.createNotifier(Object.class, Object.class, destroyQualifiers, this, false).notify(toString());
} catch (Exception e) {
LOGGER.warn("An error occurred during delivery of the @Destroyed(ApplicationScoped.class) event", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,17 @@ private Notifier<? super T> createNotifier(Class<?> runtimeType) {

static <T> Notifier<T> createNotifier(Class<?> runtimeType, Type eventType, Set<Annotation> qualifiers,
ArcContainerImpl container) {
return createNotifier(runtimeType, eventType, qualifiers, container, true);
}

static <T> Notifier<T> createNotifier(Class<?> runtimeType, Type eventType, Set<Annotation> qualifiers,
ArcContainerImpl container, boolean activateRequestContext) {
EventMetadata metadata = new EventMetadataImpl(qualifiers, eventType);
List<ObserverMethod<? super T>> notifierObserverMethods = new ArrayList<>();
for (ObserverMethod<? super T> observerMethod : container.resolveObservers(eventType, qualifiers)) {
notifierObserverMethods.add(observerMethod);
}
return new Notifier<>(runtimeType, notifierObserverMethods, metadata);
return new Notifier<>(runtimeType, notifierObserverMethods, metadata, activateRequestContext);
}

private Type initEventType(Type type) {
Expand Down Expand Up @@ -206,12 +211,19 @@ static class Notifier<T> {
private final List<ObserverMethod<? super T>> observerMethods;
private final EventMetadata eventMetadata;
private final boolean hasTxObservers;
private final boolean activateRequestContext;

Notifier(Class<?> runtimeType, List<ObserverMethod<? super T>> observerMethods, EventMetadata eventMetadata) {
this(runtimeType, observerMethods, eventMetadata, true);
}

Notifier(Class<?> runtimeType, List<ObserverMethod<? super T>> observerMethods, EventMetadata eventMetadata,
boolean activateRequestContext) {
this.runtimeType = runtimeType;
this.observerMethods = observerMethods;
this.eventMetadata = eventMetadata;
this.hasTxObservers = observerMethods.stream().anyMatch(this::isTxObserver);
this.activateRequestContext = activateRequestContext;
}

void notify(T event) {
Expand All @@ -222,7 +234,8 @@ void notify(T event) {
void notify(T event, ObserverExceptionHandler exceptionHandler, boolean async) {
if (!isEmpty()) {

Predicate<ObserverMethod<? super T>> predicate = async ? ObserverMethod::isAsync : this::isSyncObserver;
Predicate<ObserverMethod<? super T>> predicate = async ? ObserverMethod::isAsync
: Predicate.not(ObserverMethod::isAsync);

if (!async && hasTxObservers) {
// Note that tx observers are never async
Expand Down Expand Up @@ -258,17 +271,21 @@ void notify(T event, ObserverExceptionHandler exceptionHandler, boolean async) {
}
}

// Sync notifications
ManagedContext requestContext = Arc.container().requestContext();
if (requestContext.isActive()) {
notifyObservers(event, exceptionHandler, predicate);
} else {
try {
requestContext.activate();
// Non-tx observers notifications
if (activateRequestContext) {
ManagedContext requestContext = Arc.container().requestContext();
if (requestContext.isActive()) {
notifyObservers(event, exceptionHandler, predicate);
} finally {
requestContext.terminate();
} else {
try {
requestContext.activate();
notifyObservers(event, exceptionHandler, predicate);
} finally {
requestContext.terminate();
}
}
} else {
notifyObservers(event, exceptionHandler, predicate);
}
}
}
Expand Down Expand Up @@ -304,10 +321,6 @@ private boolean isNotTxObserver(ObserverMethod<?> observer) {
return !isTxObserver(observer);
}

private boolean isSyncObserver(ObserverMethod<?> observer) {
return !observer.isAsync();
}

}

static class EventContextImpl<T> implements EventContext<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@ private void fireIfNotEmpty(LazyValue<Notifier<Object>> value) {
private static Notifier<Object> createInitializedNotifier() {
return EventImpl.createNotifier(Object.class, Object.class,
new HashSet<>(Arrays.asList(Initialized.Literal.REQUEST, Any.Literal.INSTANCE)),
ArcContainerImpl.instance());
ArcContainerImpl.instance(), false);
}

private static Notifier<Object> createBeforeDestroyedNotifier() {
return EventImpl.createNotifier(Object.class, Object.class,
new HashSet<>(Arrays.asList(BeforeDestroyed.Literal.REQUEST, Any.Literal.INSTANCE)),
ArcContainerImpl.instance());
ArcContainerImpl.instance(), false);
}

private static Notifier<Object> createDestroyedNotifier() {
return EventImpl.createNotifier(Object.class, Object.class,
new HashSet<>(Arrays.asList(Destroyed.Literal.REQUEST, Any.Literal.INSTANCE)),
ArcContainerImpl.instance());
ArcContainerImpl.instance(), false);
}

static class RequestContextState implements ContextState {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.arc.impl;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;
import javax.enterprise.context.Initialized;
import javax.enterprise.inject.Any;
import org.junit.jupiter.api.Test;

public class QualifiersTest {

@Test
public void testIsSubset() {
Set<Annotation> observed = Set.of(Initialized.Literal.REQUEST, Any.Literal.INSTANCE);
Set<Annotation> event = Set.of(Initialized.Literal.APPLICATION, Any.Literal.INSTANCE);
assertFalse(Qualifiers.isSubset(observed, event, Collections.emptyMap()));

observed = Set.of(Initialized.Literal.APPLICATION, Any.Literal.INSTANCE);
assertTrue(Qualifiers.isSubset(observed, event, Collections.emptyMap()));

observed = Set.of(Any.Literal.INSTANCE);
assertTrue(Qualifiers.isSubset(observed, event, Collections.emptyMap()));

observed = Set.of(Initialized.Literal.APPLICATION);
assertTrue(Qualifiers.isSubset(observed, event, Collections.emptyMap()));
}

}

This file was deleted.

0 comments on commit 73f47a0

Please sign in to comment.