Search in sources :

Example 1 with ServiceNotFoundException

use of org.jboss.msc.service.ServiceNotFoundException 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();
    try {
        final ServiceController<?> controller = serviceRegistry.getRequiredService(serviceName);
        return getObjectInstance(controller.awaitValue(), obj, name, nameCtx, environment);
    } catch (ServiceNotFoundException e) {
        throw NamingLogger.ROOT_LOGGER.cannotResolveService(serviceName);
    } catch (InterruptedException e) {
        throw NamingLogger.ROOT_LOGGER.threadInterrupt(serviceName);
    } catch (Throwable t) {
        throw NamingLogger.ROOT_LOGGER.cannotResolveService(serviceName, getClass().getName(), "START_FAILED");
    }
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) ModularReference(org.jboss.as.naming.context.ModularReference) Reference(javax.naming.Reference) ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException)

Example 2 with ServiceNotFoundException

use of org.jboss.msc.service.ServiceNotFoundException 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 3 with ServiceNotFoundException

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

the class ExceptionTestCase method test.

@Test
public void test() throws IOException {
    Tester<Throwable> tester = ProtoStreamTesterFactory.INSTANCE.createTester();
    tester.test(new StartException(), ExceptionTestCase::assertEquals);
    tester.test(new StartException("message"), ExceptionTestCase::assertEquals);
    tester.test(new StartException(new Exception()), ExceptionTestCase::assertEquals);
    tester.test(new StartException("message", new Exception()), ExceptionTestCase::assertEquals);
    tester.test(new ServiceNotFoundException(), ExceptionTestCase::assertEquals);
    tester.test(new ServiceNotFoundException("message"), ExceptionTestCase::assertEquals);
    tester.test(new ServiceNotFoundException(new Exception()), ExceptionTestCase::assertEquals);
    tester.test(new ServiceNotFoundException("message", new Exception()), ExceptionTestCase::assertEquals);
}
Also used : ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException) StartException(org.jboss.msc.service.StartException) ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException) IOException(java.io.IOException) StartException(org.jboss.msc.service.StartException) Test(org.junit.Test)

Example 4 with ServiceNotFoundException

use of org.jboss.msc.service.ServiceNotFoundException 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());
}
Also used : ServiceName(org.jboss.msc.service.ServiceName) Reference(javax.naming.Reference) ModularReference(org.jboss.as.naming.context.ModularReference) ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException) StabilityMonitor(org.jboss.msc.service.StabilityMonitor)

Example 5 with ServiceNotFoundException

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

the class AbstractDistributedSingletonService method providersChanged.

@Override
public synchronized void providersChanged(Set<Node> nodes) {
    Group group = this.registry.get().getGroup();
    List<Node> candidates = new ArrayList<>(group.getMembership().getMembers());
    candidates.retainAll(nodes);
    // Only run election on a single node
    if (candidates.isEmpty() || candidates.get(0).equals(group.getLocalMember())) {
        // First validate that quorum was met
        int size = candidates.size();
        boolean quorumMet = size >= this.quorum;
        if ((this.quorum > 1) && (size == this.quorum)) {
            // Log fragility of singleton availability
            ClusteringServerLogger.ROOT_LOGGER.quorumJustReached(this.name.getCanonicalName(), this.quorum);
        }
        Node elected = quorumMet ? this.electionPolicy.elect(candidates) : null;
        try {
            if (elected != null) {
                // Stop service on every node except elected node
                for (Map.Entry<Node, CompletionStage<Void>> entry : this.dispatcher.executeOnGroup(new StopCommand(), elected).entrySet()) {
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                        ClusteringServerLogger.ROOT_LOGGER.tracef("Singleton service %s is not installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                    } catch (CompletionException e) {
                        Throwable cause = e.getCause();
                        if ((cause instanceof IllegalStateException) && (cause.getCause() instanceof ServiceNotFoundException)) {
                            ClusteringServerLogger.ROOT_LOGGER.debugf("Singleton service %s is no longer installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                        } else {
                            throw e;
                        }
                    }
                }
                // Start service on elected node
                try {
                    this.dispatcher.executeOnMember(new StartCommand(), elected).toCompletableFuture().join();
                } catch (CancellationException e) {
                    ClusteringServerLogger.ROOT_LOGGER.debugf("Singleton service %s could not be started on the elected primary singleton provider (%s) because it left the cluster.  A new primary provider election will take place.", this.name.getCanonicalName(), elected.getName());
                } catch (CompletionException e) {
                    Throwable cause = e.getCause();
                    if ((cause instanceof IllegalStateException) && (cause.getCause() instanceof ServiceNotFoundException)) {
                        ClusteringServerLogger.ROOT_LOGGER.debugf("Service % is no longer installed on the elected primary singleton provider (%s). A new primary provider election will take place.", this.name.getCanonicalName(), elected.getName());
                    } else {
                        throw e;
                    }
                }
            } else {
                if (!quorumMet) {
                    ClusteringServerLogger.ROOT_LOGGER.quorumNotReached(this.name.getCanonicalName(), this.quorum);
                }
                // Stop service on every node
                for (Map.Entry<Node, CompletionStage<Void>> entry : this.dispatcher.executeOnGroup(new StopCommand()).entrySet()) {
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                        ClusteringServerLogger.ROOT_LOGGER.tracef("Singleton service %s is not installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                    } catch (CompletionException e) {
                        Throwable cause = e.getCause();
                        if ((cause instanceof IllegalStateException) && (cause.getCause() instanceof ServiceNotFoundException)) {
                            ClusteringServerLogger.ROOT_LOGGER.debugf("Singleton service %s is no longer installed on %s", this.name.getCanonicalName(), entry.getKey().getName());
                        } else {
                            throw e;
                        }
                    }
                }
            }
            if (this.electionListener != null) {
                for (CompletionStage<Void> stage : this.dispatcher.executeOnGroup(new SingletonElectionCommand(candidates, elected)).values()) {
                    try {
                        stage.toCompletableFuture().join();
                    } catch (CancellationException e) {
                    // Ignore
                    }
                }
            }
        } catch (CommandDispatcherException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : Group(org.wildfly.clustering.group.Group) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node) ArrayList(java.util.ArrayList) CancellationException(java.util.concurrent.CancellationException) ServiceNotFoundException(org.jboss.msc.service.ServiceNotFoundException) CompletionException(java.util.concurrent.CompletionException) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Aggregations

ServiceNotFoundException (org.jboss.msc.service.ServiceNotFoundException)5 Reference (javax.naming.Reference)2 ModularReference (org.jboss.as.naming.context.ModularReference)2 ServiceName (org.jboss.msc.service.ServiceName)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionException (java.util.concurrent.CompletionException)1 CompletionStage (java.util.concurrent.CompletionStage)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LifecycleListener (org.jboss.msc.service.LifecycleListener)1 Mode (org.jboss.msc.service.ServiceController.Mode)1 StabilityMonitor (org.jboss.msc.service.StabilityMonitor)1 StartException (org.jboss.msc.service.StartException)1 Test (org.junit.Test)1 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)1 Group (org.wildfly.clustering.group.Group)1 Node (org.wildfly.clustering.group.Node)1 CountDownLifecycleListener (org.wildfly.clustering.service.CountDownLifecycleListener)1