Search in sources :

Example 11 with LifecycleListener

use of org.jboss.msc.service.LifecycleListener in project wildfly by wildfly.

the class EndpointPublisherImpl method doPublish.

/**
 * Publish the webapp for the WS deployment unit
 *
 * @param target
 * @param unit
 * @return
 * @throws Exception
 */
protected Context doPublish(ServiceTarget target, DeploymentUnit unit) throws Exception {
    final Deployment deployment = unit.getAttachment(WSAttachmentKeys.DEPLOYMENT_KEY);
    final List<Endpoint> endpoints = deployment.getService().getEndpoints();
    // otherwise we need to explicitly wait for the endpoint services to be started before creating the webapp.
    if (!runningInService) {
        final ServiceRegistry registry = unit.getServiceRegistry();
        final CountDownLatch latch = new CountDownLatch(endpoints.size());
        final LifecycleListener listener = new LifecycleListener() {

            @Override
            public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
                if (event == LifecycleEvent.UP) {
                    latch.countDown();
                    controller.removeListener(this);
                }
            }
        };
        ServiceName serviceName;
        for (Endpoint ep : endpoints) {
            serviceName = EndpointService.getServiceName(unit, ep.getShortName());
            registry.getRequiredService(serviceName).addListener(listener);
        }
        latch.await();
    }
    // TODO simplify and use findChild later in destroy()/stopWebApp()
    deployment.addAttachment(WebDeploymentController.class, startWebApp(host, unit));
    return new Context(unit.getAttachment(WSAttachmentKeys.JBOSSWEB_METADATA_KEY).getContextRoot(), endpoints);
}
Also used : Context(org.jboss.wsf.spi.publish.Context) Endpoint(org.jboss.wsf.spi.deployment.Endpoint) ServiceName(org.jboss.msc.service.ServiceName) Deployment(org.jboss.wsf.spi.deployment.Deployment) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) ServiceController(org.jboss.msc.service.ServiceController) ServiceRegistry(org.jboss.msc.service.ServiceRegistry) LifecycleListener(org.jboss.msc.service.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with LifecycleListener

use of org.jboss.msc.service.LifecycleListener in project wildfly by wildfly.

the class WeldBootstrapService method startServiceShutdown.

void startServiceShutdown() {
    // the start service has been shutdown, which means either we are being shutdown/undeployed
    // or we are going to need to bounce the whole deployment
    this.started = false;
    if (controller.getServiceContainer().isShutdown()) {
        // container is being shutdown, no action required
        return;
    }
    ServiceController<?> deploymentController = controller.getServiceContainer().getService(deploymentServiceName);
    if (deploymentController.getMode() != ServiceController.Mode.ACTIVE) {
        // deployment is not active, no action required
        return;
    }
    // add a listener to tentatively 'bounce' this service
    // if the service does actually restart then this will trigger a full deployment restart
    // we do it this way as we don't have visibility into MSC in the general sense
    // so we don't really know if this service is supposed to go away
    // this 'potential bounce' is hard to do in a non-racey manner
    // we need to add the listener first, but the listener may be invoked before the CAS to never
    CompletableFuture<Boolean> attemptingBounce = new CompletableFuture();
    try {
        CompletableFuture<Boolean> listenerDone = new CompletableFuture<>();
        LifecycleListener listener = new LifecycleListener() {

            @Override
            public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
                try {
                    try {
                        if (controller.getServiceContainer().isShutdown() || !attemptingBounce.get()) {
                            controller.removeListener(this);
                            return;
                        }
                    } catch (InterruptedException | ExecutionException e) {
                        throw new RuntimeException(e);
                    }
                    if (deploymentController.getMode() != ServiceController.Mode.ACTIVE || controller.getMode() == ServiceController.Mode.REMOVE) {
                        return;
                    }
                    if (event == LifecycleEvent.DOWN) {
                        controller.removeListener(this);
                        do {
                            if (controller.getMode() != ServiceController.Mode.NEVER) {
                                return;
                            }
                        } while (!controller.compareAndSetMode(ServiceController.Mode.NEVER, ServiceController.Mode.ACTIVE));
                    }
                } finally {
                    listenerDone.complete(true);
                }
            }
        };
        // 
        controller.getServiceContainer().addService(controller.getName().append("fakeStabilityService")).setInstance(new Service() {

            @Override
            public void start(StartContext context) throws StartException {
                context.asynchronous();
                listenerDone.handle(new BiFunction<Boolean, Throwable, Object>() {

                    @Override
                    public Object apply(Boolean aBoolean, Throwable throwable) {
                        context.getController().setMode(ServiceController.Mode.REMOVE);
                        context.complete();
                        return null;
                    }
                });
            }

            @Override
            public void stop(StopContext context) {
            }
        }).install();
        controller.addListener(listener);
        if (!controller.compareAndSetMode(ServiceController.Mode.ACTIVE, ServiceController.Mode.NEVER)) {
            controller.removeListener(listener);
            attemptingBounce.complete(false);
            listenerDone.complete(false);
        } else {
            attemptingBounce.complete(true);
        }
    } catch (Throwable t) {
        // should never happen
        // but lets be safe
        attemptingBounce.completeExceptionally(t);
        throw new RuntimeException(t);
    }
}
Also used : StopContext(org.jboss.msc.service.StopContext) ExecutorService(java.util.concurrent.ExecutorService) Service(org.jboss.msc.Service) LifecycleListener(org.jboss.msc.service.LifecycleListener) CompletableFuture(java.util.concurrent.CompletableFuture) StartContext(org.jboss.msc.service.StartContext) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) ServiceController(org.jboss.msc.service.ServiceController) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with LifecycleListener

