package com.jenjinstudios.io.concurrency;

import com.jenjinstudios.io.ExecutionContext;
import com.jenjinstudios.io.Message;

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

/**
 * Executes ExecutableMessage objects which have been read.
 *
 * @author Caleb Brinkman
 */
public class ExecutionTask<T extends ExecutionContext> implements Runnable
{
    private final MessageQueue messageQueue;
    private final T executionContext;
    private final Collection<Consumer<T>> contextualTasks;

    /**
     * Construct a new ExecuteTask that will execute messages from the given MessageQueue.
     *
     * @param messageQueue The MessageQueue.
     * @param executionContext The context in which messages should execute.
     * @param contextualTasks Tasks which should be invoked in synchronous fashion with the execution context.
     */
    public ExecutionTask(MessageQueue messageQueue, T executionContext, Collection<Consumer<T>> contextualTasks)
    {
        this.messageQueue = messageQueue;
        this.executionContext = executionContext;
        this.contextualTasks = contextualTasks;
    }

    @Override
    public void run() {
        final List<Message> incoming = messageQueue.getIncomingAndClear();
        incoming.forEach(message -> {
            Message response = message.execute(executionContext);
            if (response != null) {
                messageQueue.queueOutgoing(response);
            }
            contextualTasks.forEach(consumer -> consumer.accept(executionContext));
        });
    }
}

@@ -118,16 +118,73 @@
 Message;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -440,16 +440,103 @@
 nable%0A%7B%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(ExecutionTask.class);%0A
     priv
@@ -1434,16 +1434,165 @@
 ge -%3E %7B%0A
+            if (LOGGER.isDebugEnabled()) %7B%0A                LOGGER.debug(%22Executing message (Type: %7B%7D)%22, message.getClass().getName());%0A            %7D%0A
         
@@ -1800,16 +1800,176 @@
 sumer -%3E
+ %7B%0A                if (LOGGER.isDebugEnabled()) %7B%0A                    LOGGER.debug(%22Executing contextual task: %7B%7D%22, consumer);%0A                %7D%0A               
  consume
@@ -1994,16 +1994,31 @@
 Context)
+;%0A            %7D
 );%0A     

Add logging to ExecutionTask