AWS Code Sample
Catalog

ActivityWorkerWithGracefulShutdown.java

ActivityWorkerWithGracefulShutdown.java demonstrates how to implement an activity worker with a graceful shutdown.

/* * Copyright 2010-2018 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.example.helloswf; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import software.amazon.awssdk.services.swf.SWFClient; import software.amazon.awssdk.services.swf.model.PollForActivityTaskRequest; import software.amazon.awssdk.services.swf.model.PollForActivityTaskResponse; import software.amazon.awssdk.services.swf.model.RespondActivityTaskCompletedRequest; import software.amazon.awssdk.services.swf.model.RespondActivityTaskFailedRequest; import software.amazon.awssdk.services.swf.model.TaskList; public class ActivityWorkerWithGracefulShutdown { private static final SWFClient swf = SWFClient.builder().build(); private static CountDownLatch waitForTermination = new CountDownLatch(1); private static volatile boolean terminate = false; private static String executeActivityTask(String input) throws Throwable { return "Hello, " + input + "!"; } public static void main(String[] args) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { terminate = true; System.out.println("Waiting for the current poll request" + " to return before shutting down."); waitForTermination.await(60, TimeUnit.SECONDS); } catch (InterruptedException e) { // ignore } } }); try { pollAndExecute(); } finally { waitForTermination.countDown(); } } public static void pollAndExecute() { while (!terminate) { System.out.println("Polling for an activity task from the tasklist '" + HelloTypes.TASKLIST + "' in the domain '" + HelloTypes.DOMAIN + "'."); PollForActivityTaskResponse task = swf.pollForActivityTask(PollForActivityTaskRequest.builder() .domain(HelloTypes.DOMAIN) .taskList(TaskList.builder().name(HelloTypes.TASKLIST).build()) .build()); String taskToken = task.taskToken(); if (taskToken != null) { String result = null; Throwable error = null; try { System.out.println("Executing the activity task with input '" + task.input() + "'."); result = executeActivityTask(task.input()); } catch (Throwable th) { error = th; } if (error == null) { System.out.println("The activity task succeeded with result '" + result + "'."); swf.respondActivityTaskCompleted( RespondActivityTaskCompletedRequest.builder() .taskToken(taskToken) .result(result) .build()); } else { System.out.println("The activity task failed with the error '" + error.getClass().getSimpleName() + "'."); swf.respondActivityTaskFailed( RespondActivityTaskFailedRequest.builder() .taskToken(taskToken) .reason(error.getClass().getSimpleName()) .details(error.getMessage()) .build()); } } } } }

Sample Details

Service: swf

Author: soo-aws

Type: full-example

On this page: