-   Notifications  You must be signed in to change notification settings 
- Fork 5.8k
Add AWS Step Functions Java V2 Examples #1615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
     Merged  
 Changes from all commits
 Commits 
  Show all changes 
  28 commits   Select commit Hold shift + click to select a range 
 18ec77d  Create Readme.md 
  scmacdon 14016d8  Add files via upload 
  scmacdon 45e0463  Create CreateStateMachine.java 
  scmacdon baf5a20  Add files via upload 
  scmacdon 3bdf54d  Update Readme.md 
  scmacdon ddb8627  Update Readme.md 
  scmacdon 3dc7593  Add files via upload 
  scmacdon 7303a29  Create StepFunctionsTest.java 
  scmacdon 0ffd24c  Create config.properties 
  scmacdon fccbde5  Update Readme.md 
  scmacdon a96add5  Create GetObjectContentType.java 
  scmacdon e98174e  Update metadata.yaml 
  scmacdon 8f961bc  Update Readme.md 
  scmacdon 7455916  Add files via upload 
  scmacdon 5bb9838  Update metadata.yaml 
  scmacdon 77cc048  Update pom.xml 
  scmacdon 6b88ebd  Add files via upload 
  scmacdon bc471b6  Update StepFunctionsTest.java 
  scmacdon df2c8c2  Update CreateStateMachine.java 
  scmacdon ffd90d3  Update Readme.md 
  scmacdon d9011ee  Update CreateStateMachine.java 
  scmacdon f468479  Update Readme.md 
  scmacdon 053951a  Update Readme.md 
  scmacdon a9f99df  Update Readme.md 
  scmacdon 96ccded  Update Readme.md 
  scmacdon 186d32c  Add files via upload 
  scmacdon 05d7aa9  Add files via upload 
  scmacdon 697172a  Add files via upload 
  scmacdon File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        78 changes: 78 additions & 0 deletions  78   javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] | ||
