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.worker;
 
 
 
 
 public class DecisionTaskPoller implements TaskPoller {
 
     private static final Log log = LogFactory.getLog(DecisionTaskPoller.class);
 
     private static final Log decisionsLog = LogFactory.getLog(DecisionTaskPoller.class.getName() + ".decisions");
 
     private class DecisionTaskIterator implements Iterator<DecisionTask> {
 
         private final DecisionTask firstDecisionTask;
 
         private DecisionTask next;
 
         public DecisionTaskIterator() {
              =  = poll(null);
         }
 
         @Override
         public boolean hasNext() {
             return  != null;
         }
 
         @Override
         public DecisionTask next() {
             if (!hasNext()) {
                 throw new IllegalStateException("hasNext() == false");
             }
             DecisionTask result = ;
             if (.getNextPageToken() == null) {
                  = null;
             }
             else {
                  = poll(.getNextPageToken());
                 // Just to not keep around the history page
                 if ( != result) {
                     .setEvents(null);
                 }
             }
             return result;
         }
 
         @Override
         public void remove() {
             throw new UnsupportedOperationException();
         }
 
         public DecisionTask getFirstDecisionTask() {
             return ;
         }
 
     }
 
     private AmazonSimpleWorkflow service;
 
     private String domain;
 
     private String taskListToPoll;
 
     private String identity;
 
     private boolean validated;
 
 
     public DecisionTaskPoller() {
          = ManagementFactory.getRuntimeMXBean().getName();
     }
 
     public DecisionTaskPoller(AmazonSimpleWorkflow serviceString domainString taskListToPoll,
             DecisionTaskHandler decisionTaskHandler) {
        this. = service;
        this. = domain;
        this. = taskListToPoll;
        this. = decisionTaskHandler;
         = ManagementFactory.getRuntimeMXBean().getName();
    }
    public String getIdentity() {
        return ;
    }
    public void setIdentity(String identity) {
         = false;
        this. = identity;
    }
    public AmazonSimpleWorkflow getService() {
        return ;
    }
    public String getDomain() {
        return ;
    }
        return ;
    }
    public void setDecisionTaskHandler(DecisionTaskHandler decisionTaskHandler) {
         = false;
        this. = decisionTaskHandler;
    }
    public void setService(AmazonSimpleWorkflow service) {
         = false;
        this. = service;
    }
    public void setDomain(String domain) {
        this. = domain;
    }
    public String getTaskListToPoll() {
        return ;
    }
    public void setTaskListToPoll(String pollTaskList) {
        this. = pollTaskList;
    }

    
Poll for a task using getPollTimeoutInSeconds()

Parameters:
nextResultToken
Returns:
null if poll timed out
Throws:
DeciderExecutorConfigurationException
    private DecisionTask poll(String nextResultToken) {
        validate();
        PollForDecisionTaskRequest pollRequest = new PollForDecisionTaskRequest();
        pollRequest.setDomain();
        pollRequest.setIdentity();
        pollRequest.setNextPageToken(nextResultToken);
        pollRequest.setTaskList(new TaskList().withName());
        if (.isDebugEnabled()) {
            .debug("poll request begin: " + pollRequest);
        }
        DecisionTask result = .pollForDecisionTask(pollRequest);
        if (.isDebugEnabled()) {
            .debug("poll request returned decision task: workflowType=" + result.getWorkflowType() + ", workflowExecution="
                    + result.getWorkflowExecution() + ", startedEventId=" + result.getStartedEventId() + ", previousStartedEventId=" + result.getPreviousStartedEventId());
        }
        if (result == null || result.getTaskToken() == null) {
            return null;
        }
        return result;
    }

    
Poll for a workflow task and call appropriate decider. This method might call the service multiple times to retrieve the whole history it it is paginated.

Returns:
true if task was polled and decided upon, false if poll timed out
Throws:
java.lang.Exception
DeciderConfigurationException
    @Override
    public boolean pollAndProcessSingleTask() throws Exception {
        DecisionTaskIterator tasks = null;
        RespondDecisionTaskCompletedRequest taskCompletedRequest = null;
        try {
            tasks = new DecisionTaskIterator();
            if (!tasks.hasNext()) {
                return false;
            }
            taskCompletedRequest = .handleDecisionTask(tasks);
            if (.isTraceEnabled()) {
                .trace(WorkflowExecutionUtils.prettyPrintDecisions(taskCompletedRequest.getDecisions()));
            }
            .respondDecisionTaskCompleted(taskCompletedRequest);
        }
        catch (Exception e) {
            if (tasks != null) {
                DecisionTask firstTask = tasks.getFirstDecisionTask();
                if (firstTask != null) {
                    if (.isWarnEnabled()) {
                        .warn("DecisionTask failure: taskId= " + firstTask.getStartedEventId() + ", workflowExecution="
                                + firstTask.getWorkflowExecution(), e);
                    }
                    if (.isDebugEnabled() && firstTask.getEvents() != null) {
                        .debug("Failed taskId=" + firstTask.getStartedEventId() + " history: "
                                + WorkflowExecutionUtils.prettyPrintHistory(firstTask.getEvents(), true));
                    }
                }
                if (taskCompletedRequest != null && .isWarnEnabled()) {
                    .warn("Failed taskId=" + firstTask.getStartedEventId() + " decisions="
                            + WorkflowExecutionUtils.prettyPrintDecisions(taskCompletedRequest.getDecisions()));
                }
            }
            throw e;
        }
        return true;
    }

    

Parameters:
seconds
Returns:
    private void validate() throws IllegalStateException {
        if () {
            return;
        }
        checkFieldSet("decisionTaskHandler");
        checkFieldSet("service");
        checkFieldSet("identity");
         = true;
    }
    private void checkFieldSet(String fieldNameObject fieldValuethrows IllegalStateException {
        if (fieldValue == null) {
            throw new IllegalStateException("Required field " + fieldName + " is not set");
        }
    }
    protected void checkFieldNotNegative(String fieldNamelong fieldValuethrows IllegalStateException {
        if (fieldValue < 0) {
            throw new IllegalStateException("Field " + fieldName + " is negative");
        }
    }
    @Override
    public void shutdown() {
    }
    @Override
    public void shutdownNow() {
    }
    @Override
    public boolean awaitTermination(long leftTimeUnit millisecondsthrows InterruptedException {
        //TODO: Waiting for all currently running pollAndProcessSingleTask to complete 
        return false;
    }
New to GrepCode? Check out our FAQ X