All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetlinks.rule.engine.defaults.FunctionTaskExecutor Maven / Gradle / Ivy

The newest version!
package org.jetlinks.rule.engine.defaults;

import lombok.Getter;
import org.jetlinks.core.trace.TraceHolder;
import org.jetlinks.core.utils.Reactors;
import org.jetlinks.rule.engine.api.RuleConstants;
import org.jetlinks.rule.engine.api.RuleData;
import org.jetlinks.rule.engine.api.task.ExecutionContext;
import org.jetlinks.rule.engine.api.task.Task;
import org.jetlinks.rule.engine.api.task.TaskExecutor;
import org.reactivestreams.Publisher;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;

public abstract class FunctionTaskExecutor extends AbstractTaskExecutor implements TaskExecutor {

    @Getter
    private final String name;

    public FunctionTaskExecutor(String name, ExecutionContext context) {
        super(context);
        this.name = name;
    }

    protected abstract Publisher apply(RuleData input);

    private Mono doApply(RuleData input) {
        return context
            .getOutput()
            .write(Flux.from(this.apply(input))
                       .flatMap(output -> context
                           .fireEvent(RuleConstants.Event.result, output)
                           .thenReturn(output)))
            .then(context.fireEvent(RuleConstants.Event.complete, input))
            .as(tracer())
            .onErrorResume(error -> context.onError(error, input))
            .contextWrite(TraceHolder.readToContext(Context.empty(), input.getHeaders()))
            .then();
    }

    @Override
    public final Mono execute(RuleData ruleData) {
        return doApply(ruleData);
    }

    @Override
    protected Disposable doStart() {
        return context
            .getInput()
            .accept(data -> {
                if (state != Task.State.running) {
                    return Mono.empty();
                }
                return this
                    .doApply(data)
                    .then(Reactors.ALWAYS_TRUE);
            })
            ;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy