javax.enterprise.context.BeforeDestroyed Java Examples

The following examples show how to use javax.enterprise.context.BeforeDestroyed. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransactionBeanWithEvents.java    From quarkus with Apache License 2.0 6 votes vote down vote up
void transactionScopePreDestroy(@Observes @BeforeDestroyed(TransactionScoped.class) final Object event,
        final BeanManager beanManager) throws SystemException {
    Transaction tx = tm.getTransaction();
    if (tx == null) {
        log.error("@BeforeDestroyed expects an active transaction");
        throw new IllegalStateException("@BeforeDestroyed expects an active transaction");
    }
    Context ctx;
    try {
        ctx = beanManager.getContext(TransactionScoped.class);
    } catch (Exception e) {
        log.error("Context on @Initialized is not available");
        throw e;
    }
    if (!ctx.isActive()) {
        log.error("Context on @BeforeDestroyed has to be active");
        throw new IllegalStateException("Context on @BeforeDestroyed has to be active");
    }
    if (!(event instanceof Transaction)) {
        log.error("@Intialized scope expects event payload being the " + Transaction.class.getName());
        throw new IllegalStateException("@Intialized scope expects event payload being the " + Transaction.class.getName());
    }

    beforeDestroyedCount++;
}
 
Example #2
Source File: BeanArchives.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static IndexView buildAdditionalIndex() {
    Indexer indexer = new Indexer();
    // CDI API
    index(indexer, ActivateRequestContext.class.getName());
    index(indexer, Default.class.getName());
    index(indexer, Any.class.getName());
    index(indexer, Named.class.getName());
    index(indexer, Initialized.class.getName());
    index(indexer, BeforeDestroyed.class.getName());
    index(indexer, Destroyed.class.getName());
    index(indexer, Intercepted.class.getName());
    index(indexer, Model.class.getName());
    // Arc built-in beans
    index(indexer, ActivateRequestContextInterceptor.class.getName());
    index(indexer, InjectableRequestContextController.class.getName());
    return indexer.complete();
}
 
Example #3
Source File: VertxProducer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Undeploy verticles backed by contextual instances of {@link ApplicationScoped} beans before the application context is
 * destroyed. Otherwise Vertx may attempt to stop the verticles after the CDI container is shut down.
 * 
 * @param event
 * @param beanManager
 */
void undeployVerticles(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event,
        BeanManager beanManager, io.vertx.mutiny.core.Vertx mutiny) {
    // Only beans with the AbstractVerticle in the set of bean types are considered - we need a deployment id 
    Set<Bean<?>> beans = beanManager.getBeans(AbstractVerticle.class, Any.Literal.INSTANCE);
    Context applicationContext = beanManager.getContext(ApplicationScoped.class);
    for (Bean<?> bean : beans) {
        if (ApplicationScoped.class.equals(bean.getScope())) {
            // Only beans with @ApplicationScoped are considered
            Object instance = applicationContext.get(bean);
            if (instance != null) {
                // Only existing instances are considered
                try {
                    AbstractVerticle verticle = (AbstractVerticle) instance;
                    mutiny.undeploy(verticle.deploymentID()).await().indefinitely();
                    LOGGER.debugf("Undeployed verticle: %s", instance.getClass());
                } catch (Exception e) {
                    // In theory, a user can undeploy the verticle manually
                    LOGGER.debugf("Unable to undeploy verticle %s: %s", instance.getClass(), e.toString());
                }
            }
        }
    }
}
 
Example #4
Source File: AppInitializer.java    From oxTrust with MIT License 6 votes vote down vote up
public void destroy(@Observes @BeforeDestroyed(ApplicationScoped.class) ServletContext init) {
	log.info("Stopping services and closing DB connections at server shutdown...");
	log.debug("Checking who intiated destory", new Throwable());

	metricService.close();

	PersistenceEntryManager persistanceEntryManager = persistenceEntryManagerInstance.get();
	closePersistenceEntryManager(persistanceEntryManager, ApplicationFactory.PERSISTENCE_ENTRY_MANAGER_NAME);

	PersistenceEntryManager persistanceMetricEntryManager = persistenceMetricEntryManagerInstance.get();
	closePersistenceEntryManager(persistanceMetricEntryManager,
			ApplicationFactory.PERSISTENCE_METRIC_ENTRY_MANAGER_NAME);

	PersistenceEntryManager persistanceCentralEntryManager = persistenceCentralEntryManagerInstance.get();
	if (persistanceCentralEntryManager != null) {
		closePersistenceEntryManager(persistanceCentralEntryManager,
				ApplicationFactory.PERSISTENCE_CENTRAL_ENTRY_MANAGER_NAME);
	}
}
 
