|
| 1 | +--- |
| 2 | +title: "Java" |
| 3 | +weight: 1 |
| 4 | +description: Use the LocalStack SDK for Java |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +You can use the LocalStack SDK for Java to develop Java applications that interact with the LocalStack platform and internal developer endpoints. |
| 10 | +The SDK extends the REST API, offering an object-oriented interface for easier use. |
| 11 | + |
| 12 | +The LocalStack SDK for Java currently supports these features: |
| 13 | + |
| 14 | +- Save, list, load, and delete Cloud Pods. |
| 15 | +- Manage fault configurations for the Chaos API. |
| 16 | + |
| 17 | +{{< callout >}} |
| 18 | +This SDK is still in a preview phase, and will be subject to fast and breaking changes. |
| 19 | +{{< /callout >}} |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +The best way to use the LocalStack SDK for Java in your project is to consume it from Maven Central. |
| 24 | +You can use Maven to import the entire SDK into your project. |
| 25 | + |
| 26 | +```xml |
| 27 | +<dependency> |
| 28 | + <groupId>cloud.localstack</groupId> |
| 29 | + <artifactId>localstack-sdk</artifactId> |
| 30 | + <version>0.0.1</version> |
| 31 | +</dependency> |
| 32 | +``` |
| 33 | + |
| 34 | +Similarly, you can copy the following line in the dependencies block of your `build.gradle(.kts)` file, if you are using Gradle as a build tool. |
| 35 | + |
| 36 | +```kotlin |
| 37 | +implementation("cloud.localstack:localstack-sdk:0.0.1") |
| 38 | +``` |
| 39 | + |
| 40 | +## Quick Start |
| 41 | + |
| 42 | +Currently, the LocalStack SDK for Java only supports Chaos and Cloud Pods APIs. |
| 43 | +Both these features have a client that can be instantiated from the `cloud.localstack.sdk.chaos` and |
| 44 | +`cloud.localstack.sdk.pods` package, respectively. |
| 45 | + |
| 46 | +The clients accept requests built by using the [builder pattern](https://en.wikipedia.org/wiki/Builder_pattern). |
| 47 | +For instance, let us imagine the case in which you want to add a fault rule for S3 on the `us-east-1` region. |
| 48 | +You first need to use the `FaultRuleRequest` class to build a fault rule request. |
| 49 | +Then, you need to pass such a request object to the `addFaultRules` method of a created `ChaosClient`. |
| 50 | + |
| 51 | +```java |
| 52 | +import cloud.localstack.sdk.chaos.ChaosClient; |
| 53 | +import cloud.localstack.sdk.chaos.requests.FaultRuleRequest; |
| 54 | + |
| 55 | +var client = new ChaosClient(); |
| 56 | +var faultRuleRequest = new FaultRuleRequest.Builder().faultRule("s3", "us-east-1").build(); |
| 57 | +var addedRules = client.addFaultRules(request); |
| 58 | +``` |
| 59 | + |
| 60 | +As a second example, let us look at the necessary code to save and load a Cloud Pod. |
| 61 | +Similarly to the `ChaosClient`, the `PodsClient` exposes two functions, `savePod` and `loadPod`, which expect a `SavePodRequest` and a `LoadPodRequest`, respectively. |
| 62 | +The resulting code is the following: |
| 63 | + |
| 64 | +```java |
| 65 | +import cloud.localstack.sdk.pods.PodsClient; |
| 66 | +import cloud.localstack.sdk.pods.requests.LoadPodRequest; |
| 67 | +import cloud.localstack.sdk.pods.requests.SavePodRequest; |
| 68 | + |
| 69 | +var podsClient = new PodsClient(); |
| 70 | +// save a cloud pod |
| 71 | +var saveRequest = new SavePodRequest.Builder().podName(POD_NAME).build(); |
| 72 | +podsClient.savePod(saveRequest); |
| 73 | + |
| 74 | +//load a cloud pod |
| 75 | +var loadRequest = new LoadPodRequest.Builder().podName(POD_NAME).build(); |
| 76 | +podsClient.loadPod(loadRequest); |
| 77 | +``` |
0 commit comments