Search in sources :

Example 1 with CountDownLifecycleListener

use of org.wildfly.clustering.service.CountDownLifecycleListener in project wildfly by wildfly.

the class ServiceLifecycle method transition.

private void transition(Transition transition) {
    // Short-circuit if the service is already at the target state
    if (this.isComplete(transition))
        return;
    CountDownLatch latch = new CountDownLatch(1);
    LifecycleListener listener = new CountDownLifecycleListener(latch, transition.targetEvents);
    this.controller.addListener(listener);
    try {
        if (this.isComplete(transition))
            return;
        // Force service to transition to desired state
        Mode currentMode = this.controller.getMode();
        if (currentMode == ServiceController.Mode.REMOVE) {
            throw new IllegalStateException(new ServiceNotFoundException(this.controller.getName().getCanonicalName()));
        }
        Mode targetMode = transition.modeTransitions.get(currentMode);
        if (targetMode == null) {
            throw new IllegalStateException(currentMode.name());
        }
        this.controller.setMode(targetMode);
        latch.await();
        if (this.controller.getState() == State.START_FAILED) {
            throw new IllegalStateException(this.controller.getStartException());
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } finally {
        this.controller.removeListener(listener);
    }
}
Also used : ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException) Mode(org.jboss.msc.service.ServiceController.Mode) CountDownLifecycleListener(org.wildfly.clustering.service.CountDownLifecycleListener) LifecycleListener(org.jboss.msc.service.LifecycleListener) CountDownLifecycleListener(org.wildfly.clustering.service.CountDownLifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CountDownLifecycleListener

use of org.wildfly.clustering.service.CountDownLifecycleListener in project wildfly by wildfly.

the class StackOperationHandler method executeRuntimeStep.

/* Method is synchronized to avoid duplicate service exceptions if called concurrently */
@Override
protected synchronized void executeRuntimeStep(OperationContext context, ModelNode op) throws OperationFailedException {
    String name = Operations.getName(op);
    Operation<ChannelFactory> operation = this.operations.get(name);
    ServiceName serviceName = JGroupsRequirement.CHANNEL_FACTORY.getServiceName(context, UnaryCapabilityNameResolver.DEFAULT);
    Function<ChannelFactory, ModelNode> operationFunction = new Function<ChannelFactory, ModelNode>() {

        @Override
        public ModelNode apply(ChannelFactory factory) {
            try {
                return operation.execute(context, op, factory);
            } catch (OperationFailedException e) {
                throw new RuntimeException(e);
            }
        }
    };
    ServiceBuilder<?> builder = context.getServiceTarget().addService(serviceName.append(this.getClass().getSimpleName()));
    Supplier<ChannelFactory> factory = builder.requires(serviceName);
    Reference<ModelNode> reference = new Reference<>();
    CountDownLatch startLatch = new CountDownLatch(1);
    CountDownLatch removeLatch = new CountDownLatch(1);
    ServiceController<?> controller = builder.addListener(new CountDownLifecycleListener(startLatch, EnumSet.of(LifecycleEvent.UP, LifecycleEvent.FAILED))).addListener(new CountDownLifecycleListener(removeLatch, EnumSet.of(LifecycleEvent.REMOVED))).setInstance(new FunctionalService<>(reference, operationFunction, factory)).setInitialMode(ServiceController.Mode.ACTIVE).install();
    try {
        startLatch.await();
        ModelNode result = reference.get();
        if (result != null) {
            context.getResult().set(result);
        } else {
            context.getFailureDescription().set(controller.getStartException().getLocalizedMessage());
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new OperationFailedException(e);
    } finally {
        // Make sure service is removed
        try {
            controller.setMode(ServiceController.Mode.REMOVE);
            removeLatch.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        context.completeStep(OperationContext.ResultHandler.NOOP_RESULT_HANDLER);
    }
}
Also used : OperationFailedException(org.jboss.as.controller.OperationFailedException) ChannelFactory(org.wildfly.clustering.jgroups.spi.ChannelFactory) CountDownLatch(java.util.concurrent.CountDownLatch) Function(java.util.function.Function) ServiceName(org.jboss.msc.service.ServiceName) CountDownLifecycleListener(org.wildfly.clustering.service.CountDownLifecycleListener) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 CountDownLifecycleListener (org.wildfly.clustering.service.CountDownLifecycleListener)2 Function (java.util.function.Function)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 ModelNode (org.jboss.dmr.ModelNode)1 LifecycleListener (org.jboss.msc.service.LifecycleListener)1 Mode (org.jboss.msc.service.ServiceController.Mode)1 ServiceName (org.jboss.msc.service.ServiceName)1 ServiceNotFoundException (org.jboss.msc.service.ServiceNotFoundException)1 ChannelFactory (org.wildfly.clustering.jgroups.spi.ChannelFactory)1