Skip to content

Commit 141cf68

Browse files
committed
Idempotency module
1 parent 9639d9e commit 141cf68

33 files changed

+3376
-0
lines changed

powertools-idempotency/pom.xml

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>software.amazon.lambda</groupId>
9+
<artifactId>powertools-parent</artifactId>
10+
<version>1.10.2</version>
11+
</parent>
12+
13+
<artifactId>powertools-idempotency</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<name>AWS Lambda Powertools Java library Idempotency</name>
17+
<description>
18+
19+
</description>
20+
<url>https://aws.amazon.com/lambda/</url>
21+
<issueManagement>
22+
<system>GitHub Issues</system>
23+
<url>https://github.com/awslabs/aws-lambda-powertools-java/issues</url>
24+
</issueManagement>
25+
<scm>
26+
<url>https://github.com/awslabs/aws-lambda-powertools-java.git</url>
27+
</scm>
28+
<developers>
29+
<developer>
30+
<name>AWS Lambda Powertools team</name>
31+
<organization>Amazon Web Services</organization>
32+
<organizationUrl>https://aws.amazon.com/</organizationUrl>
33+
</developer>
34+
</developers>
35+
36+
<distributionManagement>
37+
<snapshotRepository>
38+
<id>ossrh</id>
39+
<url>https://aws.oss.sonatype.org/content/repositories/snapshots</url>
40+
</snapshotRepository>
41+
</distributionManagement>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>software.amazon.lambda</groupId>
46+
<artifactId>powertools-core</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>software.amazon.lambda</groupId>
50+
<artifactId>powertools-utilities</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.amazonaws</groupId>
54+
<artifactId>aws-lambda-java-core</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>software.amazon.awssdk</groupId>
58+
<artifactId>dynamodb</artifactId>
59+
<exclusions>
60+
<exclusion>
61+
<groupId>software.amazon.awssdk</groupId>
62+
<artifactId>netty-nio-client</artifactId>
63+
</exclusion>
64+
<exclusion>
65+
<groupId>software.amazon.awssdk</groupId>
66+
<artifactId>apache-client</artifactId>
67+
</exclusion>
68+
</exclusions>
69+
</dependency>
70+
<dependency>
71+
<groupId>software.amazon.awssdk</groupId>
72+
<artifactId>dynamodb-enhanced</artifactId>
73+
<exclusions>
74+
<exclusion>
75+
<groupId>software.amazon.awssdk</groupId>
76+
<artifactId>netty-nio-client</artifactId>
77+
</exclusion>
78+
<exclusion>
79+
<groupId>software.amazon.awssdk</groupId>
80+
<artifactId>apache-client</artifactId>
81+
</exclusion>
82+
</exclusions>
83+
</dependency>
84+
<dependency>
85+
<groupId>software.amazon.awssdk</groupId>
86+
<artifactId>url-connection-client</artifactId>
87+
<version>${aws.sdk.version}</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.aspectj</groupId>
91+
<artifactId>aspectjrt</artifactId>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.apache.logging.log4j</groupId>
95+
<artifactId>log4j-slf4j-impl</artifactId>
96+
</dependency>
97+
98+
<!-- Test dependencies -->
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter-api</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
<dependency>
105+
<groupId>org.junit.jupiter</groupId>
106+
<artifactId>junit-jupiter-engine</artifactId>
107+
<scope>test</scope>
108+
</dependency>
109+
<dependency>
110+
<groupId>org.junit-pioneer</groupId>
111+
<artifactId>junit-pioneer</artifactId>
112+
<version>1.5.0</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.mockito</groupId>
117+
<artifactId>mockito-core</artifactId>
118+
<scope>test</scope>
119+
</dependency>
120+
<dependency>
121+
<groupId>org.mockito</groupId>
122+
<artifactId>mockito-inline</artifactId>
123+
<scope>test</scope>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.apache.commons</groupId>
127+
<artifactId>commons-lang3</artifactId>
128+
<scope>test</scope>
129+
</dependency>
130+
<dependency>
131+
<groupId>org.assertj</groupId>
132+
<artifactId>assertj-core</artifactId>
133+
<scope>test</scope>
134+
</dependency>
135+
<dependency>
136+
<groupId>com.amazonaws</groupId>
137+
<artifactId>aws-lambda-java-events</artifactId>
138+
<scope>test</scope>
139+
</dependency>
140+
<dependency>
141+
<groupId>com.amazonaws</groupId>
142+
<artifactId>aws-lambda-java-tests</artifactId>
143+
</dependency>
144+
<dependency>
145+
<groupId>com.amazonaws</groupId>
146+
<artifactId>DynamoDBLocal</artifactId>
147+
<version>[1.12,2.0)</version>
148+
<scope>test</scope>
149+
</dependency>
150+
</dependencies>
151+
<build>
152+
<pluginManagement>
153+
<plugins>
154+
<plugin>
155+
<groupId>org.apache.maven.plugins</groupId>
156+
<artifactId>maven-surefire-plugin</artifactId>
157+
<configuration>
158+
<systemPropertyVariables>
159+
<sqlite4java.library.path>${project.build.directory}/native-libs</sqlite4java.library.path>
160+
</systemPropertyVariables>
161+
</configuration>
162+
</plugin>
163+
</plugins>
164+
</pluginManagement>
165+
<plugins>
166+
<plugin>
167+
<groupId>org.apache.maven.plugins</groupId>
168+
<artifactId>maven-dependency-plugin</artifactId>
169+
<executions>
170+
<execution>
171+
<id>copy</id>
172+
<phase>test-compile</phase>
173+
<goals>
174+
<goal>copy-dependencies</goal>
175+
</goals>
176+
<configuration>
177+
<includeScope>test</includeScope>
178+
<includeTypes>so,dll,dylib</includeTypes>
179+
<outputDirectory>${project.build.directory}/native-libs</outputDirectory>
180+
</configuration>
181+
</execution>
182+
</executions>
183+
</plugin>
184+
<plugin>
185+
<groupId>org.apache.maven.plugins</groupId>
186+
<artifactId>maven-jar-plugin</artifactId>
187+
<configuration>
188+
<archive>
189+
<manifestEntries>
190+
<Automatic-Module-Name>software.amazon.awssdk.enhanced.dynamodb</Automatic-Module-Name>
191+
</manifestEntries>
192+
</archive>
193+
</configuration>
194+
</plugin>
195+
</plugins>
196+
</build>
197+
<repositories>
198+
<repository>
199+
<id>dynamodb-local-oregon</id>
200+
<name>DynamoDB Local Release Repository</name>
201+
<url>https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release</url>
202+
</repository>
203+
</repositories>
204+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.idempotency;
15+
16+
public class Constants {
17+
public static final String LAMBDA_FUNCTION_NAME_ENV = "AWS_LAMBDA_FUNCTION_NAME";
18+
public static final String AWS_REGION_ENV = "AWS_REGION";
19+
public static final String IDEMPOTENCY_DISABLED_ENV = "POWERTOOLS_IDEMPOTENCY_DISABLED";
20+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.idempotency;
15+
16+
import com.amazonaws.services.lambda.runtime.Context;
17+
import software.amazon.lambda.powertools.idempotency.persistence.BasePersistenceStore;
18+
19+
/**
20+
* Holds the configuration for idempotency:
21+
* <ul>
22+
* <li>The persistence layer to use for persisting the request and response of the function (mandatory).</li>
23+
* <li>The general configuration for idempotency (optional, see {@link IdempotencyConfig.Builder} methods to see defaults values.</li>
24+
* </ul>
25+
* <br/>
26+
* Use it before the function handler ({@link com.amazonaws.services.lambda.runtime.RequestHandler#handleRequest(Object, Context)})
27+
* get called.
28+
* <br/>
29+
* Example:
30+
* <pre>
31+
* Idempotency.config().withPersistenceStore(...).configure();
32+
* </pre>
33+
*/
34+
public class Idempotency {
35+
private IdempotencyConfig config;
36+
private BasePersistenceStore persistenceStore;
37+
38+
private Idempotency() {
39+
}
40+
41+
public IdempotencyConfig getConfig() {
42+
return config;
43+
}
44+
45+
public BasePersistenceStore getPersistenceStore() {
46+
if (persistenceStore == null) {
47+
throw new IllegalStateException("Persistence Store is null, did you call 'configure()'?");
48+
}
49+
return persistenceStore;
50+
}
51+
52+
private void setConfig(IdempotencyConfig config) {
53+
this.config = config;
54+
}
55+
56+
private void setPersistenceStore(BasePersistenceStore persistenceStore) {
57+
this.persistenceStore = persistenceStore;
58+
}
59+
60+
private static class Holder {
61+
private final static Idempotency instance = new Idempotency();
62+
}
63+
64+
public static Idempotency getInstance() {
65+
return Holder.instance;
66+
}
67+
68+
/**
69+
* Acts like a builder that can be used to configure {@link Idempotency}
70+
*
71+
* @return a new instance of {@link Config}
72+
*/
73+
public static Config config() {
74+
return new Config();
75+
}
76+
77+
public static class Config {
78+
79+
private IdempotencyConfig config;
80+
private BasePersistenceStore store;
81+
82+
/**
83+
* Use this method after configuring persistence layer (mandatory) and idem potency configuration (optional)
84+
*/
85+
public void configure() {
86+
if (store == null) {
87+
throw new IllegalStateException("Persistence Layer is null, configure one with 'withPersistenceStore()'");
88+
}
89+
if (config == null) {
90+
config = IdempotencyConfig.builder().build();
91+
}
92+
Idempotency.getInstance().setConfig(config);
93+
Idempotency.getInstance().setPersistenceStore(store);
94+
}
95+
96+
public Config withPersistenceStore(BasePersistenceStore persistenceStore) {
97+
this.store = persistenceStore;
98+
return this;
99+
}
100+
101+
public Config withConfig(IdempotencyConfig config) {
102+
this.config = config;
103+
return this;
104+
}
105+
}
106+
107+
108+
}

0 commit comments

Comments
 (0)