Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.simpleworkflow.flow;
 
 import java.util.List;
 
 
 public class WorkflowReplayer<T> {
 
 
         private final T workflowImplementation;
 
         private WorkflowReplayerPOJOFactoryFactory(T workflowImplementationthrows InstantiationExceptionIllegalAccessException {
             this. = workflowImplementation;
             super.addWorkflowImplementationType(workflowImplementation.getClass());
         }
 
         @Override
         protected POJOWorkflowImplementationFactory getImplementationFactory(Class<?> workflowImplementationType,
                 Class<?> workflowIntefaceWorkflowType workflowType) {
             return new POJOWorkflowImplementationFactory() {
 
                 @Override
                 public Object newInstance(DecisionContext decisionContextthrows Exception {
                     return ;
                 }
 
                 @Override
                 public void deleteInstance(Object instance) {
                 }
             };
         }
     }
 
     private abstract class DecisionTaskIterator implements Iterator<DecisionTask> {
 
         private DecisionTask next;
         
         private boolean initialized;
 
         protected void initNext() {
              = true;
              = getNextHistoryTask(null);
         }
 
         @Override
         public boolean hasNext() {
             if (!) {
                 initNext();
             }
             
             if ( == null) {
                 return false;
             }
             List<HistoryEventevents = .getEvents();
             if (events.size() == 0) {
                 return false;
             }
             if ( == 0) {
                 return true;
             }
             HistoryEvent firstEvent = .getEvents().get(0);
             return firstEvent.getEventId() <= ;
         }
 
         @Override
        public DecisionTask next() {
            if (!hasNext()) {
                throw new IllegalStateException("hasNext() == false");
            }
            DecisionTask result = ;
            if (.getNextPageToken() == null) {
                 = null;
            }
            else {
                 = getNextHistoryTask(.getNextPageToken());
            }
            return result;
        }
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
        protected List<HistoryEventtruncateHistory(List<HistoryEventevents) {
            if (events.size() == 0) {
                return null;
            }
            if ( == 0) {
                return events;
            }
            HistoryEvent lastEvent = events.get(events.size() - 1);
            if (lastEvent.getEventId() <= ) {
                return events;
            }
            List<HistoryEventtruncated = new ArrayList<HistoryEvent>();
            for (HistoryEvent event : events) {
                if (event.getEventId() > ) {
                    break;
                }
                truncated.add(event);
            }
            if (truncated.size() == 0) {
                return null;
            }
            return truncated;
        }
        protected abstract DecisionTask getNextHistoryTask(String nextPageToken);
    }
    private class ServiceDecisionTaskIterator extends DecisionTaskIterator {
        private final AmazonSimpleWorkflow service;
        private final String domain;
        private final WorkflowExecution workflowExecution;
        public ServiceDecisionTaskIterator(AmazonSimpleWorkflow serviceString domainWorkflowExecution workflowExecution) {
            this. = service;
            this. = domain;
            this. = workflowExecution;
        }
        protected DecisionTask getNextHistoryTask(String nextPageToken) {
            WorkflowExecutionInfo executionInfo = WorkflowExecutionUtils.describeWorkflowInstance(,
                    );
            History history = WorkflowExecutionUtils.getHistoryPage(nextPageToken);
            DecisionTask task = new DecisionTask();
            List<HistoryEventevents = history.getEvents();
            events = truncateHistory(events);
            if (events == null) {
                return null;
            }
            task.setEvents(events);
            task.setWorkflowExecution();
            task.setWorkflowType(executionInfo.getWorkflowType());
            task.setNextPageToken(history.getNextPageToken());
            return task;
        }
    }
        private final WorkflowExecution workflowExecution;
        private final Iterable<HistoryEventhistory;
        public HistoryIterableDecisionTaskIterator(WorkflowExecution workflowExecutionIterable<HistoryEventhistory) {
            this. = workflowExecution;
            this. = history;
        }
        @Override
        protected DecisionTask getNextHistoryTask(String nextPageToken) {
            DecisionTask result = new DecisionTask();
            Iterator<HistoryEventiterator = .iterator();
            if (!iterator.hasNext()) {
                throw new IllegalStateException("empty history");
            }
            HistoryEvent startEvent = iterator.next();
            WorkflowExecutionStartedEventAttributes startedAttributes = startEvent.getWorkflowExecutionStartedEventAttributes();
            if (startedAttributes == null) {
                throw new IllegalStateException("first event is not WorkflowExecutionStarted: " + startEvent);
            }
            List<HistoryEventevents = new ArrayList<HistoryEvent>();
            events.add(startEvent);
            EventType eventType = null;
            int lastStartedIndex = 0;
            int index = 1;
            long previousStartedEventId = 0;
            long startedEventId = 0;
            while (iterator.hasNext()) {
                HistoryEvent event = iterator.next();
                eventType = EventType.fromValue(event.getEventType());
                events.add(event);
                if (eventType == .) {
                    previousStartedEventId = startedEventId;
                    startedEventId = event.getEventId();
                    lastStartedIndex = index;
                }
                index++;
            }
            if (events.size() > lastStartedIndex + 1) {
                events = events.subList(0, lastStartedIndex + 1);
            }
            result.setEvents(events);
            result.setPreviousStartedEventId(previousStartedEventId);
            result.setStartedEventId(startedEventId);
            result.setWorkflowExecution();
            WorkflowType workflowType = startedAttributes.getWorkflowType();
            result.setWorkflowType(workflowType);
            return result;
        }
    }
    private final Iterator<DecisionTasktaskIterator;
    private final AsyncDecisionTaskHandler taskHandler;
    private int replayUpToEventId;
    public WorkflowReplayer(AmazonSimpleWorkflow serviceString domainWorkflowExecution workflowExecution,
            Class<T> workflowImplementationTypethrows InstantiationExceptionIllegalAccessException {
        ff.addWorkflowImplementationType(workflowImplementationType);
         = new ServiceDecisionTaskIterator(servicedomainworkflowExecution);
         = new AsyncDecisionTaskHandler(ff);
    }
    public WorkflowReplayer(AmazonSimpleWorkflow serviceString domainWorkflowExecution workflowExecution,
            final T workflowImplementationthrows InstantiationExceptionIllegalAccessException {
        WorkflowDefinitionFactoryFactory ff = new WorkflowReplayerPOJOFactoryFactory(workflowImplementation);
         = new ServiceDecisionTaskIterator(servicedomainworkflowExecution);
         = new AsyncDecisionTaskHandler(ff);
    }
    public WorkflowReplayer(AmazonSimpleWorkflow serviceString domainWorkflowExecution workflowExecution,
            WorkflowDefinitionFactoryFactory workflowDefinitionFactoryFactory)
            throws InstantiationExceptionIllegalAccessException {
         = new ServiceDecisionTaskIterator(servicedomainworkflowExecution);
         = new AsyncDecisionTaskHandler(workflowDefinitionFactoryFactory);
    }
    public WorkflowReplayer(Iterable<HistoryEventhistoryWorkflowExecution workflowExecution,
            Class<T> workflowImplementationTypethrows InstantiationExceptionIllegalAccessException {
        ff.addWorkflowImplementationType(workflowImplementationType);
         = new HistoryIterableDecisionTaskIterator(workflowExecutionhistory);
         = new AsyncDecisionTaskHandler(ff);
    }
    public WorkflowReplayer(Iterable<HistoryEventhistoryWorkflowExecution workflowExecutionfinal T workflowImplementation)
            throws InstantiationExceptionIllegalAccessException {
        WorkflowDefinitionFactoryFactory ff = new WorkflowReplayerPOJOFactoryFactory(workflowImplementation);
         = new HistoryIterableDecisionTaskIterator(workflowExecutionhistory);
         = new AsyncDecisionTaskHandler(ff);
    }
    public WorkflowReplayer(Iterable<HistoryEventhistoryWorkflowExecution workflowExecution,
            WorkflowDefinitionFactoryFactory workflowDefinitionFactoryFactory)
            throws InstantiationExceptionIllegalAccessException {
         = new HistoryIterableDecisionTaskIterator(workflowExecutionhistory);
         = new AsyncDecisionTaskHandler(workflowDefinitionFactoryFactory);
    }
    public WorkflowReplayer(Iterator<DecisionTaskdecisionTasksClass<T> workflowImplementationType)
            throws InstantiationExceptionIllegalAccessException {
        ff.addWorkflowImplementationType(workflowImplementationType);
         = decisionTasks;
         = new AsyncDecisionTaskHandler(ff);
    }
    public WorkflowReplayer(Iterator<DecisionTaskdecisionTasksfinal T workflowImplementation)
            throws InstantiationExceptionIllegalAccessException {
        WorkflowDefinitionFactoryFactory ff = new WorkflowReplayerPOJOFactoryFactory(workflowImplementation);
         = decisionTasks;
         = new AsyncDecisionTaskHandler(ff);
    }
    public WorkflowReplayer(Iterator<DecisionTaskdecisionTasks,
            WorkflowDefinitionFactoryFactory workflowDefinitionFactoryFactory)
            throws InstantiationExceptionIllegalAccessException {
         = decisionTasks;
         = new AsyncDecisionTaskHandler(workflowDefinitionFactoryFactory);
    }
    public int getReplayUpToEventId() {
        return ;
    }

    
The replay stops at the event with the given eventId. Default is 0.

Parameters:
replayUpToEventId 0 means the whole history.
    public void setReplayUpToEventId(int replayUpToEventId) {
        this. = replayUpToEventId;
    }
    }
    @SuppressWarnings("unchecked")
    public T loadWorkflow() throws Exception {
        POJOWorkflowDefinition pojoDefinition = (POJOWorkflowDefinitiondefinition;
        return (T) pojoDefinition.getImplementationInstance();
    }
    }
    }
New to GrepCode? Check out our FAQ X