Search in sources :

Example 1 with EventMetadata

use of javax.enterprise.inject.spi.EventMetadata in project core by weld.

the class FastEvent method of.

/**
 * Constructs a new FastEvent instance
 * @param type the event type
 * @param manager the bean manager
 * @param notifier the notifier to be used for observer method resolution
 * @param qualifiers the event qualifiers
 * @return
 */
public static <T> FastEvent<T> of(Class<T> type, BeanManagerImpl manager, ObserverNotifier notifier, Annotation... qualifiers) {
    ResolvedObservers<T> resolvedObserverMethods = notifier.<T>resolveObserverMethods(type, qualifiers);
    if (resolvedObserverMethods.isMetadataRequired()) {
        EventMetadata metadata = new EventMetadataImpl(type, null, qualifiers);
        CurrentEventMetadata metadataService = manager.getServices().get(CurrentEventMetadata.class);
        return new FastEventWithMetadataPropagation<T>(resolvedObserverMethods, metadata, metadataService);
    } else {
        return new FastEvent<T>(resolvedObserverMethods);
    }
}
Also used : EventMetadata(javax.enterprise.inject.spi.EventMetadata)

Example 2 with EventMetadata

use of javax.enterprise.inject.spi.EventMetadata in project core by weld.

the class ObserverNotifier method notifyAsyncObservers.

