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);
}
}
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);
}
}
Aggregations