Search in sources :

Example 6 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class Foreach method splitAndProcess.

private Publisher<CoreEvent> splitAndProcess(CoreEvent request) {
    AtomicInteger count = new AtomicInteger();
    final AtomicReference<CoreEvent> currentEvent = new AtomicReference<>(request);
    // Split into sequence of TypedValue
    return fromIterable(() -> splitRequest(request)).onErrorMap(throwable -> new MessagingException(request, throwable, Foreach.this)).transform(p -> batchSize > 1 ? from(p).buffer(batchSize).map(list -> new TypedValue<>(list, fromObject(list))) : p).flatMapSequential(typedValue -> {
        EventContext parentContext = currentEvent.get().getContext();
        BaseEventContext childContext = newChildContext(currentEvent.get(), ofNullable(getLocation()));
        Builder partEventBuilder = builder(childContext, currentEvent.get());
        if (typedValue.getValue() instanceof EventBuilderConfigurer) {
            // Support EventBuilderConfigurer currently used by Batch Module
            EventBuilderConfigurer configurer = (EventBuilderConfigurer) typedValue.getValue();
            configurer.configure(partEventBuilder);
            childContext.onResponse((e, t) -> {
                configurer.eventCompleted();
            });
        } else if (typedValue.getValue() instanceof Message) {
            // If value is a Message then use it directly conserving attributes and properties.
            partEventBuilder.message((Message) typedValue.getValue());
        } else {
            // Otherwise create a new message
            partEventBuilder.message(Message.builder().payload(typedValue).build());
        }
        return Mono.from(just(partEventBuilder.addVariable(counterVariableName, count.incrementAndGet()).build()).transform(nestedChain).doOnNext(completeSuccessIfNeeded(childContext, true)).switchIfEmpty(Mono.from(childContext.getResponsePublisher())).map(result -> builder(parentContext, result).build()).doOnNext(result -> currentEvent.set(CoreEvent.builder(result).build())).doOnError(MessagingException.class, me -> me.setProcessedEvent(builder(parentContext, me.getEvent()).build())).doOnSuccess(result -> {
            if (result == null) {
                childContext.success();
            }
        }));
    }, // Force sequential execution of the chain for each element
    1).switchIfEmpty(defer(() -> {
        if (count.get() == 0) {
            logger.warn("Split expression returned no results. If this is not expected please check your expression");
            return just(request);
        } else {
            return empty();
        }
    })).takeLast(1).map(s -> CoreEvent.builder(currentEvent.get()).message(request.getMessage()).build()).errorStrategyStop();
}
Also used : MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Message(org.mule.runtime.api.message.Message) Flux.from(reactor.core.publisher.Flux.from) Mono.defer(reactor.core.publisher.Mono.defer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Iterators(com.google.common.collect.Iterators) Collections.singletonList(java.util.Collections.singletonList) Scope(org.mule.runtime.core.privileged.processor.Scope) MuleException(org.mule.runtime.api.exception.MuleException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProcessors.newChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.newChildContext) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) Map(java.util.Map) Mono.just(reactor.core.publisher.Mono.just) AbstractMessageProcessorOwner(org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Mono.empty(reactor.core.publisher.Mono.empty) DEFAULT_SPLIT_EXPRESSION(org.mule.runtime.core.internal.routing.ExpressionSplittingStrategy.DEFAULT_SPLIT_EXPRESSION) EventContext(org.mule.runtime.api.event.EventContext) Iterator(java.util.Iterator) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Flux.fromIterable(reactor.core.publisher.Flux.fromIterable) Optional.ofNullable(java.util.Optional.ofNullable) MessageProcessors.completeSuccessIfNeeded(org.mule.runtime.core.privileged.processor.MessageProcessors.completeSuccessIfNeeded) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) EventBuilderConfigurerList(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerList) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) Optional(java.util.Optional) EventBuilderConfigurerIterator(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerIterator) MessageProcessors.getProcessingStrategy(org.mule.runtime.core.privileged.processor.MessageProcessors.getProcessingStrategy) EventContext(org.mule.runtime.api.event.EventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Message(org.mule.runtime.api.message.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) AtomicReference(java.util.concurrent.atomic.AtomicReference) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 7 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class EventContextBenchmark method createEventContextWithFlowAndCompleteWithExternalCompletion.

@Benchmark
public Object[] createEventContextWithFlowAndCompleteWithExternalCompletion() {
    CompletableFuture<Void> completableFuture = new CompletableFuture<>();
    AtomicReference<CoreEvent> result = new AtomicReference();
    AtomicBoolean complete = new AtomicBoolean();
    BaseEventContext eventContext = (BaseEventContext) create(flow, CONNECTOR_LOCATION, null, of(completableFuture));
    from(from(eventContext.getResponsePublisher())).doOnSuccess(response -> result.set(response)).subscribe();
    eventContext.onTerminated((response, throwable) -> complete.set(true));
    eventContext.success(event);
    completableFuture.complete(null);
    return new Object[] { result, complete };
}
Also used : Measurement(org.openjdk.jmh.annotations.Measurement) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Warmup(org.openjdk.jmh.annotations.Warmup) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) NullExceptionHandler.getInstance(org.mule.runtime.core.api.exception.NullExceptionHandler.getInstance) Mono.from(reactor.core.publisher.Mono.from) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) TearDown(org.openjdk.jmh.annotations.TearDown) EventContext(org.mule.runtime.api.event.EventContext) Setup(org.openjdk.jmh.annotations.Setup) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) UUID(org.mule.runtime.core.api.util.UUID) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) Flow(org.mule.runtime.core.api.construct.Flow) Benchmark(org.openjdk.jmh.annotations.Benchmark) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 8 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class EventContextBenchmark method createEventContextWithFlowAndComplete.

