diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableObserverMethod.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableObserverMethod.java index 1fc8598b139bb5..36780c811f856a 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableObserverMethod.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableObserverMethod.java @@ -9,6 +9,9 @@ import jakarta.enterprise.inject.spi.Bean; import jakarta.enterprise.inject.spi.ObserverMethod; +import io.quarkus.arc.impl.EventContextImpl; +import io.quarkus.arc.impl.EventMetadataImpl; + /** * Represents an observer method. * @@ -38,6 +41,11 @@ default Bean getDeclaringBean() { return Arc.container().bean(getDeclaringBeanIdentifier()); } + default void notify(T event) { + notify(new EventContextImpl<>(event, + new EventMetadataImpl(getObservedQualifiers(), event.getClass()))); + } + /** * * @return the identifier or null for synthetic observers diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventContextImpl.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventContextImpl.java new file mode 100644 index 00000000000000..be52039fc0c806 --- /dev/null +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventContextImpl.java @@ -0,0 +1,28 @@ +package io.quarkus.arc.impl; + +import jakarta.enterprise.inject.spi.EventContext; +import jakarta.enterprise.inject.spi.EventMetadata; + +// this class is public because it is used in io.quarkus.arc.InjectableObserverMethod +public final class EventContextImpl implements EventContext { + + private final T payload; + + private final EventMetadata metadata; + + public EventContextImpl(T payload, EventMetadata metadata) { + this.payload = payload; + this.metadata = metadata; + } + + @Override + public T getEvent() { + return payload; + } + + @Override + public EventMetadata getMetadata() { + return metadata; + } + +} diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventImpl.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventImpl.java index 71dbb4d24d316a..b35920b97c1f38 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventImpl.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventImpl.java @@ -29,7 +29,6 @@ import jakarta.enterprise.inject.Any; import jakarta.enterprise.inject.spi.EventContext; import jakarta.enterprise.inject.spi.EventMetadata; -import jakarta.enterprise.inject.spi.InjectionPoint; import jakarta.enterprise.inject.spi.ObserverMethod; import jakarta.enterprise.util.TypeLiteral; import jakarta.transaction.RollbackException; @@ -351,58 +350,6 @@ private boolean isNotTxObserver(ObserverMethod observer) { } - static class EventContextImpl implements EventContext { - - private final T payload; - - private final EventMetadata metadata; - - public EventContextImpl(T payload, EventMetadata metadata) { - this.payload = payload; - this.metadata = metadata; - } - - @Override - public T getEvent() { - return payload; - } - - @Override - public EventMetadata getMetadata() { - return metadata; - } - - } - - static class EventMetadataImpl implements EventMetadata { - - private final Set qualifiers; - - private final Type eventType; - - public EventMetadataImpl(Set qualifiers, Type eventType) { - this.qualifiers = qualifiers; - this.eventType = eventType; - } - - @Override - public Set getQualifiers() { - return qualifiers; - } - - @Override - public InjectionPoint getInjectionPoint() { - // Currently we do not support injection point of the injected Event instance which fired the event - return null; - } - - @Override - public Type getType() { - return eventType; - } - - } - static class ArcSynchronization implements Synchronization { private List> deferredEvents; diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventMetadataImpl.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventMetadataImpl.java new file mode 100644 index 00000000000000..cf01e6aa0c3659 --- /dev/null +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/EventMetadataImpl.java @@ -0,0 +1,38 @@ +package io.quarkus.arc.impl; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Set; + +import jakarta.enterprise.inject.spi.EventMetadata; +import jakarta.enterprise.inject.spi.InjectionPoint; + +// this class is public because it is used in io.quarkus.arc.InjectableObserverMethod +public final class EventMetadataImpl implements EventMetadata { + + private final Set qualifiers; + + private final Type eventType; + + public EventMetadataImpl(Set qualifiers, Type eventType) { + this.qualifiers = qualifiers; + this.eventType = eventType; + } + + @Override + public Set getQualifiers() { + return qualifiers; + } + + @Override + public InjectionPoint getInjectionPoint() { + // Currently we do not support injection point of the injected Event instance which fired the event + return null; + } + + @Override + public Type getType() { + return eventType; + } + +} diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/ifexists/ReceptionIfExistsTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/ifexists/ReceptionIfExistsTest.java index 5cab7d8d171f1e..99a4cd94c31dda 100644 --- a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/ifexists/ReceptionIfExistsTest.java +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/ifexists/ReceptionIfExistsTest.java @@ -53,7 +53,6 @@ public void testObserver() { assertEquals(DependentObserver.class.getName() + "foo", EVENTS.get(1)); container.requestContext().deactivate(); - // TODO check reception metadata Set> foundOm = Arc.container().beanManager().resolveObserverMethods("eventString"); assertEquals(2, foundOm.size()); for (ObserverMethod om : foundOm) { diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/notification/ManualNotifyInvocationTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/notification/ManualNotifyInvocationTest.java new file mode 100644 index 00000000000000..a60ac261dd7e2c --- /dev/null +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/observers/notification/ManualNotifyInvocationTest.java @@ -0,0 +1,43 @@ +package io.quarkus.arc.test.observers.notification; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.inject.spi.ObserverMethod; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.arc.test.ArcTestContainer; + +public class ManualNotifyInvocationTest { + + @RegisterExtension + public ArcTestContainer container = new ArcTestContainer(StringObserver.class); + + @Test + public void testManualNotifyInvocation() { + assertEquals(0, StringObserver.NOTIFIED.get()); + Arc.container().beanManager().getEvent().fire("hello"); + assertEquals(1, StringObserver.NOTIFIED.get()); + + Set> foundOM = Arc.container().beanManager().resolveObserverMethods("foo"); + assertEquals(1, foundOM.size()); + foundOM.iterator().next().notify("test"); + assertEquals(2, StringObserver.NOTIFIED.get()); + } + + @ApplicationScoped + static class StringObserver { + private static final AtomicInteger NOTIFIED = new AtomicInteger(); + + void observeString(@Observes String value) { + NOTIFIED.incrementAndGet(); + } + } +}