use of org.jboss.msc.service.LifecycleListener in project wildfly by wildfly.

the class WeldStartService method start.

@Override
public void start(final StartContext context) throws StartException {
    /*
         * Weld service restarts are not supported. Therefore, if we detect that Weld is being restarted we
         * trigger restart of the entire deployment.
         */
    if (!runOnce.compareAndSet(false, true)) {
        ServiceController<?> controller = context.getController().getServiceContainer().getService(deploymentServiceName);
        controller.addListener(new LifecycleListener() {

            @Override
            public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
                if (event == LifecycleEvent.DOWN) {
                    controller.setMode(Mode.ACTIVE);
                    controller.removeListener(this);
                }
            }
        });
        controller.setMode(Mode.NEVER);
        return;
    }
    ClassLoader oldTccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    try {
        for (SetupAction action : setupActions) {
            action.setup(null);
        }
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
        bootstrapSupplier.get().getBootstrap().startInitialization();
        bootstrapSupplier.get().getBootstrap().deployBeans();
        bootstrapSupplier.get().getBootstrap().validateBeans();
    } finally {
        for (SetupAction action : setupActions) {
            try {
                action.teardown(null);
            } catch (Exception e) {
                WeldLogger.DEPLOYMENT_LOGGER.exceptionClearingThreadState(e);
            }
        }
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
    }
}
Also used : LifecycleEvent(org.jboss.msc.service.LifecycleEvent) SetupAction(org.jboss.as.server.deployment.SetupAction) LifecycleListener(org.jboss.msc.service.LifecycleListener) StartException(org.jboss.msc.service.StartException)

Example 14 with LifecycleListener

use of org.jboss.msc.service.LifecycleListener in project wildfly by wildfly.

the class WeldBootstrapService method start.

/**
 * Starts the weld container
 *
 * @throws IllegalStateException if the container is already running
 */
