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

com.github.dreamhead.moco.action.MocoAsyncAction Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.action;

import com.github.dreamhead.moco.MocoConfig;
import com.github.dreamhead.moco.MocoEventAction;
import com.github.dreamhead.moco.Request;
import com.github.dreamhead.moco.procedure.LatencyProcedure;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class MocoAsyncAction implements MocoEventAction {
    private final MocoEventAction action;
    private final LatencyProcedure procedure;
    private final ExecutorService service = Executors.newCachedThreadPool();

    public MocoAsyncAction(final MocoEventAction action, final LatencyProcedure procedure) {
        this.action = action;
        this.procedure = procedure;
    }

    @Override
    public void execute(final Request request) {
        service.execute(new Runnable() {
            @Override
            public void run() {
                procedure.execute();
                action.execute(request);
            }
        });
    }

    @Override
    public MocoEventAction apply(final MocoConfig config) {
        MocoEventAction appliedAction = this.action.apply(config);
        if (this.action != appliedAction) {
            return new MocoAsyncAction(appliedAction, procedure);
        }

        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy