private void handleSuccess(CoreEvent childEvent) { Result result = childEvent != null ? EventedResult.from(childEvent) : Result.builder().build(); try { successHandler.accept(result); } catch (Throwable error) { errorHandler.accept(error, result); } }
@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); } }
public static <T, A> EventedResult<T, A> from(CoreEvent event) { return new EventedResult<>(event); }
@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)); }
private CoreEvent handleError(Throwable error, CoreEvent childEvent) { try { errorHandler.accept(error, EventedResult.from(childEvent)); } catch (Throwable e) { ((BaseEventContext) originalEvent.getContext()).error(e); } return null; }
public static <T, A> EventedResult<T, A> from(CoreEvent event) { return new EventedResult<>(event); }
@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)); }
private void handleSuccess(CoreEvent childEvent) { Result result = childEvent != null ? EventedResult.from(childEvent) : Result.builder().build(); try { successHandler.accept(result); } catch (Throwable error) { errorHandler.accept(error, result); } }
/** * 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(); } }
private CoreEvent handleError(Throwable error, CoreEvent childEvent) { try { errorHandler.accept(error, EventedResult.from(childEvent)); } catch (Throwable e) { ((BaseEventContext) originalEvent.getContext()).error(e); } return null; }
@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); } }
@Override public SpyResult spy(CoreEvent event) { final SpyResult spyResult = new SpyResult(); Latch latch = new Latch(); processors.process(EventedResult.from(event), result -> { spyResult.setResult(result); latch.release(); }, (error, result) -> { spyResult.setThrowable(error); spyResult.setResult(result); latch.release(); }); try { latch.await(); } catch (InterruptedException e) { throw new MunitError(e); } return spyResult; }
/** * 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(); } }