use of org.mule.runtime.core.api.event.CoreEvent.builder in project mule by mulesoft.
the class SplitterTestCase method assertRouted.
private void assertRouted(Object payload, int count, boolean counted) throws Exception, MuleException {
MuleSession session = new DefaultMuleSession();
Map<String, Serializable> inboundProps = new HashMap<>();
inboundProps.put("inbound1", "1");
inboundProps.put("inbound2", 2);
inboundProps.put("inbound3", session);
Map<String, Serializable> outboundProps = new HashMap<>();
outboundProps.put("outbound1", "3");
outboundProps.put("outbound2", 4);
outboundProps.put("outbound3", session);
Map<String, Object> invocationProps = new HashMap<>();
invocationProps.put("invoke1", "5");
invocationProps.put("invoke2", 6);
invocationProps.put("invoke3", session);
Set<Integer> expectedSequences = new HashSet<>();
for (int i = 1; i <= count; i++) {
expectedSequences.add(i);
}
Message toSplit = InternalMessage.builder().value(payload).inboundProperties(inboundProps).outboundProperties(outboundProps).build();
Splitter splitter = new Splitter();
Grabber grabber = new Grabber();
splitter.setMuleContext(muleContext);
splitter.setListener(grabber);
splitter.initialise();
final Builder eventBuilder = this.<PrivilegedEvent.Builder>getEventBuilder().message(toSplit).session(session);
for (Map.Entry<String, Object> entry : invocationProps.entrySet()) {
eventBuilder.addVariable(entry.getKey(), entry.getValue());
}
CoreEvent event = eventBuilder.build();
splitter.process(event);
List<CoreEvent> splits = grabber.getEvents();
assertEquals(count, splits.size());
Set<Object> actualSequences = new HashSet<>();
assertSplitParts(count, counted, inboundProps, outboundProps, invocationProps, splits, actualSequences);
assertEquals(expectedSequences, actualSequences);
}
use of org.mule.runtime.core.api.event.CoreEvent.builder in project mule by mulesoft.
the class AbstractForkJoinStrategyTestCase method flowVarsMerged.
@Test
@Description("After successful completion of all routes the variables from each route are merged into the result.")
public void flowVarsMerged() throws Throwable {
final String beforeVarName = "before";
final String beforeVarValue = "beforeValue";
final String beforeVar2Name = "before2";
final String beforeVar2Value = "before2Value";
final String beforeVar2NewValue = "before2NewValue";
final String fooVarName = "foo";
final String fooVarValue = "fooValue1";
final String fooVar2Name = "foo2";
final String fooVar2Value1 = "foo2Value1";
final String fooVar2Value2 = "foo2Value2";
final String fooVar3Name = "foo3";
final String fooVar3Value1 = "foo3Value1";
final Apple fooVar3Value2 = new Apple();
CoreEvent original = builder(this.<CoreEvent>newEvent()).addVariable(beforeVarName, beforeVarValue).addVariable(beforeVar2Name, beforeVar2Value).build();
RoutingPair pair1 = of(original, createChain(event -> builder(event).addVariable(beforeVar2Name, beforeVar2NewValue).addVariable(fooVarName, fooVarValue).addVariable(fooVar2Name, fooVar2Value1).addVariable(fooVar3Name, fooVar3Value1).build()));
RoutingPair pair2 = of(original, createChain(event -> builder(event).addVariable(fooVar2Name, fooVar2Value2).addVariable(fooVar3Name, fooVar3Value2).build()));
CoreEvent result = invokeStrategyBlocking(strategy, original, asList(pair1, pair2));
assertThat(result.getVariables().keySet(), hasSize(5));
assertThat(result.getVariables().keySet(), hasItems(beforeVarName, beforeVar2Name, fooVarName, fooVarName, fooVar2Name, fooVar3Name));
assertThat(result.getVariables().get(beforeVarName).getValue(), equalTo(beforeVarValue));
assertThat(result.getVariables().get(beforeVar2Name).getValue(), equalTo(beforeVar2NewValue));
assertThat(result.getVariables().get(fooVarName).getValue(), equalTo(fooVarValue));
TypedValue fooVar2 = result.getVariables().get(fooVar2Name);
assertThat(fooVar2.getDataType(), equalTo(DataType.builder().collectionType(List.class).itemType(String.class).build()));
assertThat(((List<String>) fooVar2.getValue()), hasItems(fooVar2Value1, fooVar2Value2));
TypedValue fooVar3 = result.getVariables().get(fooVar3Name);
assertThat(fooVar3.getDataType(), equalTo(DataType.builder().collectionType(List.class).itemType(Object.class).build()));
assertThat(((List<Object>) fooVar3.getValue()), hasItems(fooVar3Value1, fooVar3Value2));
}
use of org.mule.runtime.core.api.event.CoreEvent.builder in project mule by mulesoft.
the class AbstractExecutableComponent method execute.
@Override
public final CompletableFuture<ExecutionResult> execute(InputEvent inputEvent) {
CompletableFuture completableFuture = new CompletableFuture();
CoreEvent.Builder builder = CoreEvent.builder(createEventContext(of(completableFuture)));
CoreEvent event = builder.message(inputEvent.getMessage()).error(inputEvent.getError().orElse(null)).variables(inputEvent.getVariables()).build();
return from(MessageProcessors.process(event, getExecutableFunction())).onErrorMap(throwable -> {
MessagingException messagingException = (MessagingException) throwable;
CoreEvent messagingExceptionEvent = messagingException.getEvent();
return new ComponentExecutionException(messagingExceptionEvent.getError().get().getCause(), messagingExceptionEvent);
}).<ExecutionResult>map(result -> new ExecutionResultImplementation(result, completableFuture)).toFuture();
}
use of org.mule.runtime.core.api.event.CoreEvent.builder in project mule by mulesoft.
the class AbstractMessageSequenceSplitter method processParts.
protected List<CoreEvent> processParts(MessageSequence<?> seq, CoreEvent originalEvent) throws MuleException {
List<CoreEvent> resultEvents = new ArrayList<>();
int correlationSequence = 0;
MessageSequence<?> messageSequence = seq;
if (batchSize > 1) {
messageSequence = new PartitionedMessageSequence<>(seq, batchSize);
}
Integer count = messageSequence.size();
CoreEvent lastResult = null;
for (; messageSequence.hasNext(); ) {
correlationSequence++;
final Builder builder = builder(originalEvent);
propagateFlowVars(lastResult, builder);
if (counterVariableName != null) {
builder.addVariable(counterVariableName, correlationSequence);
}
builder.groupCorrelation(Optional.of(count != null ? GroupCorrelation.of(correlationSequence, count) : GroupCorrelation.of(correlationSequence)));
Object nextValue = messageSequence.next();
initEventBuilder(nextValue, originalEvent, builder, resolvePropagatedFlowVars(lastResult));
try {
// TODO MULE-13052 Migrate Splitter and Foreach implementation to non-blocking
CoreEvent resultEvent = processToApplyWithChildContext(builder.build(), applyNext());
if (resultEvent != null) {
resultEvents.add(builder(originalEvent.getContext(), resultEvent).build());
lastResult = resultEvent;
}
} catch (MessagingException e) {
if (!filterOnErrorTypeAcceptor.accept(e.getEvent())) {
throw e;
}
} finally {
if (nextValue instanceof EventBuilderConfigurer) {
((EventBuilderConfigurer) nextValue).eventCompleted();
}
}
}
if (correlationSequence == 1) {
logger.debug("Splitter only returned a single result. If this is not expected, please check your split expression");
}
return resultEvents;
}
use of org.mule.runtime.core.api.event.CoreEvent.builder in project mule by mulesoft.
the class Foreach method apply.
@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
return from(publisher).doOnNext(event -> {
if (expression.equals(DEFAULT_SPLIT_EXPRESSION) && Map.class.isAssignableFrom(event.getMessage().getPayload().getDataType().getType())) {
throw new IllegalArgumentException(MAP_NOT_SUPPORTED_MESSAGE);
}
}).flatMap(originalEvent -> {
// Keep reference to existing rootMessage/count variables in order to restore later to support foreach nesting.
final Object previousCounterVar = originalEvent.getVariables().containsKey(counterVariableName) ? originalEvent.getVariables().get(counterVariableName).getValue() : null;
final Object previousRootMessageVar = originalEvent.getVariables().containsKey(rootMessageVariableName) ? originalEvent.getVariables().get(rootMessageVariableName).getValue() : null;
final CoreEvent requestEvent = builder(originalEvent).addVariable(rootMessageVariableName, originalEvent.getMessage()).build();
return Mono.from(splitAndProcess(requestEvent)).map(result -> {
final Builder responseBuilder = builder(result).message(originalEvent.getMessage());
restoreVariables(previousCounterVar, previousRootMessageVar, responseBuilder);
return responseBuilder.build();
}).onErrorMap(MessagingException.class, me -> {
// Restore variables in case of error also
CoreEvent.Builder exceptionEventBuilder = builder(me.getEvent());
restoreVariables(previousCounterVar, previousRootMessageVar, exceptionEventBuilder);
me.setProcessedEvent(exceptionEventBuilder.build());
return me;
});
});
}
Aggregations