public synchronized void start(final StartContext context) {
    if (!runOnce.compareAndSet(false, true)) {
        ServiceController<?> controller = context.getController().getServiceContainer().getService(deploymentServiceName);
        controller.addListener(new LifecycleListener() {

            @Override
            public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
                if (event == LifecycleEvent.DOWN) {
                    controller.setMode(ServiceController.Mode.ACTIVE);
                    controller.removeListener(this);
                }
            }
        });
        controller.setMode(ServiceController.Mode.NEVER);
        return;
    }
    if (started) {
        throw WeldLogger.ROOT_LOGGER.alreadyRunning("WeldContainer");
    }
    started = true;
    WeldLogger.DEPLOYMENT_LOGGER.startingWeldService(deploymentName);
    // set up injected services
    addWeldService(SecurityServices.class, securityServicesSupplier.get());
    TransactionServices transactionServices = weldTransactionServicesSupplier != null ? weldTransactionServicesSupplier.get() : null;
    if (transactionServices != null) {
        addWeldService(TransactionServices.class, transactionServices);
    }
    if (!deployment.getServices().contains(ExecutorServices.class)) {
        addWeldService(ExecutorServices.class, executorServicesSupplier.get());
    }
    ModuleGroupSingletonProvider.addClassLoaders(deployment.getModule().getClassLoader(), deployment.getSubDeploymentClassLoaders());
    ClassLoader oldTccl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
    try {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(deployment.getModule().getClassLoader());
        bootstrap.startContainer(deploymentName, environment, deployment);
        WeldProvider.containerInitialized(Container.instance(deploymentName), getBeanManager(), deployment);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
    }
    weldBootstrapServiceConsumer.accept(this);
    // this is the actual service that clients depend on
    // we install it here so that if a restart needs to happen all our dependants won't be able to start
    final ServiceBuilder<?> weldBootstrapServiceBuilder = context.getChildTarget().addService(weldBootstrapServiceName);
    Consumer<WeldBootstrapService> provider = weldBootstrapServiceBuilder.provides(weldBootstrapServiceName);
    weldBootstrapServiceBuilder.setInstance(new Service() {

        @Override
        public void start(StartContext context) throws StartException {
            provider.accept(WeldBootstrapService.this);
        }

        @Override
        public void stop(StopContext context) {
            context.getController().setMode(ServiceController.Mode.REMOVE);
        }
    });
    weldBootstrapServiceBuilder.install();
    controller = context.getController();
}
Also used : StopContext(org.jboss.msc.service.StopContext) ExecutorService(java.util.concurrent.ExecutorService) Service(org.jboss.msc.Service) LifecycleListener(org.jboss.msc.service.LifecycleListener) StartContext(org.jboss.msc.service.StartContext) TransactionServices(org.jboss.weld.transaction.spi.TransactionServices) ExecutorServices(org.jboss.weld.manager.api.ExecutorServices) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) StartException(org.jboss.msc.service.StartException)

Example 15 with LifecycleListener

use of org.jboss.msc.service.LifecycleListener in project wildfly by wildfly.

the class MailSessionAdd method installRuntimeServices.

