forked from aws-powertools/powertools-lambda-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdempotency.java
118 lines (100 loc) · 3.79 KB
/
Idempotency.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 software.amazon.lambda.powertools.idempotency;
import com.amazonaws.services.lambda.runtime.Context;
import software.amazon.lambda.powertools.idempotency.persistence.BasePersistenceStore;
/**
* Holds the configuration for idempotency:
* <ul>
* <li>The persistence layer to use for persisting the request and response of the function (mandatory).</li>
* <li>The general configuration for idempotency (optional, see {@link IdempotencyConfig.Builder} methods to see defaults values.</li>
* </ul>
* <br/>
* Use it before the function handler ({@link com.amazonaws.services.lambda.runtime.RequestHandler#handleRequest(Object, Context)})
* get called.
* <br/>
* Example:
* <pre>
* Idempotency.config().withPersistenceStore(...).configure();
* </pre>
*/
public class Idempotency {
private IdempotencyConfig config;
private BasePersistenceStore persistenceStore;
private Idempotency() {
}
public IdempotencyConfig getConfig() {
return config;
}
public BasePersistenceStore getPersistenceStore() {
if (persistenceStore == null) {
throw new IllegalStateException("Persistence Store is null, did you call 'configure()'?");
}
return persistenceStore;
}
private void setConfig(IdempotencyConfig config) {
this.config = config;
}
private void setPersistenceStore(BasePersistenceStore persistenceStore) {
this.persistenceStore = persistenceStore;
}
private static class Holder {
private final static Idempotency instance = new Idempotency();
}
public static Idempotency getInstance() {
return Holder.instance;
}
/**
* Can be used in a method which is not the handler to capture the Lambda context,
* to calculate the remaining time before the invocation times out.
*
* @param lambdaContext
*/
public static void registerLambdaContext(Context lambdaContext) {
getInstance().getConfig().setLambdaContext(lambdaContext);
}
/**
* Acts like a builder that can be used to configure {@link Idempotency}
*
* @return a new instance of {@link Config}
*/
public static Config config() {
return new Config();
}
public static class Config {
private IdempotencyConfig config;
private BasePersistenceStore store;
/**
* Use this method after configuring persistence layer (mandatory) and idem potency configuration (optional)
*/
public void configure() {
if (store == null) {
throw new IllegalStateException("Persistence Layer is null, configure one with 'withPersistenceStore()'");
}
if (config == null) {
config = IdempotencyConfig.builder().build();
}
Idempotency.getInstance().setConfig(config);
Idempotency.getInstance().setPersistenceStore(store);
}
public Config withPersistenceStore(BasePersistenceStore persistenceStore) {
this.store = persistenceStore;
return this;
}
public Config withConfig(IdempotencyConfig config) {
this.config = config;
return this;
}
}
}