protected <T, U extends T> CompletionStage<U> notifyAsyncObservers(List<ObserverMethod<? super T>> observers, U event, EventMetadata metadata, Executor executor, NotificationOptions options) {
    if (executor == null) {
        executor = asyncEventExecutor;
    }
    if (observers.isEmpty()) {
        return AsyncEventDeliveryStage.completed(event, executor);
    }
    // We should always initialize and validate all notification options first
    final NotificationMode mode = initModeOption(options.get(WeldNotificationOptions.MODE));
    final Long timeout = initTimeoutOption(options.get(WeldNotificationOptions.TIMEOUT));
    final Consumer<Runnable> securityContextActionConsumer = securityServices.getSecurityContextAssociator();
    final ObserverExceptionHandler exceptionHandler;
    CompletableFuture<U> completableFuture;
    if (observers.size() > 1 && NotificationMode.PARALLEL.equals(mode)) {
        // Attempt to notify async observers in parallel
        exceptionHandler = new CollectingExceptionHandler(new CopyOnWriteArrayList<>());
        List<CompletableFuture<T>> completableFutures = new ArrayList<>(observers.size());
        for (ObserverMethod<? super T> observer : observers) {
            completableFutures.add(CompletableFuture.supplyAsync(createSupplier(securityContextActionConsumer, event, metadata, exceptionHandler, false, () -> {
                notifyAsyncObserver(observer, event, metadata, exceptionHandler);
            }), executor));
        }
        completableFuture = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[] {})).thenApply((ignoredVoid) -> {
            handleExceptions(exceptionHandler);
            return event;
        });
    } else {
        // Async observers are notified serially in a single worker thread
        exceptionHandler = new CollectingExceptionHandler();
        completableFuture = CompletableFuture.supplyAsync(createSupplier(securityContextActionConsumer, event, metadata, exceptionHandler, true, () -> {
            for (ObserverMethod<? super T> observer : observers) {
                notifyAsyncObserver(observer, event, metadata, exceptionHandler);
            }
        }), executor);
    }
    // If NotificationOptionKeys.TIMEOUT is set, we will trigger the counter and use CompletableFuture.anyOf()
    if (timeout != null) {
        completableFuture = CompletableFuture.anyOf(completableFuture, startTimer(timeout)).thenApply((ignoredObject) -> event);
    }
    return new AsyncEventDeliveryStage<>(completableFuture, executor);
}
Also used : ServiceRegistry(org.jboss.weld.bootstrap.api.ServiceRegistry) WeldNotificationOptions(org.jboss.weld.events.WeldNotificationOptions) EventLogger(org.jboss.weld.logging.EventLogger) Reflections(org.jboss.weld.util.reflection.Reflections) SharedObjectCache(org.jboss.weld.resources.SharedObjectCache) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) Types(org.jboss.weld.util.Types) Reflections.cast(org.jboss.weld.util.reflection.Reflections.cast) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ComputingCache(org.jboss.weld.util.cache.ComputingCache) QualifierInstance(org.jboss.weld.resolution.QualifierInstance) LazyValueHolder(org.jboss.weld.util.LazyValueHolder) NotificationOptions(javax.enterprise.event.NotificationOptions) SecurityServices(org.jboss.weld.security.spi.SecurityServices) ExecutorServices(org.jboss.weld.manager.api.ExecutorServices) Observers(org.jboss.weld.util.Observers) EventMetadata(javax.enterprise.inject.spi.EventMetadata) UtilLogger(org.jboss.weld.logging.UtilLogger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RequestContext(org.jboss.weld.context.RequestContext) LinkedList(java.util.LinkedList) ResolvableBuilder(org.jboss.weld.resolution.ResolvableBuilder) ThreadLocalStackReference(org.jboss.weld.injection.ThreadLocalStack.ThreadLocalStackReference) NotificationMode(org.jboss.weld.events.WeldNotificationOptions.NotificationMode) Executor(java.util.concurrent.Executor) UnboundLiteral(org.jboss.weld.context.unbound.UnboundLiteral) Set(java.util.Set) ObserverException(javax.enterprise.event.ObserverException) CompletionException(java.util.concurrent.CompletionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Resolvable(org.jboss.weld.resolution.Resolvable) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ComputingCacheBuilder(org.jboss.weld.util.cache.ComputingCacheBuilder) Type(java.lang.reflect.Type) ForkJoinPool(java.util.concurrent.ForkJoinPool) Annotation(java.lang.annotation.Annotation) Collections(java.util.Collections) TypeSafeObserverResolver(org.jboss.weld.resolution.TypeSafeObserverResolver) Container(org.jboss.weld.Container) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NotificationMode(org.jboss.weld.events.WeldNotificationOptions.NotificationMode) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 3 with EventMetadata

use of javax.enterprise.inject.spi.EventMetadata in project core by weld.

the class ProbeObserver method notify.

@Override
public void notify(Object event) {
    EventMetadata metadata = currentEventMetadata.peek();
    if (excludePattern != null && excludePattern.matcher(Formats.formatType(metadata.getType(), false)).matches()) {
        ProbeLogger.LOG.eventExcluded(metadata.getType());
        return;
    }
    boolean containerEvent = isContainerEvent(metadata.getQualifiers());
    List<ObserverMethod<?>> observers = resolveObservers(metadata, containerEvent);
    EventInfo info = new EventInfo(metadata.getType(), metadata.getQualifiers(), event, metadata.getInjectionPoint(), observers, containerEvent, System.currentTimeMillis());
    probe.addEvent(info);
}
Also used : EventMetadata(javax.enterprise.inject.spi.EventMetadata) CurrentEventMetadata(org.jboss.weld.event.CurrentEventMetadata) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod)

Example 4 with EventMetadata

use of javax.enterprise.inject.spi.EventMetadata in project core by weld.

the class HttpContextLifecycle method fireEventForApplicationScope.

private void fireEventForApplicationScope(ServletContext ctx, Annotation qualifier) {
    if (module != null) {
        // Deliver events sequentially
        synchronized (container) {
            if (module.isWebModule()) {
                module.fireEvent(ServletContext.class, ctx, qualifier);
            } else {
                // fallback for backward compatibility
                ServletLogger.LOG.noEeModuleDescriptor(beanManager);
                final EventMetadata metadata = new EventMetadataImpl(ServletContext.class, null, Collections.singleton(qualifier));
                beanManager.getAccessibleLenientObserverNotifier().fireEvent(ServletContext.class, ctx, metadata, qualifier);
            }
        }
    }
}
Also used : EventMetadataImpl(org.jboss.weld.event.EventMetadataImpl) EventMetadata(javax.enterprise.inject.spi.EventMetadata)

Example 5 with EventMetadata

use of javax.enterprise.inject.spi.EventMetadata in project core by weld.

the class BeanDeploymentModule method fireEvent.

/**
 * Fire an event and notify observers that belong to this module.
 * @param eventType
 * @param event
 * @param qualifiers
 */
public void fireEvent(Type eventType, Object event, Annotation... qualifiers) {
    final EventMetadata metadata = new EventMetadataImpl(eventType, null, qualifiers);
    notifier.fireEvent(eventType, event, metadata, qualifiers);
}
Also used : EventMetadataImpl(org.jboss.weld.event.EventMetadataImpl) EventMetadata(javax.enterprise.inject.spi.EventMetadata)

Aggregations

EventMetadata (javax.enterprise.inject.spi.EventMetadata)7 Type (java.lang.reflect.Type)3 EventMetadataImpl (org.jboss.weld.event.EventMetadataImpl)3 ObserverMethod (javax.enterprise.inject.spi.ObserverMethod)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 CompletionStage (java.util.concurrent.CompletionStage)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Executor (java.util.concurrent.Executor)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1