use of org.jboss.msc.service.StabilityMonitor in project wildfly by wildfly.
the class ServiceContainerHelper method transition.
private static <T> void transition(final ServiceController<T> targetController, final State targetState) {
// Short-circuit if the service is already at the target state
if (targetController.getState() == targetState)
return;
StabilityMonitor monitor = new StabilityMonitor();
monitor.addController(targetController);
try {
// Force service to transition to desired state
Mode targetMode = modeToggle.get(targetState).get(targetController.getMode());
if (targetMode != null) {
targetController.setMode(targetMode);
}
monitor.awaitStability();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
monitor.removeController(targetController);
}
}
use of org.jboss.msc.service.StabilityMonitor in project wildfly by wildfly.
the class ServiceReferenceObjectFactory method getObjectInstance.
@SuppressWarnings("unchecked")
@Override
public final Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
final Reference reference = (Reference) obj;
final ServiceNameRefAdr nameAdr = (ServiceNameRefAdr) reference.get("srof");
if (nameAdr == null) {
throw NamingLogger.ROOT_LOGGER.invalidContextReference("srof");
}
final ServiceName serviceName = (ServiceName) nameAdr.getContent();
final ServiceController<?> controller;
try {
controller = serviceRegistry.getRequiredService(serviceName);
} catch (ServiceNotFoundException e) {
throw NamingLogger.ROOT_LOGGER.cannotResolveService(serviceName);
}
final StabilityMonitor monitor = new StabilityMonitor();
monitor.addController(controller);
try {
monitor.awaitStability();
} catch (InterruptedException e) {
throw NamingLogger.ROOT_LOGGER.threadInterrupt(serviceName);
} finally {
monitor.removeController(controller);
}
switch(controller.getState()) {
case UP:
return getObjectInstance(controller.getValue(), obj, name, nameCtx, environment);
case START_FAILED:
throw NamingLogger.ROOT_LOGGER.cannotResolveService(serviceName, getClass().getName(), "START_FAILED");
case REMOVED:
throw NamingLogger.ROOT_LOGGER.cannotResolveService(serviceName, getClass().getName(), "REMOVED");
}
// we should never get here, as the listener should not notify unless the state was one of the above
throw NamingLogger.ROOT_LOGGER.cannotResolveServiceBug(serviceName, getClass().getName(), controller.getState().toString());
}
use of org.jboss.msc.service.StabilityMonitor in project wildfly by wildfly.
the class SecurityDomainResourceDefinition method waitForService.
/**
* Wait for the required service to start up and fail otherwise. This method is necessary when a runtime operation
* uses a service that might have been created within a composite operation.
*
* This method will wait at most 100 millis.
*
* @param controller the service to wait for
* @throws OperationFailedException if the service is not available, or the thread was interrupted.
*/
private static void waitForService(final ServiceController<?> controller) throws OperationFailedException {
if (controller.getState() == ServiceController.State.UP)
return;
final StabilityMonitor monitor = new StabilityMonitor();
monitor.addController(controller);
try {
monitor.awaitStability(100, MILLISECONDS);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw SecurityLogger.ROOT_LOGGER.interruptedWaitingForSecurityDomain(controller.getName().getSimpleName());
} finally {
monitor.removeController(controller);
}
if (controller.getState() != ServiceController.State.UP) {
throw SecurityLogger.ROOT_LOGGER.requiredSecurityDomainServiceNotAvailable(controller.getName().getSimpleName());
}
}
Aggregations