@Override public void process(Result result, Consumer<Result> onSuccess, BiConsumer<Throwable, Result> onError) { if (result instanceof EventedResult) { doProcess(((EventedResult) result).getEvent(), onSuccess, onError); } else { process(result.getOutput(), result.getAttributes(), onSuccess, onError); } }
@Test public void testDoProcessOnErrorGenericException() throws InterruptedException { when(chain.apply(any())).thenReturn(error(new RuntimeException())); ImmutableProcessorChainExecutor chainExecutor = new ImmutableProcessorChainExecutor(coreEvent, chain); AtomicInteger successCalls = new AtomicInteger(0); AtomicInteger errorCalls = new AtomicInteger(0); Reference<Event> errorEvent = new Reference<>(); doProcessAndWait(chainExecutor, r -> successCalls.incrementAndGet(), (t, r) -> { errorCalls.incrementAndGet(); errorEvent.set(((EventedResult) r).getEvent()); }); assertThat(successCalls.get(), is(0)); assertThat(errorCalls.get(), is(1)); assertThat(errorEvent.get().getMessage().getPayload().getValue(), is(TEST_PAYLOAD)); }
@Test public void testDoProcessOnErrorMessagingException() throws InterruptedException, MuleException { final String ERROR_PAYLOAD = "ERROR_PAYLOAD"; when(chain.apply(any())).thenReturn(error(new MessagingException(createStaticMessage(""), getEventBuilder().message(of(ERROR_PAYLOAD)).build()))); ImmutableProcessorChainExecutor chainExecutor = new ImmutableProcessorChainExecutor(coreEvent, chain); AtomicInteger successCalls = new AtomicInteger(0); AtomicInteger errorCalls = new AtomicInteger(0); Reference<Event> errorEvent = new Reference<>(); doProcessAndWait(chainExecutor, r -> successCalls.incrementAndGet(), (t, r) -> { errorCalls.incrementAndGet(); errorEvent.set(((EventedResult) r).getEvent()); }); assertThat(successCalls.get(), is(0)); assertThat(errorCalls.get(), is(1)); assertThat(errorEvent.get().getMessage().getPayload().getValue(), is(ERROR_PAYLOAD)); }
/** * If the {@code value} is a {@link Message}, then a new one is created merging the contents of the returned value with the ones * of the input message. The merging criteria is as follows: * <li> * <ul> * The {@code value}'s payload and DataType is set on the output message * </ul> * <ul> * If the {@code value} has a not {@code null} output for {@link Message#getAttributes()}, then that value is set on the * outbound message. Otherwise, whatever value the input message had is maintained * </ul> * </li> * <p> * If the {@code value} is of any other type, then it's set as the payload of the outgoing message {@inheritDoc} */ @Override public CoreEvent asReturnValue(Object value, ExecutionContextAdapter operationContext) { return value instanceof EventedResult ? ((EventedResult) value).getEvent() : CoreEvent.builder(operationContext.getEvent()) .securityContext(operationContext.getSecurityContext()) .message(toMessage(value, operationContext)).build(); } }
@Override public void process(Result result, Consumer<Result> onSuccess, BiConsumer<Throwable, Result> onError) { if (result instanceof EventedResult) { doProcess(((EventedResult) result).getEvent(), onSuccess, onError); } else { process(result.getOutput(), result.getAttributes(), onSuccess, onError); } }
/** * If the {@code value} is a {@link Message}, then a new one is created merging the contents of the returned value with the ones * of the input message. The merging criteria is as follows: * <li> * <ul> * The {@code value}'s payload and DataType is set on the output message * </ul> * <ul> * If the {@code value} has a not {@code null} output for {@link Message#getAttributes()}, then that value is set on the * outbound message. Otherwise, whatever value the input message had is maintained * </ul> * </li> * <p> * If the {@code value} is of any other type, then it's set as the payload of the outgoing message {@inheritDoc} */ @Override public CoreEvent asReturnValue(Object value, ExecutionContextAdapter operationContext) { return value instanceof EventedResult ? ((EventedResult) value).getEvent() : CoreEvent.builder(operationContext.getEvent()) .securityContext(operationContext.getSecurityContext()) .message(toMessage(value, operationContext)).build(); } }