Search in sources :

Example 21 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class LifecycleAwareConfigurationInstance method testConnectivity.

private void testConnectivity() throws MuleException {
    ConnectionProvider provider = connectionProvider.get();
    if (provider instanceof NoConnectivityTest) {
        return;
    }
    Scheduler retryScheduler = schedulerService.ioScheduler();
    RetryPolicyTemplate retryTemplate = connectionManager.getRetryTemplateFor(provider);
    ReconnectionConfig reconnectionConfig = connectionManager.getReconnectionConfigFor(provider);
    RetryCallback retryCallback = new RetryCallback() {

        @Override
        public void doWork(RetryContext context) throws Exception {
            Lock lock = testConnectivityLock;
            if (lock != null) {
                final boolean lockAcquired = lock.tryLock();
                if (lockAcquired) {
                    LOGGER.info("Doing testConnectivity() for config " + getName());
                    try {
                        ConnectionValidationResult result = connectionManager.testConnectivity(LifecycleAwareConfigurationInstance.this);
                        if (result.isValid()) {
                            context.setOk();
                        } else {
                            if ((reconnectionConfig.isFailsDeployment())) {
                                context.setFailed(result.getException());
                                throw new ConnectionException(format("Connectivity test failed for config '%s'", getName()), result.getException());
                            } else {
                                if (LOGGER.isInfoEnabled()) {
                                    LOGGER.info(format("Connectivity test failed for config '%s'. Application deployment will continue. Error was: ", getName(), result.getMessage()), result.getException());
                                }
                            }
                        }
                    } finally {
                        lock.unlock();
                    }
                } else {
                    LOGGER.warn("There is a testConnectivity() already running for config " + getName());
                }
            }
        }

        @Override
        public String getWorkDescription() {
            return format("Testing connectivity for config '%s'", getName());
        }

        @Override
        public Object getWorkOwner() {
            return value;
        }
    };
    try {
        retryTemplate.execute(retryCallback, retryScheduler);
    } catch (Exception e) {
        throw new DefaultMuleException(createStaticMessage(format("Could not perform connectivity testing for config '%s'", getName())), e);
    } finally {
        if (retryScheduler != null) {
            retryScheduler.stop();
        }
    }
}
Also used : RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) Scheduler(org.mule.runtime.api.scheduler.Scheduler) RetryContext(org.mule.runtime.core.api.retry.RetryContext) ReconnectionConfig(org.mule.runtime.core.internal.retry.ReconnectionConfig) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) ConnectionProvider(org.mule.runtime.api.connection.ConnectionProvider) Lock(java.util.concurrent.locks.Lock) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ConnectionValidationResult(org.mule.runtime.api.connection.ConnectionValidationResult) NoConnectivityTest(org.mule.runtime.extension.api.connectivity.NoConnectivityTest) ConnectionException(org.mule.runtime.api.connection.ConnectionException) RetryCallback(org.mule.runtime.core.api.retry.RetryCallback)

Example 22 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class SourceConfigurer method configure.

/**
 * Performs the configuration of the given {@code source} and returns the result
 *
 * @param source a {@link Source}
 * @param config the {@link ConfigurationInstance config instance} associated to {@code this} source object.
 * @return the configured instance
 * @throws MuleException
 */
public Source configure(Source source, Optional<ConfigurationInstance> config) throws MuleException {
    ResolverSetBasedObjectBuilder<Source> builder = new ResolverSetBasedObjectBuilder<Source>(source.getClass(), model, resolverSet) {

        @Override
        protected Source instantiateObject() {
            return source;
        }

        @Override
        public Source build(ValueResolvingContext context) throws MuleException {
            Source source = build(resolverSet.resolve(context));
            injectDefaultEncoding(model, source, muleContext.getConfiguration().getDefaultEncoding());
            injectComponentLocation(source, componentLocation);
            config.ifPresent(c -> injectRefName(source, c.getName(), getReflectionCache()));
            return source;
        }
    };
    CoreEvent initialiserEvent = null;
    try {
        initialiserEvent = getInitialiserEvent(muleContext);
        Source configuredSource = builder.build(from(initialiserEvent, config));
        if (configuredSource instanceof PollingSource) {
            Scheduler scheduler = (Scheduler) resolverSet.getResolvers().get(SCHEDULING_STRATEGY_PARAMETER_NAME).resolve(ValueResolvingContext.from(initialiserEvent));
            configuredSource = new PollingSourceWrapper((PollingSource) configuredSource, scheduler);
        }
        return configuredSource;
    } catch (Exception e) {
        throw new MuleRuntimeException(createStaticMessage("Exception was found trying to configure source of type " + source.getClass().getName()), e);
    } finally {
        if (initialiserEvent != null) {
            ((BaseEventContext) initialiserEvent.getContext()).success();
        }
    }
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ResolverSetBasedObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ResolverSetBasedObjectBuilder) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) Source(org.mule.runtime.extension.api.runtime.source.Source) PollingSourceWrapper(org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper) MuleException(org.mule.runtime.api.exception.MuleException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 23 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class SourceAdapter method start.

@Override
public void start() throws MuleException {
    injectComponentLocation();
    try {
        setConfiguration(configurationInstance);
        setConnection();
        muleContext.getInjector().inject(sourceInvokationTarget.get());
        if (source instanceof SourceWrapper) {
            muleContext.getInjector().inject(source);
        }
        source.onStart(createSourceCallback());
    } catch (Exception e) {
        throw new DefaultMuleException(e);
    }
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) PollingSourceWrapper(org.mule.runtime.module.extension.internal.runtime.source.poll.PollingSourceWrapper) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) TransactionException(org.mule.runtime.api.tx.TransactionException) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 24 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class ExtensionConnectivityTestingStrategyTestCase method connectionProviderThrowsException.

@Test
public void connectionProviderThrowsException() throws MuleException {
    final Exception e = new RuntimeException();
    when(connectionProviderResolver.resolve(any())).thenThrow(e);
    ConnectionValidationResult connectionResult = extensionConnectivityTestingStrategy.testConnectivity(connectionProviderResolver);
    assertThat(connectionResult.isValid(), is(false));
    assertThat(connectionResult.getException(), is(sameInstance(e)));
}
Also used : ConnectionValidationResult(org.mule.runtime.api.connection.ConnectionValidationResult) MuleException(org.mule.runtime.api.exception.MuleException) Test(org.junit.Test)

Example 25 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class ExtensionMessageSourceTestCase method failToStart.

@Test
public void failToStart() throws Exception {
    MuleException e = new DefaultMuleException(new Exception());
    doThrow(e).when(source).onStart(any());
    expectedException.expect(is(instanceOf(RetryPolicyExhaustedException.class)));
    messageSource.initialise();
    messageSource.start();
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) LifecycleException(org.mule.runtime.api.lifecycle.LifecycleException) IOException(java.io.IOException) RetryPolicyExhaustedException(org.mule.runtime.core.api.retry.policy.RetryPolicyExhaustedException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) Test(org.junit.Test)

Aggregations

MuleException (org.mule.runtime.api.exception.MuleException)68 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)21 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)19 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)19 Test (org.junit.Test)15 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)14 Processor (org.mule.runtime.core.api.processor.Processor)12 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)9 Logger (org.slf4j.Logger)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)6 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)6 Map (java.util.Map)5 ConnectionException (org.mule.runtime.api.connection.ConnectionException)5 MuleContext (org.mule.runtime.core.api.MuleContext)5 LifecycleUtils.stopIfNeeded (org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded)5 Thread.currentThread (java.lang.Thread.currentThread)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Startable (org.mule.runtime.api.lifecycle.Startable)4