static void installRuntimeServices(OperationContext context, PathAddress address, ModelNode fullModel) throws OperationFailedException {
    final String jndiName = getJndiName(fullModel, context);
    final CapabilityServiceTarget serviceTarget = context.getCapabilityServiceTarget();
    final MailSessionConfig config = from(context, fullModel);
    final MailSessionService service = new MailSessionService(config);
    final CapabilityServiceBuilder mailSessionBuilder = serviceTarget.addCapability(SESSION_CAPABILITY.fromBaseCapability(address.getLastElement().getValue())).setInstance(service);
    addOutboundSocketDependency(service, mailSessionBuilder, config.getImapServer());
    addCredentialStoreReference(config.getImapServer(), context, fullModel, mailSessionBuilder, MailSubsystemModel.IMAP_SERVER_PATH);
    addOutboundSocketDependency(service, mailSessionBuilder, config.getPop3Server());
    addCredentialStoreReference(config.getPop3Server(), context, fullModel, mailSessionBuilder, MailSubsystemModel.POP3_SERVER_PATH);
    addOutboundSocketDependency(service, mailSessionBuilder, config.getSmtpServer());
    addCredentialStoreReference(config.getSmtpServer(), context, fullModel, mailSessionBuilder, MailSubsystemModel.SMTP_SERVER_PATH);
    for (CustomServerConfig server : config.getCustomServers()) {
        if (server.getOutgoingSocketBinding() != null) {
            addOutboundSocketDependency(service, mailSessionBuilder, server);
        }
        addCredentialStoreReference(server, context, fullModel, mailSessionBuilder, PathElement.pathElement(MailSubsystemModel.CUSTOM_SERVER_PATH.getKey(), server.getProtocol()));
    }
    mailSessionBuilder.addAliases(MAIL_SESSION_SERVICE_NAME.append(address.getLastElement().getValue()));
    final ManagedReferenceFactory valueManagedReferenceFactory = new MailSessionManagedReferenceFactory(service);
    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(jndiName);
    final BinderService binderService = new BinderService(bindInfo.getBindName());
    binderService.getManagedObjectInjector().inject(valueManagedReferenceFactory);
    final ServiceBuilder<?> binderBuilder = serviceTarget.addService(bindInfo.getBinderServiceName(), binderService).addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, binderService.getNamingStoreInjector()).addListener(new LifecycleListener() {

        private volatile boolean bound;

        @Override
        public void handleEvent(final ServiceController<?> controller, final LifecycleEvent event) {
            switch(event) {
                case UP:
                    {
                        MailLogger.ROOT_LOGGER.boundMailSession(jndiName);
                        bound = true;
                        break;
                    }
                case DOWN:
                    {
                        if (bound) {
                            MailLogger.ROOT_LOGGER.unboundMailSession(jndiName);
                        }
                        break;
                    }
                case REMOVED:
                    {
                        MailLogger.ROOT_LOGGER.removedMailSession(jndiName);
                        break;
                    }
            }
        }
    });
    mailSessionBuilder.setInitialMode(ServiceController.Mode.ACTIVE).install();
    binderBuilder.setInitialMode(ServiceController.Mode.ACTIVE).install();
}
Also used : LifecycleListener(org.jboss.msc.service.LifecycleListener) BinderService(org.jboss.as.naming.service.BinderService) CapabilityServiceBuilder(org.jboss.as.controller.CapabilityServiceBuilder) ServiceBasedNamingStore(org.jboss.as.naming.ServiceBasedNamingStore) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) LifecycleEvent(org.jboss.msc.service.LifecycleEvent) CapabilityServiceTarget(org.jboss.as.controller.CapabilityServiceTarget) ContextNames(org.jboss.as.naming.deployment.ContextNames)

Aggregations

LifecycleListener (org.jboss.msc.service.LifecycleListener)23 LifecycleEvent (org.jboss.msc.service.LifecycleEvent)22 ServiceName (org.jboss.msc.service.ServiceName)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ContextNames (org.jboss.as.naming.deployment.ContextNames)8 ServiceController (org.jboss.msc.service.ServiceController)7 BinderService (org.jboss.as.naming.service.BinderService)6 ServiceBasedNamingStore (org.jboss.as.naming.ServiceBasedNamingStore)5 ServiceBuilder (org.jboss.msc.service.ServiceBuilder)5 ServiceRegistry (org.jboss.msc.service.ServiceRegistry)3 ExecutorService (java.util.concurrent.ExecutorService)2 DataSourceStatisticsService (org.jboss.as.connector.services.datasources.statistics.DataSourceStatisticsService)2 DataSourceReferenceFactoryService (org.jboss.as.connector.subsystems.datasources.DataSourceReferenceFactoryService)2 ContextListAndJndiViewManagedReferenceFactory (org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory)2 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 Module (org.jboss.modules.Module)2 ServiceContainer (org.jboss.msc.service.ServiceContainer)2 StartException (org.jboss.msc.service.StartException)2 CompletableFuture (java.util.concurrent.CompletableFuture)1