| // snippet-sourcedescription:[GetObjectContentType.java demonstrates how to determine the content type of an object in an Amazon Simple Storage Service (Amazon S3) bucket.] | ||
| //snippet-keyword:[AWS SDK for Java v2] | ||
| //snippet-keyword:[Code Sample] | ||
| //snippet-service:[Amazon S3] | ||
| //snippet-sourcetype:[full-example] | ||
| //snippet-sourcedate:[01/06/2021] | ||
| //snippet-sourceauthor:[scmacdon-aws] | ||
|  | ||
| /* | ||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package com.example.s3; | ||
|  | ||
| // snippet-start:[s3.java2.getobjectcontenttype.import] | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.*; | ||
| // snippet-end:[s3.java2.getobjectcontenttype.import] | ||
|  | ||
| /** | ||
| * To run this AWS code example, ensure that you have setup your development environment, including your AWS credentials. | ||
| * | ||
| * For information, see this documentation topic: | ||
| * | ||
| * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
| */ | ||
|  | ||
| public class GetObjectContentType { | ||
|  | ||
| public static void main(String[] args) { | ||
|  | ||
| final String USAGE = "\n" + | ||
| "Usage:\n" + | ||
| " GetObjectContentType <bucketName> <keyName>>\n\n" + | ||
| "Where:\n" + | ||
| " bucketName - the Amazon S3 bucket name. \n\n"+ | ||
| " keyName - the key name. \n\n"; | ||
|  | ||
| if (args.length != 2) { | ||
| System.out.println(USAGE); | ||
| System.exit(1); | ||
| } | ||
|  | ||
| String bucketName = args[0]; | ||
| String keyName = args[1]; | ||
|  | ||
| Region region = Region.US_WEST_2; | ||
| S3Client s3 = S3Client.builder() | ||
| .region(region) | ||
| .build(); | ||
|  | ||
| getContentType(s3,bucketName,keyName); | ||
| s3.close(); | ||
| } | ||
|  | ||
| // snippet-start:[s3.java2.getobjectcontenttype.main] | ||
| public static void getContentType (S3Client s3, String bucketName, String keyName) { | ||
|  | ||
| try { | ||
| HeadObjectRequest objectRequest = HeadObjectRequest.builder() | ||
| .key(keyName) | ||
| .bucket(bucketName) | ||
| .build(); | ||
|  | ||
| HeadObjectResponse objectHead = s3.headObject(objectRequest); | ||
| String type = objectHead.contentType(); | ||
| System.out.println("The object content type is "+type); | ||
|  | ||
| } catch (S3Exception e) { | ||
| System.err.println(e.awsErrorDetails().errorMessage()); | ||
| System.exit(1); | ||
| } | ||
| // snippet-end:[s3.java2.getobjectcontenttype.main] | ||
| } | ||
| } | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # AWS Step Functions Java code examples | ||
|  | ||
| This README discusses how to run and test the Java code examples for Step Functions. | ||
|  | ||
| ## Running the Step Functions Java files | ||
|  | ||
| **IMPORTANT** | ||
|  | ||
| The Java code examples perform AWS operations for the account and AWS Region for which you've specified credentials, and you may incur AWS service charges by running them. See the [AWS Pricing page](https://aws.amazon.com/pricing/) for details about the charges you can expect for a given service and operation. | ||
|  | ||
| Some of these examples perform *destructive* operations on AWS resources, such as deleting a state machine. **Be very careful** when running an operation that deletes or modifies AWS resources in your account. It's best to create separate test-only resources when experimenting with these examples. | ||
|  | ||
| To run these examples, you can setup your development environment to use Apache Maven or Gradle to configure and build AWS SDK for Java projects. For more information, | ||
| see [Get started with the AWS SDK for Java 2.x](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html). | ||
|  | ||
|  | ||
| ## Testing the Step Functions Java files | ||
|  | ||
| You can test the Java code examples for Step Functions by running a test file named **StepFunctionsTest**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/java** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/). | ||
|  | ||
| You can run the JUnit tests from a Java IDE, such as IntelliJ, or from the command line by using Maven. As each test is executed, you can view messages that inform you if the various tests succeed or fail. For example, the following message informs you that Test 3 passed. | ||
|  | ||
| Test 3 passed | ||
|  | ||
| **WARNING**: _Running these JUnit tests manipulates real Amazon resources and may incur charges on your account._ | ||
|  | ||
| ### Properties file | ||
| Before running the Step Functions JUnit tests, you must define values in the **config.properties** file located in the **resources** folder. This file contains values that are required to run the JUnit tests. For example, you define an alarm name for various tests. If you do not define all values, the JUnit tests fail. | ||
|  | ||
| Define these values to successfully run the JUnit tests: | ||
|  | ||
| - **jsonFile** - A JSON file that contains the values to pass to the worflow and used in the **StartExecution** test. | ||
|  | ||
| - **jsonFileSM** – A JSON file that represents the Amazon States Language definition of the state machine and used in the **CreateStateMachine** test. | ||
|  | ||
| - **roleARN** – The Amazon Resource Name (ARN) of the AWS Identity and Access Management (IAM) role to use for this state machine and used in the **CreateStateMachine** test. | ||
|  | ||
| - **stateMachineName** - The name of the state machine to create and used in the **CreateStateMachine** test. | ||
|  | ||
| ### Command line | ||
|  | ||
| To run the JUnit tests from the command line, you can use the following command. | ||
|  | ||
| mvn test | ||
|  | ||
| You will see output from the JUnit tests, as shown here. | ||
|  | ||
| [INFO] ------------------------------------------------------- | ||
| [INFO] T E S T S | ||
| [INFO] ------------------------------------------------------- | ||
| [INFO] Running StepFunctionsTest | ||
| Test 1 passed | ||
| Test 2 passed | ||
| ... | ||
| Done! | ||
| [INFO] Results: | ||
| [INFO] | ||
| [INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0 | ||
| [INFO] | ||
| INFO] -------------------------------------------- | ||
| [INFO] BUILD SUCCESS | ||
| [INFO]-------------------------------------------- | ||
| [INFO] Total time: 12.003 s | ||
| [INFO] Finished at: 2020-02-10T14:25:08-05:00 | ||
| [INFO] -------------------------------------------- | ||
|  | ||
| ### Unsuccessful tests | ||
|  | ||
| If you do not define the correct values in the properties file, your JUnit tests are not successful. You will see an error message such as the following. You need to double-check the values that you set in the properties file and run the tests again. | ||
|  | ||
| [INFO] | ||
| [INFO] -------------------------------------- | ||
| [INFO] BUILD FAILURE | ||
| [INFO] -------------------------------------- | ||
| [INFO] Total time: 19.038 s | ||
| [INFO] Finished at: 2020-02-10T14:41:51-05:00 | ||
| [INFO] --------------------------------------- | ||
| [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project S3J2Project: There are test failures. | ||
| [ERROR]; | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| files: | ||
| - path: src/main/java/com/example/stepfunctions/CreateStateMachine.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/main/java/com/example/stepfunctions/DeleteStateMachine.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/main/java/com/example/stepfunctions/GetExecutionHistory.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/main/java/com/example/stepfunctions/ListActivities.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/main/java/com/example/stepfunctions/GetFailedExecutions.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/main/java/com/example/stepfunctions/ListStateMachines.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/main/java/com/example/stepfunctions/StartExecution.java | ||
| services: | ||
| - stepfunctions | ||
| - path: src/test/java/StepFunctionsTest.java | ||
| services: | ||
| - stepfunctions | ||
|  | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|  | ||
| <groupId>StepFunctionsV2</groupId> | ||
| <artifactId>StepFunctionsV2</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <java.version>1.8</java.version> | ||
| </properties> | ||
|  | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.1</version> | ||
| <configuration> | ||
| <source>${java.version}</source> | ||
| <target>${java.version}</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>bom</artifactId> | ||
| <version>2.15.66</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <version>5.4.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>5.4.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.platform</groupId> | ||
| <artifactId>junit-platform-commons</artifactId> | ||
| <version>1.4.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.platform</groupId> | ||
| <artifactId>junit-platform-launcher</artifactId> | ||
| <version>1.4.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-log4j12</artifactId> | ||
| <version>1.7.25</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.googlecode.json-simple</groupId> | ||
| <artifactId>json-simple</artifactId> | ||
| <version>1.1.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>sfn</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> | 
   104 changes: 104 additions & 0 deletions  104   ...xample_code/stepfunctions/src/main/java/com/example/stepfunctions/CreateStateMachine.java          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //snippet-sourcedescription:[CreateStateMachine.java demonstrates how to creates a state machine for AWS Step Functions.] | ||
| //snippet-keyword:[AWS SDK for Java v2] | ||
| //snippet-keyword:[Code Sample] | ||
| //snippet-service:[AWS Step Functions] | ||
| //snippet-sourcetype:[full-example] | ||
| //snippet-sourcedate:[01/28/2021] | ||
| //snippet-sourceauthor:[scmacdon-AWS] | ||
|  | ||
| /* | ||
| Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|  | ||
| package com.example.stepfunctions; | ||
|  | ||
| // snippet-start:[stepfunctions.java2.create_machine.import] | ||
| import org.json.simple.JSONObject; | ||
| import org.json.simple.parser.JSONParser; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.sfn.SfnClient; | ||
| import software.amazon.awssdk.services.sfn.model.CreateStateMachineRequest; | ||
| import software.amazon.awssdk.services.sfn.model.StateMachineType; | ||
| import software.amazon.awssdk.services.sfn.model.CreateStateMachineResponse; | ||
| import software.amazon.awssdk.services.sfn.model.SfnException; | ||
| import java.io.FileReader; | ||
| import java.io.IOException; | ||
| // snippet-end:[stepfunctions.java2.create_machine.import] | ||
|  | ||
| /** | ||
| * To run this example, you need a JSON file that represents the Amazon States Language definition for the state machine. | ||
| * | ||
| * To see an Amazon States Language definition example that you can use in this code example, see | ||
| * "Getting started with AWS Step Functions" at https://docs.aws.amazon.com/step-functions/latest/dg/getting-started.html. | ||
| */ | ||
|  | ||
| public class CreateStateMachine { | ||
|  | ||
| public static void main(String[] args) { | ||
| final String USAGE = "\n" + | ||
| "Usage:\n" + | ||
| " CreateStateMachine <jsonFile> <roleARN> <stateMachineName>\n\n" + | ||
| "Where:\n" + | ||
| " jsonFile - A JSON file that represents the Amazon States Language definition of the state machine.\n\n" + | ||
| " roleARN - The Amazon Resource Name (ARN) of the AWS Identity and Access Management (IAM) role to use for this state machine.\n" + | ||
| " stateMachineName - The name of the state machine to create.\n"; | ||
|  | ||
| if (args.length != 3) { | ||
| System.out.println(USAGE); | ||
| System.exit(1); | ||
| } | ||
|  | ||
| String jsonFile = args[0]; | ||
| String roleARN = args[1]; | ||
| String stateMachineName = args[2]; | ||
|  | ||
| Region region = Region.US_EAST_1; | ||
| SfnClient sfnClient = SfnClient.builder() | ||
| .region(region) | ||
| .build(); | ||
|  | ||
| String arnStateMachine = createMachine(sfnClient, roleARN, stateMachineName, jsonFile); | ||
| System.out.println("The ARN of the new state machine is "+arnStateMachine); | ||
| sfnClient.close(); | ||
| } | ||
|  | ||
| // snippet-start:[stepfunctions.java2.create_machine.main] | ||
| public static String createMachine( SfnClient sfnClient, String roleARN, String stateMachineName, String jsonFile) { | ||
|  | ||
| String json = getJSONString(jsonFile); | ||
| try { | ||
|  | ||
| CreateStateMachineRequest machineRequest = CreateStateMachineRequest.builder() | ||
| .definition(json) | ||
| .name(stateMachineName) | ||
| .roleArn(roleARN) | ||
| .type(StateMachineType.STANDARD) | ||
| .build(); | ||
|  | ||
| CreateStateMachineResponse response = sfnClient.createStateMachine(machineRequest); | ||
| return response.stateMachineArn(); | ||
|  | ||
| } catch (SfnException e) { | ||
| System.err.println(e.awsErrorDetails().errorMessage()); | ||
| System.exit(1); | ||
| } | ||
| return ""; | ||
| } | ||
|  | ||
| private static String getJSONString(String path) { | ||
| try { | ||
|  | ||
| JSONParser parser = new JSONParser(); | ||
| JSONObject data = (JSONObject) parser.parse(new FileReader(path));//path to the JSON file. | ||
| String json = data.toJSONString(); | ||
| return json; | ||
| } catch (IOException | org.json.simple.parser.ParseException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return ""; | ||
| } | ||
| // snippet-end:[stepfunctions.java2.create_machine.main] | ||
| } | ||
|  | ||
|  | ||
  Oops, something went wrong.  
  Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the most interesting part of this example is what exactly is in the JSON file. Instead of an argument, can it be created in the code? It can be as simple as the one in the Getting Started docs:
{ "Comment": "A Hello World example of the Amazon States Language using Pass states", "StartAt": "Hello", "States": { "Hello": { "Type": "Pass", "Result": "Hello", "Next": "World" }, "World": { "Type": "Pass", "Result": "World", "End": true } } }There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referenced clearly where the user can get a JSON file for use with this example.