Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ FROM openjdk:8

COPY --from=builder /sample/target/aws_java_test-1.0.0-SNAPSHOT-jar-with-dependencies.jar /usr/app/aws_java_test-1.0.0-SNAPSHOT-jar-with-dependencies.jar
COPY --from=builder /sample/entry.sh /usr/app/entry.sh
ENV BOLT_CUSTOM_DOMAIN=longrunningaws.bolt.projectn.co
ENV GRANICA_CUSTOM_DOMAIN=longrunningaws.bolt.projectn.co
ENV BUCKET=test-bucket OBJECT=lidar.ama
ENTRYPOINT ["/usr/app/entry.sh"]
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# README

Welcome to our AWSClient and BoltClient Java sample code repository! These examples showcase simple and easy-to-use interfaces for interacting with S3 and Bolt endpoints in your Java applications.
Welcome to our AWSClient and GranicaClient Java sample code repository! These examples showcase simple and easy-to-use interfaces for interacting with S3 and Granica endpoints in your Java applications.

In order to use Java SDK for bolt, please make sure you have it [configured correctly](https://github.com/project-n-oss/projectn-bolt-java#usage).
In order to use Java SDK for Granica, please make sure you have it [configured correctly](https://github.com/project-n-oss/projectn-bolt-java#usage).

`AWSClient.java` is a singleton that you can reference in your Java objects to read data from S3. `BoltClient.java` is another singleton, built on top of the AWS SDK, that you can use to read data from Bolt endpoints.
`AWSClient.java` is a singleton that you can reference in your Java objects to read data from S3. `GranicaClient.java` is another singleton, built on top of the AWS SDK, that you can use to read data from Granica endpoints.

In addition to these examples, we have included an example file called `App.java` that shows how you can use these singletons in a POJO. The example first attempts to read data from the Bolt endpoint and, if it returns a 404 error, it falls back to reading from S3.
In addition to these examples, we have included an example file called `App.java` that shows how you can use these singletons in a POJO. The example first attempts to read data from the Granica endpoint and, if it returns a 404 error, it falls back to reading from S3.

We hope these examples will be a helpful resource for your projects. Thank you for using our sample code repo!

Expand All @@ -16,11 +16,11 @@ We hope these examples will be a helpful resource for your projects. Thank you f
Create an environment file

```
BOLT_CUSTOM_DOMAIN=our-internal.bolt.projectn.co
GRANICA_CUSTOM_DOMAIN=our-internal.endpoint.company.com
BUCKET=test-bucket
OBJECT=test-object.txt
```

## Run the image with the env file
`docker run --net=host --name <container-name> --env-file <env-file-name> projectnsamples/bolt-java-sample-2:main`
`docker run --net=host --name <container-name> --env-file <env-file-name> projectnsamples/crunch-java-sample-2:main`

19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<version>1.0.0-SNAPSHOT</version>
<name>aws_java_test</name>



<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -28,6 +30,17 @@
</dependency>
</dependencies>
</dependencyManagement>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<dependencies>
<dependency>
Expand All @@ -39,9 +52,9 @@
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>co.projectn</groupId>
<artifactId>bolt-java-sdk</artifactId>
<version>1.0.2</version>
<groupId>ai.granica</groupId>
<artifactId>granica-java-sdk</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/projectn/aws_java_test/example/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@


class AppCode {
private BoltClient boltS3Client; // AWS-Java-sdk 2.18.16 client
private GranicaClient boltS3Client; // AWS-Java-sdk 2.18.16 client
private AWSClient awsClient;

public AppCode(BoltClient boltS3Client, AWSClient aWSClient) {
public AppCode(GranicaClient boltS3Client, AWSClient aWSClient) {
this.boltS3Client = boltS3Client;
this.awsClient = aWSClient;
}
Expand All @@ -34,6 +34,8 @@ public byte[] getObject(String bucketName, String key){
} catch (S3Exception e) {
statusCode = e.statusCode();
System.out.println(String.format("Error from bolt call, statuscode: %d", statusCode));
} catch (Exception e){
this.boltS3Client = GranicaClient.refresh();
}

if (statusCode == 404){
Expand All @@ -52,10 +54,10 @@ public byte[] getObject(String bucketName, String key){
}
}
public class App{
static BoltClient boltClient;
static GranicaClient boltClient;
static AWSClient awsClient;
public static void main(String[] args) {
boltClient = BoltClient.getInstance();
boltClient = GranicaClient.getInstance();
awsClient = AWSClient.getInstance();

String bucket = System.getenv("BUCKET");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.time.Duration;

import com.projectn.awssdk.services.s3.BoltS3Client;

import ai.granica.awssdk.services.s3.GranicaS3Client;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.retry.RetryPolicy;
Expand All @@ -14,33 +13,47 @@
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;

public class BoltClient{
public class GranicaClient{

private S3Client boltClient;
private static GranicaClient boltClientInstance = null;
private static S3Client boltClient;
private static final Object lock = new Object();

public BoltClient(){
this.boltClient = BoltS3Client.builder().build();
private GranicaClient(){
synchronized (GranicaClient.class) {
boltClient = GranicaS3Client.builder().build();
}
}

public BoltClient(AwsCredentialsProvider credentialsProvider){
this.boltClient = getBoltClientBuilder().credentialsProvider(credentialsProvider).build();
private GranicaClient(AwsCredentialsProvider credentialsProvider){
boltClient = getBoltClientBuilder().credentialsProvider(credentialsProvider).build();
}

public BoltClient(long delayBeforeNextRetry, int maxConnections, Long socketTimeout, Boolean tcpKeepAlive){
this.boltClient = getBoltClientBuilder(delayBeforeNextRetry, maxConnections, socketTimeout, tcpKeepAlive)
private GranicaClient(long delayBeforeNextRetry, int maxConnections, Long socketTimeout, Boolean tcpKeepAlive){
boltClient = getBoltClientBuilder(delayBeforeNextRetry, maxConnections, socketTimeout, tcpKeepAlive)
.build();
}

public BoltClient(AwsCredentialsProvider credentialsProvider, long delayBeforeNextRetry,
private GranicaClient(AwsCredentialsProvider credentialsProvider, long delayBeforeNextRetry,
int maxConnections, Long socketTimeout, Boolean tcpKeepAlive){

this.boltClient = getBoltClientBuilder(credentialsProvider, delayBeforeNextRetry, maxConnections, socketTimeout, tcpKeepAlive)
boltClient = getBoltClientBuilder(credentialsProvider, delayBeforeNextRetry, maxConnections, socketTimeout, tcpKeepAlive)
.build();
}

public static BoltClient getInstance() {
// We can replace this constructor with other BoltClient constructor default values
return new BoltClient();
public static GranicaClient getInstance() {
if (boltClientInstance == null) {
synchronized (lock) {
if (boltClientInstance == null) {
boltClientInstance = new GranicaClient();
}
}
}
return boltClientInstance;
}
public static GranicaClient refresh(){
boltClientInstance = null;
return getInstance();
}

public static SdkHttpClient getSdkHttpClientWithConfig(int maxConnections, Long socketTimeout, Boolean tcpKeepAlive){
Expand All @@ -66,7 +79,7 @@ public Duration computeDelayBeforeNextRetry(RetryPolicyContext context) {
* @return S3ClientBuilder
*/
public static S3ClientBuilder getBoltClientBuilder(){
return BoltS3Client.builder();
return GranicaS3Client.builder();
}

/**
Expand All @@ -92,7 +105,7 @@ public static S3ClientBuilder getBoltClientBuilder(long delayBeforeNextRetry, in

SdkHttpClient httpClient = getSdkHttpClientWithConfig(maxConnections, socketTimeout, tcpKeepAlive);

return BoltS3Client.builder()
return GranicaS3Client.builder()
.overrideConfiguration(clientOverrideConfiguration)
.httpClient(httpClient);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.projectn.aws_java_test.example;

import java.io.IOException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.S3Object;

import ai.granica.awssdk.services.s3.GranicaS3Client;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;

public class GranicaFallbackToS3 {

public static void boltFallBackToS3() {
S3Client mygranicaClient = GranicaS3Client.builder().build();
AmazonS3 awsClient = AmazonS3ClientBuilder.standard().build();
String bucketName = "bucket-under-crunch";
String key = "mypath/mykey.ama";
byte[] data = new byte[2048];
int statusCode = 200;
try {

GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(key).build();
ResponseBytes<GetObjectResponse> response = mygranicaClient.getObject(getObjectRequest, ResponseTransformer.toBytes());
data = response.asByteArray();

} catch (S3Exception e) {
statusCode = e.statusCode();
}
if (statusCode == 404) {
com.amazonaws.services.s3.model.GetObjectRequest request = new
com.amazonaws.services.s3.model.GetObjectRequest(bucketName, key);
S3Object s3Obj = awsClient.getObject(request);
try {
s3Obj.getObjectContent().read(data);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}