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

com.microsoft.durabletask.TaskActivityExecutor Maven / Gradle / Ivy

Go to download

This package contains classes and interfaces for building Durable Task orchestrations in Java.

There is a newer version: 1.5.0
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.durabletask;

import java.util.HashMap;
import java.util.logging.Logger;

final class TaskActivityExecutor {
    private final HashMap activityFactories;
    private final DataConverter dataConverter;
    private final Logger logger;

    public TaskActivityExecutor(
            HashMap activityFactories,
            DataConverter dataConverter,
            Logger logger) {
        this.activityFactories = activityFactories;
        this.dataConverter = dataConverter;
        this.logger = logger;
    }

    public String execute(String taskName, String input, int taskId) throws Throwable {
        TaskActivityFactory factory = this.activityFactories.get(taskName);
        if (factory == null) {
            throw new IllegalStateException(
                    String.format("No activity task named '%s' is registered.", taskName));
        }
        
        TaskActivity activity = factory.create();
        if (activity == null) {
            throw new IllegalStateException(
                    String.format("The task factory '%s' returned a null TaskActivity object.", taskName));
        }

        TaskActivityContextImpl context = new TaskActivityContextImpl(taskName, input);

        // Unhandled exceptions are allowed to escape
        Object output = activity.run(context);
        if (output != null) {
            return this.dataConverter.serialize(output);
        }

        return null;
    }

    private class TaskActivityContextImpl implements TaskActivityContext {
        private final String name;
        private final String rawInput;

        private final DataConverter dataConverter = TaskActivityExecutor.this.dataConverter;

        public TaskActivityContextImpl(String activityName, String rawInput) {
            this.name = activityName;
            this.rawInput = rawInput;
        }

        @Override
        public String getName() {
            return this.name;
        }

        @Override
        public  T getInput(Class targetType) {
            if (this.rawInput == null) {
                return null;
            }

            return this.dataConverter.deserialize(this.rawInput, targetType);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy