@Override protected String execute(String input, ActivityExecutionContext context) throws ActivityFailureException, CancellationException { Object[] inputParameters = converter.fromData(input, Object[].class); CurrentActivityExecutionContext.set(context); Object result = null; ActivityExecutionContext executionContext = contextProvider.getActivityExecutionContext(); String taskToken = executionContext.getTaskToken(); try { result = swfWorkflowConsumer.processActivity(inputParameters, taskToken); } catch (InvocationTargetException invocationException) { throwActivityFailureException(invocationException.getTargetException() != null ? invocationException.getTargetException() : invocationException); } catch (IllegalArgumentException illegalArgumentException) { throwActivityFailureException(illegalArgumentException); } catch (IllegalAccessException illegalAccessException) { throwActivityFailureException(illegalAccessException); } catch (Exception e) { throwActivityFailureException(e); } finally { CurrentActivityExecutionContext.unset(); } String resultSerialized = converter.toData(result); return resultSerialized; }
@Override public void delayActivity(long time) { calls.add("delayActivity"); ActivityExecutionContextProvider provider = new ActivityExecutionContextProviderImpl(); ActivityExecutionContext context = provider.getActivityExecutionContext(); try { while (true) { Thread.sleep(time * 1000); context.recordActivityHeartbeat(null); } } catch (InterruptedException e) { } }
/** * Heartbeat every 5 minutes. It is not a good idea to heartbeat too * frequently as each noteActivityProgress event ends up eating history * events count. * * @return time of the last heartbeat */ private long heartbeat(long lastHeartbeatTime, int progress) { if (System.currentTimeMillis() - lastHeartbeatTime > HEARTBEAT_INTERVAL) { ActivityExecutionContext context = contextProvider.getActivityExecutionContext(); context.recordActivityHeartbeat(Integer.toString((progress))); lastHeartbeatTime = System.currentTimeMillis(); } return lastHeartbeatTime; }
@Override @ManualActivityCompletion public String humanActivity() { ActivityExecutionContext executionContext = contextProvider.getActivityExecutionContext(); String taskToken = executionContext.getTaskToken(); System.out.println("Task received, completion token: " + taskToken); //This will not be returned to the caller return null; }
/** * Fail in 20% of invocations to demonstrate retry logic */ @Override public void doSomeWork(String parameter) { Random r = new Random(); if (r.nextInt(100) < 20) { throw new RuntimeException("simulated exception to force retry"); } ActivityExecutionContext context = contextProvider.getActivityExecutionContext(); ActivityTask task = context.getTask(); String taskid = task.getActivityId(); System.out.println("Processed activity task with id: " + taskid); }
/** * */ @Override public void doSomeWork(String parameter) { ActivityExecutionContext context = contextProvider.getActivityExecutionContext(); ActivityTask task = context.getTask(); String taskid = task.getActivityId(); System.out.println("Processed activity task with id: " + taskid); }
@Override public void doSomeWork(String parameter) { // Reset counter on the new run which changes when workflow continues as new ActivityExecutionContext activityExecutionContext = contextProvider.getActivityExecutionContext(); WorkflowExecution workflowExecution = activityExecutionContext.getWorkflowExecution(); String runId = workflowExecution.getRunId(); if (this.runId == null || !runId.equals(this.runId)) { runCount++; } this.runId = runId; workCount++; if (workCount % 2 != 0) { throw new RuntimeException("simulated failure to cause retry"); } }
@Override public void doSomeWork(String parameter) { // Reset counter on the new run which changes when workflow continues as new ActivityExecutionContext activityExecutionContext = contextProvider.getActivityExecutionContext(); WorkflowExecution workflowExecution = activityExecutionContext.getWorkflowExecution(); String runId = workflowExecution.getRunId(); if (this.runId == null || !runId.equals(this.runId)) { runCount++; } this.runId = runId; workCount++; }
@Override public void doSomeWork(String parameter) { // Reset counter on the new run which changes when workflow continues as new ActivityExecutionContext activityExecutionContext = contextProvider.getActivityExecutionContext(); WorkflowExecution workflowExecution = activityExecutionContext.getWorkflowExecution(); String runId = workflowExecution.getRunId(); if (this.runId != null && !runId.equals(this.runId)) { currentRunWorkCount = 0; } this.runId = runId; workCount++; currentRunWorkCount++; }