Example #5
Source File: WorkerPoolRegistry.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public void terminate(
        @Observes(notifyObserver = Reception.IF_EXISTS) @Priority(100) @BeforeDestroyed(ApplicationScoped.class) Object event) {
    if (!workerExecutors.isEmpty()) {
        for (WorkerExecutor executor : workerExecutors.values()) {
            executor.close();
        }
    }
}
 
Example #6
Source File: JPAConfig.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Need to shutdown all instances of Hibernate ORM before the actual destroy event,
 * as it might need to use the datasources during shutdown.
 *
 * @param event ignored
 */
void destroy(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event) {
    for (LazyPersistenceUnit factory : persistenceUnits.values()) {
        try {
            factory.close();
        } catch (Exception e) {
            LOGGER.warn("Unable to close the EntityManagerFactory: " + factory, e);
        }
    }
}
 
Example #7
Source File: QuartzScheduler.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Need to gracefully shutdown the scheduler making sure that all triggers have been
 * released before datasource shutdown.
 *
 * @param event ignored
 */
void destroy(@BeforeDestroyed(ApplicationScoped.class) Object event) { //
    if (scheduler != null) {
        try {
            scheduler.shutdown(true); // gracefully shutdown
        } catch (SchedulerException e) {
            LOGGER.warnf("Unable to gracefully shutdown the scheduler", e);
        }
    }
}
 
Example #8
Source File: QuarkusWorkerPoolRegistry.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void terminate(
        @Observes(notifyObserver = Reception.IF_EXISTS) @Priority(100) @BeforeDestroyed(ApplicationScoped.class) Object event) {
    if (!workerExecutors.isEmpty()) {
        for (WorkerExecutor executor : workerExecutors.values()) {
            executor.close();
        }
    }
}
 
Example #9
Source File: AppInitializer.java    From oxAuth with MIT License 5 votes vote down vote up
public void destroy(@Observes @BeforeDestroyed(ApplicationScoped.class) ServletContext init) {
	log.info("Stopping services and closing DB connections at server shutdown...");
	log.debug("Checking who intiated destory", new Throwable());

	metricService.close();

	PersistenceEntryManager persistenceEntryManager = persistenceEntryManagerInstance.get();
	closePersistenceEntryManager(persistenceEntryManager, ApplicationFactory.PERSISTENCE_ENTRY_MANAGER_NAME);

	List<PersistenceEntryManager> persistenceAuthEntryManagers = persistenceAuthEntryManagerInstance.get();
	closePersistenceEntryManagers(persistenceAuthEntryManagers);
}
 
Example #10
Source File: MqttServerConnector.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
public void terminate(
        @Observes(notifyObserver = Reception.IF_EXISTS) @Priority(50) @BeforeDestroyed(ApplicationScoped.class) Object event) {
    if (source != null) {
        source.close();
    }
}
 
Example #11
Source File: AmqpConnector.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
public void terminate(
        @Observes(notifyObserver = Reception.IF_EXISTS) @Priority(50) @BeforeDestroyed(ApplicationScoped.class) Object event) {
    clients.forEach(c -> c.close().subscribeAsCompletionStage());
    clients.clear();
}
 
Example #12
Source File: ExecutionHolder.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
public void terminate(
        @Observes(notifyObserver = Reception.IF_EXISTS) @Priority(200) @BeforeDestroyed(ApplicationScoped.class) Object event) {
    if (internalVertxInstance) {
        vertx.close().await().indefinitely();
    }
}
 
Example #13
Source File: KafkaConnector.java    From smallrye-reactive-messaging with Apache License 2.0 4 votes vote down vote up
public void terminate(
        @Observes(notifyObserver = Reception.IF_EXISTS) @Priority(50) @BeforeDestroyed(ApplicationScoped.class) Object event) {
    sources.forEach(KafkaSource::closeQuietly);
    sinks.forEach(KafkaSink::closeQuietly);
}
 
Example #14
Source File: RequestContext.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static Notifier<Object> createBeforeDestroyedNotifier() {
    return EventImpl.createNotifier(Object.class, Object.class,
            new HashSet<>(Arrays.asList(BeforeDestroyed.Literal.REQUEST, Any.Literal.INSTANCE)),
            ArcContainerImpl.instance());
}
 
Example #15
Source File: ContextObserver.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public void observeContextBeforeDestroyed(@Observes @BeforeDestroyed(RequestScoped.class) Object event) {
    beforeDestroyedObserved++;
}
 
Example #16
Source File: ApplicationInitializedTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
void beforeDestroyed(@Observes @BeforeDestroyed(ApplicationScoped.class) Object container) {
    BEFORE_DESTROYED.set(true);
}