@Benchmark
public Object[] createEventContextWithFlowAndComplete() {
    AtomicReference<CoreEvent> result = new AtomicReference();
    AtomicBoolean complete = new AtomicBoolean();
    BaseEventContext eventContext = (BaseEventContext) create(flow, CONNECTOR_LOCATION);
    from(from(eventContext.getResponsePublisher())).doOnSuccess(response -> result.set(response)).subscribe();
    eventContext.onTerminated((response, throwable) -> complete.set(true));
    eventContext.success(event);
    return new Object[] { result, complete };
}
Also used : Measurement(org.openjdk.jmh.annotations.Measurement) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Message(org.mule.runtime.api.message.Message) Optional.of(java.util.Optional.of) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Warmup(org.openjdk.jmh.annotations.Warmup) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) NullExceptionHandler.getInstance(org.mule.runtime.core.api.exception.NullExceptionHandler.getInstance) Mono.from(reactor.core.publisher.Mono.from) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) TearDown(org.openjdk.jmh.annotations.TearDown) EventContext(org.mule.runtime.api.event.EventContext) Setup(org.openjdk.jmh.annotations.Setup) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) UUID(org.mule.runtime.core.api.util.UUID) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) Flow(org.mule.runtime.core.api.construct.Flow) Benchmark(org.openjdk.jmh.annotations.Benchmark) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) LegacyRegistryUtils.lookupObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.lookupObject) LegacyRegistryUtils.registerObject(org.mule.runtime.core.privileged.registry.LegacyRegistryUtils.registerObject) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 9 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class MessageChunkAggregatorTestCase method testMessageProcessor.

@Test
public void testMessageProcessor() throws Exception {
    MuleSession session = new DefaultMuleSession();
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    MessageChunkAggregator router = new MessageChunkAggregator();
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    Message message1 = of("test event A");
    Message message2 = of("test event B");
    Message message3 = of("test event C");
    EventContext context = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    CoreEvent event1 = InternalEvent.builder(context).message(message1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 3))).session(session).build();
    CoreEvent event2 = InternalEvent.builder(context).message(message2).session(session).build();
    CoreEvent event3 = InternalEvent.builder(context).message(message3).session(session).build();
    assertNull(router.process(event1));
    assertNull(router.process(event2));
    CoreEvent resultEvent = router.process(event3);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    String payload = getPayloadAsString(resultMessage);
    assertTrue(payload.contains("test event A"));
    assertTrue(payload.contains("test event B"));
    assertTrue(payload.contains("test event C"));
    assertTrue(payload.matches("test event [A,B,C]test event [A,B,C]test event [A,B,C]"));
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Example 10 with EventContext

use of org.mule.runtime.api.event.EventContext in project mule by mulesoft.

the class ResequencerTestCase method testMessageResequencerWithComparator.

@Test
public void testMessageResequencerWithComparator() throws Exception {
    MuleSession session = new DefaultMuleSession();
    Flow flow = createFlow(muleContext, "test");
    assertNotNull(flow);
    when(componentLocator.find(any(Location.class))).thenReturn(of(flow));
    TestEventResequencer router = new TestEventResequencer(3);
    Map<QName, Object> fakeComponentLocationAnnotations = ImmutableMap.<QName, Object>builder().put(LOCATION_KEY, fromSingleComponent("test")).build();
    router.setAnnotations(fakeComponentLocationAnnotations);
    initialiseIfNeeded(router, true, muleContext);
    EventContext context = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    Message message1 = Message.of("test event A");
    Message message2 = Message.of("test event B");
    Message message3 = Message.of("test event C");
    CoreEvent event1 = InternalEvent.builder(context).message(message1).session(session).build();
    CoreEvent event2 = InternalEvent.builder(context).message(message2).session(session).build();
    CoreEvent event3 = InternalEvent.builder(context).message(message3).session(session).build();
    // set a resequencing comparator. We need to reset the router since it will
    // not process the same event group
    // twice
    router = new TestEventResequencer(3);
    router.setMuleContext(muleContext);
    router.setEventComparator(new EventPayloadComparator());
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    assertNull(router.process(event2));
    assertNull(router.process(event3));
    CoreEvent resultEvent = router.process(event1);
    assertNotNull(resultEvent);
    Message resultMessage = resultEvent.getMessage();
    assertNotNull(resultMessage);
    assertEquals("test event C", getPayloadAsString(resultMessage));
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) QName(javax.xml.namespace.QName) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) MuleTestUtils.createFlow(org.mule.tck.MuleTestUtils.createFlow) Location(org.mule.runtime.api.component.location.Location) Test(org.junit.Test)

Aggregations

EventContext (org.mule.runtime.api.event.EventContext)12 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)12 Message (org.mule.runtime.api.message.Message)11 Flow (org.mule.runtime.core.api.construct.Flow)9 Test (org.junit.Test)7 MuleTestUtils.createAndRegisterFlow (org.mule.tck.MuleTestUtils.createAndRegisterFlow)7 List (java.util.List)4 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)4 DefaultMuleSession (org.mule.runtime.core.privileged.event.DefaultMuleSession)4 MuleSession (org.mule.runtime.core.privileged.event.MuleSession)4 ArrayList (java.util.ArrayList)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Collectors.toList (java.util.stream.Collectors.toList)3 MuleException (org.mule.runtime.api.exception.MuleException)3 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)3 Optional.of (java.util.Optional.of)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 NANOSECONDS (java.util.concurrent.TimeUnit.NANOSECONDS)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 SchedulerService (org.mule.runtime.api.scheduler.SchedulerService)2