use of org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT in project mule by mulesoft.
the class PolicyNextActionMessageProcessor method apply.
@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
return from(publisher).doOnNext(coreEvent -> logExecuteNextEvent("Before execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId())).flatMap(event -> {
Processor nextOperation = policyStateHandler.retrieveNextOperation(event.getContext().getCorrelationId());
if (nextOperation == null) {
return error(new MuleRuntimeException(createStaticMessage("There's no next operation configured for event context id " + event.getContext().getCorrelationId())));
}
popBeforeNextFlowFlowStackElement().accept(event);
notificationHelper.notification(BEFORE_NEXT).accept(event);
return from(processWithChildContext(event, nextOperation, ofNullable(getLocation()))).doOnSuccessOrError(notificationHelper.successOrErrorNotification(AFTER_NEXT).andThen((ev, t) -> pushAfterNextFlowStackElement().accept(event))).onErrorResume(MessagingException.class, t -> {
PolicyStateId policyStateId = new PolicyStateId(event.getContext().getCorrelationId(), muleContext.getConfiguration().getId());
policyStateHandler.getLatestState(policyStateId).ifPresent(latestStateEvent -> t.setProcessedEvent(policyEventConverter.createEvent((PrivilegedEvent) t.getEvent(), (PrivilegedEvent) latestStateEvent)));
// Given we've used child context to ensure AFTER_NEXT notifications are fired at exactly the right time we need
// to propagate the error to parent context manually.
((BaseEventContext) event.getContext()).error(resolveMessagingException(t.getFailingComponent(), muleContext).apply(t));
return empty();
}).doOnNext(coreEvent -> logExecuteNextEvent("After execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId()));
});
}
use of org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT in project mule by mulesoft.
the class ApplicationPolicyDeploymentTestCase method appliesApplicationPolicyWithNotificationListener.
@Test
public void appliesApplicationPolicyWithNotificationListener() throws Exception {
policyManager.registerPolicyTemplate(fooPolicyFileBuilder.getArtifactFile());
ApplicationFileBuilder applicationFileBuilder = createExtensionApplicationWithServices(APP_WITH_EXTENSION_PLUGIN_CONFIG, helloExtensionV1Plugin);
addPackedAppFromBuilder(applicationFileBuilder);
startDeployment();
assertApplicationDeploymentSuccess(applicationDeploymentListener, applicationFileBuilder.getId());
List<Integer> notificationListenerActionIds = new ArrayList<>();
PolicyNotificationListener<PolicyNotification> notificationListener = notification -> notificationListenerActionIds.add(notification.getAction().getActionId());
policyManager.addPolicy(applicationFileBuilder.getId(), fooPolicyFileBuilder.getArtifactId(), new PolicyParametrization(FOO_POLICY_ID, pointparameters -> true, 1, singletonMap(POLICY_PROPERTY_KEY, POLICY_PROPERTY_VALUE), getResourceFile("/fooPolicy.xml"), singletonList(notificationListener)));
executeApplicationFlow("main");
assertThat(invocationCount, equalTo(1));
new PollingProber(POLICY_NOTIFICATION_TIMEOUT, 100).check(new JUnitProbe() {
@Override
protected boolean test() throws Exception {
assertThat(notificationListenerActionIds, hasSize(4));
assertThat(notificationListenerActionIds, hasItems(PROCESS_START, BEFORE_NEXT, AFTER_NEXT, PROCESS_END));
return true;
}
});
}
Aggregations