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();
}
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 };
}
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 };
}
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]"));
}
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));
}
Aggregations