Search in sources :

Example 1 with Cursor

use of org.mule.runtime.api.streaming.Cursor in project mule by mulesoft.

the class StreamingUtils method withCursoredEvent.

/**
 * Executes the given function {@code f} considering that the given {@code event} might have a {@link CursorProvider} as
 * payload. In that case, this method obtains a cursor from the provider and executes the function.
 * <p>
 * Closing the opened cursor, handling exceptions and return values are all taken care of by this utility method.
 *
 * @param event an {@link CoreEvent}
 * @param f     the function to execute
 * @return the output {@link CoreEvent}
 * @throws MuleException
 */
public static CoreEvent withCursoredEvent(CoreEvent event, CheckedFunction<CoreEvent, CoreEvent> f) throws MuleException {
    if (event.getMessage().getPayload() == null) {
        return event;
    }
    Reference<Throwable> exception = new Reference<>();
    CheckedFunction<CoreEvent, CoreEvent> function = new CheckedFunction<CoreEvent, CoreEvent>() {

        @Override
        public CoreEvent applyChecked(CoreEvent event) throws Throwable {
            return f.apply(event);
        }

        @Override
        public CoreEvent handleException(Throwable throwable) {
            exception.set(unwrap(throwable));
            return null;
        }
    };
    Object payload = event.getMessage().getPayload().getValue();
    CursorProvider cursorProvider = null;
    Cursor cursor = null;
    try {
        if (payload instanceof CursorProvider) {
            cursorProvider = (CursorProvider) payload;
            cursor = cursorProvider.openCursor();
            event = replacePayload(event, cursor);
        }
        CoreEvent value = function.apply(event);
        if (value == null) {
            handlePossibleException(exception);
        } else if (value.getMessage().getPayload().getValue() == cursor) {
            value = replacePayload(value, cursorProvider);
        }
        return value;
    } finally {
        if (cursor != null) {
            closeQuietly(cursor);
        }
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Reference(org.mule.runtime.api.util.Reference) Cursor(org.mule.runtime.api.streaming.Cursor) CheckedFunction(org.mule.runtime.core.api.util.func.CheckedFunction)

Example 2 with Cursor

use of org.mule.runtime.api.streaming.Cursor in project mule by mulesoft.

the class CursorResetInterceptor method before.

@Override
public void before(ExecutionContext<OperationModel> ctx) throws Exception {
    Map<Cursor, Long> cursorPositions = new HashMap<>();
    ctx.getParameters().forEach((key, value) -> {
        if (value instanceof Cursor) {
            final Cursor cursor = (Cursor) value;
            cursorPositions.put(cursor, cursor.getPosition());
        }
    });
    if (!cursorPositions.isEmpty()) {
        ((ExecutionContextAdapter<OperationModel>) ctx).setVariable(CURSOR_POSITIONS, cursorPositions);
    }
}
Also used : HashMap(java.util.HashMap) Cursor(org.mule.runtime.api.streaming.Cursor) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter)

Example 3 with Cursor

use of org.mule.runtime.api.streaming.Cursor in project mule by mulesoft.

the class DefaultStreamingManager method manage.

/**
 * {@inheritDoc}
 */
@Override
public void manage(InputStream stream, CoreEvent creatorEvent) {
    if (stream instanceof Cursor) {
        return;
    }
    final BaseEventContext ctx = ((BaseEventContext) creatorEvent.getContext()).getRootContext();
    ctx.onTerminated((response, throwable) -> closeQuietly(stream));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Cursor(org.mule.runtime.api.streaming.Cursor)

Aggregations

Cursor (org.mule.runtime.api.streaming.Cursor)3 HashMap (java.util.HashMap)1 CursorProvider (org.mule.runtime.api.streaming.CursorProvider)1 Reference (org.mule.runtime.api.util.Reference)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 CheckedFunction (org.mule.runtime.core.api.util.func.CheckedFunction)1 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)1 ExecutionContextAdapter (org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter)1