Search in sources :

Example 6 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class CursorManager method manage.

/**
 * Becomes aware of the given {@code provider} and returns a replacement provider which is managed by the runtime, allowing for
 * automatic resource handling
 *
 * @param provider the provider to be tracked
 * @param creatorEvent the event that created the provider
 * @return a {@link CursorContext}
 */
public CursorProvider manage(CursorProvider provider, CoreEvent creatorEvent) {
    final BaseEventContext ownerContext = ((BaseEventContext) creatorEvent.getContext()).getRootContext();
    registerEventContext(ownerContext);
    registry.getUnchecked(ownerContext.getId()).addProvider(provider);
    final CursorContext context = new CursorContext(provider, ownerContext);
    if (provider instanceof CursorStreamProvider) {
        return new ManagedCursorStreamProvider(context, this);
    } else if (provider instanceof CursorIteratorProvider) {
        return new ManagedCursorIteratorProvider(context, this);
    }
    throw new MuleRuntimeException(createStaticMessage("Unknown cursor provider type: " + context.getClass().getName()));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ManagedCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider) CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) ManagedCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.ManagedCursorIteratorProvider) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) ManagedCursorStreamProvider(org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ManagedCursorIteratorProvider(org.mule.runtime.core.internal.streaming.object.ManagedCursorIteratorProvider)

Example 7 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class IdempotentRedeliveryPolicy method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    Optional<Exception> exceptionSeen = empty();
    String messageId = null;
    try {
        messageId = getIdForEvent(event);
    } catch (ExpressionRuntimeException e) {
        logger.warn("The message cannot be processed because the digest could not be generated. Either make the payload serializable or use an expression.");
        return null;
    } catch (Exception ex) {
        exceptionSeen = of(ex);
    }
    Lock lock = lockFactory.createLock(idrId + "-" + messageId);
    lock.lock();
    try {
        RedeliveryCounter counter = findCounter(messageId);
        if (exceptionSeen.isPresent()) {
            throw new MessageRedeliveredException(messageId, counter.counter.get(), maxRedeliveryCount, exceptionSeen.get());
        } else if (counter != null && counter.counter.get() > maxRedeliveryCount) {
            throw new MessageRedeliveredException(messageId, counter.errors, counter.counter.get(), maxRedeliveryCount);
        }
        try {
            CoreEvent returnEvent = processNext(CoreEvent.builder(DefaultEventContext.child((BaseEventContext) event.getContext(), empty()), event).build());
            counter = findCounter(messageId);
            if (counter != null) {
                resetCounter(messageId);
            }
            return returnEvent;
        } catch (Exception ex) {
            if (ex instanceof MessagingException) {
                incrementCounter(messageId, (MessagingException) ex);
                throw ex;
            } else {
                MessagingException me = createMessagingException(event, ex, this);
                incrementCounter(messageId, me);
                throw ex;
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) Lock(java.util.concurrent.locks.Lock)

Example 8 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class TopLevelParameterObjectFactory method doGetObject.

@Override
public Object doGetObject() throws Exception {
    return withContextClassLoader(classLoader, () -> {
        // TODO MULE-10919 - This logic is similar to that of the resolverset object builder and should
        // be generalized
        DefaultObjectBuilder builder = this.builder.get();
        resolveParameters(objectType, builder);
        resolveParameterGroups(objectType, builder);
        injectFields();
        ValueResolver<Object> resolver = new ObjectBuilderValueResolver<>(builder, muleContext);
        if (resolver.isDynamic()) {
            return resolver;
        }
        CoreEvent initialiserEvent = null;
        try {
            initialiserEvent = getInitialiserEvent(muleContext);
            staticProduct = resolver.resolve(from(initialiserEvent));
            muleContext.getInjector().inject(staticProduct);
            return staticProduct;
        } finally {
            if (initialiserEvent != null) {
                ((BaseEventContext) initialiserEvent.getContext()).success();
            }
        }
    }, Exception.class, exception -> {
        throw exception;
    });
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ObjectBuilderValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ObjectBuilderValueResolver) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)

Example 9 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class DefaultEventContextTestCase method componentData.

@Test
@Description("Verify that a location produces connector and source data.")
public void componentData() throws Exception {
    TypedComponentIdentifier typedComponentIdentifier = TypedComponentIdentifier.builder().type(SOURCE).identifier(buildFromStringRepresentation("http:listener")).build();
    ComponentLocation location = mock(ComponentLocation.class);
    when(location.getComponentIdentifier()).thenReturn(typedComponentIdentifier);
    when(location.getParts()).thenReturn(asList(new DefaultLocationPart("flow", empty(), empty(), empty())));
    BaseEventContext context = contextWithComponentLocation.apply(location);
    assertThat(context.getOriginatingLocation().getComponentIdentifier().getIdentifier().getNamespace(), is("http"));
    assertThat(context.getOriginatingLocation().getComponentIdentifier().getIdentifier().getName(), is("listener"));
}
Also used : ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) DefaultLocationPart(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.DefaultLocationPart) TypedComponentIdentifier(org.mule.runtime.api.component.TypedComponentIdentifier) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 10 with BaseEventContext

use of org.mule.runtime.core.privileged.event.BaseEventContext in project mule by mulesoft.

the class DefaultEventContextTestCase method multipleBranches.

@Test
@Description("Parent EventContext only completes once response publisher completes with a value and all child contexts are complete, even if one branch of the tree completes.")
public void multipleBranches() throws Exception {
    BaseEventContext parent = context.get();
    BaseEventContext child1 = child(parent, empty());
    BaseEventContext child2 = child(parent, empty());
    BaseEventContext grandchild1 = child(child1, empty());
    BaseEventContext grandchild2 = child(child1, empty());
    BaseEventContext grandchild3 = child(child2, empty());
    BaseEventContext grandchild4 = child(child2, empty());
    grandchild1.success();
    grandchild2.success();
    assertThat(grandchild1.isTerminated(), is(true));
    assertThat(grandchild2.isTerminated(), is(true));
    assertThat(child1.isTerminated(), is(false));
    assertThat(parent.isTerminated(), is(false));
    child1.success();
    assertThat(child1.isTerminated(), is(true));
    assertThat(parent.isTerminated(), is(false));
    grandchild3.success();
    grandchild4.success();
    child2.success();
    assertThat(grandchild3.isTerminated(), is(true));
    assertThat(grandchild4.isTerminated(), is(true));
    assertThat(child2.isTerminated(), is(true));
    assertThat(parent.isTerminated(), is(false));
    parent.success();
    assertThat(parent.isTerminated(), is(true));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Description(io.qameta.allure.Description) Test(org.junit.Test)

Aggregations

BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)45 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)34 Test (org.junit.Test)24 MuleException (org.mule.runtime.api.exception.MuleException)10 Message (org.mule.runtime.api.message.Message)10 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)8 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)7 Description (io.qameta.allure.Description)6 Optional.of (java.util.Optional.of)6 HeisenbergExtension (org.mule.test.heisenberg.extension.HeisenbergExtension)6 Optional (java.util.Optional)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 EventContext (org.mule.runtime.api.event.EventContext)5 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)5 Publisher (org.reactivestreams.Publisher)5 Mono.from (reactor.core.publisher.Mono.from)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Processor (org.mule.runtime.core.api.processor.Processor)4 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)4 String.format (java.lang.String.format)3