DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Keep Your Application Secrets Secret
  • Exploring Hazelcast With Spring Boot
  • Providing Enum Consistency Between Application and Data
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Trending

  • Tactical Domain-Driven Design: Bringing Strategy to Code
  • Microservices: Externalized Configuration
  • The Network Attach Problem Nobody Warns You About
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  1. DZone
  2. Coding
  3. Java
  4. Async Rest Client to DynamoDB Using Micronaut, Maven, and GraalVM

Async Rest Client to DynamoDB Using Micronaut, Maven, and GraalVM

By 
Yegor Bondarenko user avatar
Yegor Bondarenko
·
May. 20, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

It will be a simple how-to article where I will be showing how to implement simple Rest DynamoDB client using Micronaut Framework and Maven, build a native image with GraalVM and simple comparison in resource usage between clients on Spring Boot and on Micronaut with GraalVM.

For those who are not familiar with Micronaut - it is a framework for building microservices and serverless applications. One of the key differences between Spring Boot and Micronaut is that Micronaut doesn’t use reflection to do IoC, so application startup time and memory consumption are not bound to the size of project codebase.

So our task is to handle HTTP requests for retrieving or storing some Event(id: string, body: string). Events will be stored in DynamoDB.

It might be easier to simply look at the code on Github and follow it there.

Maven

Let’s start with Maven runtime dependencies for Micronaut and DynamoDB SDK. As Micronaut doesn’t use reflection/annotation processing during startup but does it during build — we need to add annotation processors to maven-compiler-plugin. 

XML
 




xxxxxxxxxx
1
84


 
1
<dependencyManagement>
2
    <dependencies>
3
        <dependency>
4
            <groupId>io.micronaut</groupId>
5
            <artifactId>micronaut-bom</artifactId>
6
            <version>${micronaut.version}</version>
7
            <type>pom</type>
8
            <scope>import</scope>
9
        </dependency>
10
    </dependencies>
11
</dependencyManagement>
12

          
13
<dependencies>
14
    <dependency>
15
        <groupId>io.micronaut</groupId>
16
        <artifactId>micronaut-inject-java</artifactId>
17
    </dependency>
18
    <dependency>
19
        <groupId>io.micronaut</groupId>
20
        <artifactId>micronaut-runtime</artifactId>
21
    </dependency>
22
    <dependency>
23
        <groupId>io.micronaut</groupId>
24
        <artifactId>micronaut-http-server-netty</artifactId>
25
    </dependency>
26

          
27
    <dependency>
28
        <groupId>com.amazonaws</groupId>
29
        <artifactId>aws-java-sdk-dynamodb</artifactId>
30
        <version>1.11.762</version>
31
    </dependency>
32
</dependencies>
33

          
34
<build>
35
  <plugins>
36
    <plugin>
37
        <groupId>org.apache.maven.plugins</groupId>
38
        <artifactId>maven-compiler-plugin</artifactId>
39
        <version>3.8.1</version>
40
        <configuration>
41
            <compilerArgs>
42
                <arg>-parameters</arg>
43
            </compilerArgs>
44
            <annotationProcessorPaths>
45
                <path>
46
                    <groupId>io.micronaut</groupId>
47
                    <artifactId>micronaut-inject-java</artifactId>
48
                    <version>${micronaut.version}</version>
49
                </path>
50
                <path>
51
                    <groupId>io.micronaut</groupId>
52
                    <artifactId>micronaut-validation</artifactId>
53
                    <version>${micronaut.version}</version>
54
                </path>
55
            </annotationProcessorPaths>
56
        </configuration>
57
        <executions>
58
            <execution>
59
                <id>test-compile</id>
60
                <goals>
61
                    <goal>testCompile</goal>
62
                </goals>
63
                <configuration>
64
                    <compilerArgs>
65
                        <arg>-parameters</arg>
66
                    </compilerArgs>
67
                    <annotationProcessorPaths>
68
                        <path>
69
                            <groupId>io.micronaut</groupId>
70
                            <artifactId>micronaut-inject-java</artifactId>
71
                            <version>${micronaut.version}</version>
72
                        </path>
73
                        <path>
74
                            <groupId>io.micronaut</groupId>
75
                            <artifactId>micronaut-validation</artifactId>
76
                            <version>${micronaut.version}</version>
77
                        </path>
78
                    </annotationProcessorPaths>
79
                </configuration>
80
            </execution>
81
        </executions>
82
    </plugin>
83
  </plugins>
84
</build>  



DynamoDB

Configuration

A simple config where we set up connection to DynamoDB. For test purpose we need to specify dynamoEndpoint. In case of real application we need to specify region instead of endpoint. 

Java
 




xxxxxxxxxx
1
22


 
1
@Factory
2
public class Config {
3

          
4
    @Bean
5
    AmazonDynamoDBAsync dynamoDbAsyncClient(Environment environment) {
6
        Optional<String> secretKey = environment.get("aws.secretkey", String.class);
7
        Optional<String> accessKey = environment.get("aws.accesskey", String.class);
8
        String endpoint = environment.get("dynamo.endpoint", String.class, "http://localhost:8000");
9
        if (!secretKey.isPresent() || !accessKey.isPresent()) {
10
            throw new IllegalArgumentException("Aws credentials not provided");
11
        }
12
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey.get(), secretKey.get());
13
        AmazonDynamoDBAsyncClientBuilder clientBuilder = AmazonDynamoDBAsyncClientBuilder
14
                .standard()
15
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
16
                .withEndpointConfiguration(
17
                        new AwsClientBuilder.EndpointConfiguration(endpoint, null)
18
                );
19

          
20
        return clientBuilder.build();
21
    }
22
}



Async DynamoDB Service 

Service for saving/retrieving event to/from DynamoDB. All async requests to aws are wrapped into RxJava constructions for easy handling of futures.

Java
 




xxxxxxxxxx
1
79


 
1
@Singleton
2
public class DynamoDBService {
3

          
4
    public static final String TABLE_NAME = "events";
5
    public static final String ID_COLUMN = "id";
6
    public static final String BODY_COLUMN = "body";
7

          
8
    private final AmazonDynamoDBAsync client;
9

          
10
    public DynamoDBService(AmazonDynamoDBAsync client) {
11
        this.client = client;
12
    }
13

          
14
    //Create DynamoDB table if not exists
15
    @PostConstruct
16
    public void createTableIfNotExists() {
17
        if (!isTableExists()) {
18
            createTable();
19
        }
20
    }
21
    
22
    public Maybe<Event> getEvent(String eventId) {
23
        Map<String, AttributeValue> searchCriteria = new HashMap<>();
24
        searchCriteria.put(ID_COLUMN, new AttributeValue().withS(eventId));
25
        // Building request to get event by Id
26
        GetItemRequest request = new GetItemRequest()
27
                .withTableName(TABLE_NAME)
28
                .withKey(searchCriteria)
29
                .withAttributesToGet(BODY_COLUMN); // lets retrieve only body as id we already have
30
        return Maybe.fromFuture(client.getItemAsync(request))
31
                .subscribeOn(Schedulers.io())
32
                .filter(result -> result.getItem() != null) // check that request returned something
33
                .map(result ->  //building Event from response
34
                     new Event(eventId, 
35
                               result.getItem().get(BODY_COLUMN).getS())
36
                              ); 
37
    }
38

          
39
    public Single<String> saveEvent(String eventBody) {
40
        String id = UUID.randomUUID().toString();
41

          
42
        Map<String, AttributeValue> item = new HashMap<>();
43
        item.put(ID_COLUMN, new AttributeValue().withS(id));
44
        item.put(BODY_COLUMN, new AttributeValue().withS(eventBody));
45

          
46
        PutItemRequest putRequest = new PutItemRequest()
47
                .withTableName(TABLE_NAME)
48
                .withItem(item);
49

          
50
        return Single.fromFuture(client.putItemAsync(putRequest))
51
                .subscribeOn(Schedulers.io())
52
                .map(result -> id);
53
    }
54

          
55
    private boolean isTableExists() {
56
        ListTablesRequest tablesRequest = new ListTablesRequest()
57
                .withExclusiveStartTableName(TABLE_NAME);
58
        ListTablesResult result = client.listTables(tablesRequest);
59
        return result.getTableNames().contains(TABLE_NAME);
60
    }
61

          
62
    private CreateTableResult createTable() {
63
        KeySchemaElement keyDefinitions = new KeySchemaElement()
64
                .withAttributeName(ID_COLUMN)
65
                .withKeyType(KeyType.HASH);
66

          
67
        AttributeDefinition keyType = new AttributeDefinition()
68
                .withAttributeName(ID_COLUMN)
69
                .withAttributeType(ScalarAttributeType.S);
70

          
71
        CreateTableRequest request = new CreateTableRequest()
72
                .withTableName(TABLE_NAME)
73
                .withKeySchema(keyDefinitions)
74
                .withAttributeDefinitions(keyType)
75
                .withBillingMode(BillingMode.PAY_PER_REQUEST);
76

          
77
        return client.createTable(request);
78
    }
79
}



Controller

Here we gonna expose our REST Api with GET method for retrieving event from DynamoDB and POST for storing event. 

Java
 




xxxxxxxxxx
1
23


 
1
@Controller("/event")
2
public class SimpleController {
3

          
4
    private final DynamoDBService dynamoDBService;
5

          
6
    public SimpleController(DynamoDBService dynamoDBService) {
7
        this.dynamoDBService = dynamoDBService;
8
    }
9

          
10
    @Get("/{eventId}")
11
    @Produces(MediaType.APPLICATION_JSON) 
12
    public Maybe<Event> getEvent(@PathVariable String eventId) {
13
        Maybe<Event> event = dynamoDBService.getEvent(eventId);
14
        return event;
15
    }
16

          
17
    @Post("/")
18
    @Produces(MediaType.APPLICATION_JSON)
19
    public Single<String> saveEvent(@Body String body) {
20
        Single<String> event = dynamoDBService.saveEvent(body);
21
        return event;
22
    }
23
}



Integration Test

Maven dependencies 

For running integration test with DynamoDB we need DynamoDBLocal dependency, which is not really the DynamoDB, but SQLite with implemented DynamoDB interfaces on top of it. 

XML
 




xxxxxxxxxx
1
55


 
1
 <dependency>
2
    <groupId>com.amazonaws</groupId>
3
    <artifactId>DynamoDBLocal</artifactId>
4
    <version>1.12.0</version>
5
    <scope>test</scope>
6
</dependency>
7
<dependency>
8
    <groupId>io.micronaut</groupId>
9
    <artifactId>micronaut-http-client</artifactId>
10
    <scope>test</scope>
11
</dependency>
12
<dependency>
13
    <groupId>io.micronaut.test</groupId>
14
    <artifactId>micronaut-test-junit5</artifactId>
15
    <scope>test</scope>
16
</dependency>
17
<dependency>
18
    <groupId>org.junit.jupiter</groupId>
19
    <artifactId>junit-jupiter-api</artifactId>
20
    <version>5.6.0</version>
21
    <scope>test</scope>
22
</dependency>
23

          
24
<build>
25
    <plugins>
26
        <plugin>
27
            <groupId>org.apache.maven.plugins</groupId>
28
            <artifactId>maven-dependency-plugin</artifactId>
29
            <version>2.10</version>
30
            <executions>
31
                <execution>
32
                    <id>copy</id>
33
                    <phase>test-compile</phase>
34
                    <goals>
35
                        <goal>copy-dependencies</goal>
36
                    </goals>
37
                    <configuration>
38
                        <includeScope>test</includeScope>
39
                        <includeTypes>so,dll,dylib</includeTypes>
40
                        <!--Keep an eye on output directory - it will be used for starting dynamodb-->
41
                        <outputDirectory>${project.basedir}/target/native-libs</outputDirectory>
42
                    </configuration>
43
                </execution>
44
            </executions>
45
        </plugin>
46
    </plugins>
47
</build>
48

          
49
<repositories>
50
    <repository>
51
        <id>dynamodb-local-oregon</id>
52
        <name>DynamoDB Local Release Repository</name>
53
        <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
54
    </repository>
55
</repositories>


DynamoDB server 

Now we need to start DynamoDB before tests, we can do it with the Jupiter Extension. 

Java
 




xxxxxxxxxx
1
29


 
1
public class LocalDynamoDbExtension implements AfterAllCallback, BeforeAllCallback {
2

          
3
    protected DynamoDBProxyServer server;
4

          
5
    public LocalDynamoDbExtension() {
6
        //here we set the path from "outputDirectory" of maven-dependency-plugin
7
        System.setProperty("sqlite4java.library.path", "target/native-libs");
8
    }
9

          
10
    @Override
11
    public void afterAll(ExtensionContext extensionContext) throws Exception {
12
        stopUnchecked(server);
13
    }
14

          
15
    @Override
16
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
17
        this.server = ServerRunner
18
                .createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", "8000"});
19
        server.start();
20
    }
21

          
22
    protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) {
23
        try {
24
            dynamoDbServer.stop();
25
        } catch (Exception e) {
26
            throw new RuntimeException(e);
27
        }
28
    }
29
}



Running test

Now we can create an integration test and check if our REST Api methods do what we think. 

Java
 




xxxxxxxxxx
1
41


 
1
@MicronautTest
2
@ExtendWith(LocalDynamoDbExtension.class)
3
public class SimpleControllerTest {
4

          
5
    @Inject
6
    @Client("/event")
7
    RxStreamingHttpClient client;
8

          
9
    @Inject
10
    DynamoDBService dynamoDBService;
11

          
12
    @Test
13
    public void getEventsTest() {
14
        //add event to database so we can query it via http
15
        String eventBody = "testMessage";
16
        String eventId = dynamoDBService.saveEvent(eventBody).blockingGet();
17
        HttpRequest request = HttpRequest.GET(eventId);
18
        HttpResponse<List<Event>> rsp = client
19
          .toBlocking()
20
          .exchange(request, Argument.listOf(Event.class));
21

          
22
        assertEquals(HttpStatus.OK, rsp.getStatus());
23
        List<Event> body = rsp.body();
24
        assertEquals(1, body.size());
25
        assertEquals(eventBody, body.get(0).getBody());
26
    }
27

          
28
    @Test
29
    public void saveEventTest() {
30
        HttpRequest request = HttpRequest.POST("/", "postBody");
31
        HttpResponse<String> rsp = client
32
          .toBlocking()
33
          .exchange(request, Argument.of(String.class));
34
        Optional<String> id = rsp.getBody();
35
        assertTrue(id.isPresent());
36

          
37
        Event event = dynamoDBService.getEvent(id.get()).blockingGet();
38
        assertEquals(id.get(), event.getId());
39
        assertEquals("postBody", event.getBody());
40
    }
41
}



Native Image

Using GraalVM we can build ahead-of-time compiled native image which is very useful for small applications. Native image includes an application classes, classes from its dependencies, classes from JDK. It does not run on the JVM. So in the end you will get standalone executable image which you can run without any JVM.

Because image already compiled, linked and partly initialized it will start faster, and you will get lower memory footprint. But keep in mind that there is a price for that - absence of JIT compiler, much simpler GC(SerialGC), platform dependent, hard to use frameworks which heavily relies on reflection(Spring Framework).

You can build image in several ways:

Building Image With Docker Multistage 

One disadvantage is that you need to know application’s classpath or download all dependencies in folder and point to it during building image.

Dockerfile
 




xxxxxxxxxx
1
13


 
1
FROM oracle/graalvm-ce:20.0.0-java11 as graalvm
2
RUN gu install native-image
3

          
4
COPY . /home/app/micronaut-dynamodb-client
5
WORKDIR /home/app/micronaut-dynamodb-client
6

          
7
RUN native-image --no-server -cp all-runtime-deps.jar
8

          
9
FROM frolvlad/alpine-glibc
10
RUN apk update && apk add libstdc++
11
EXPOSE 8080
12
COPY --from=graalvm /home/app/micronaut-dynamodb-client/micronaut-dynamodb-client /srv/micronaut-dynamodb-client
13
ENTRYPOINT ["/srv/micronaut-dynamodb-client", "-Xmx68m"]



Building image With Maven

A bit harder than with a docker. You need to install GrallVM JDK, install native-image tool, set GraalVM as JDK for your project. After that you can add a plugin to maven and plugin will do the job. 

XML
 




xxxxxxxxxx
1
18


 
1
<plugin>
2
    <groupId>org.graalvm.nativeimage</groupId>
3
    <artifactId>native-image-maven-plugin</artifactId>
4
    <version>20.0.0</version>
5
    <executions>
6
        <execution>
7
            <goals>
8
                <goal>native-image</goal>
9
            </goals>
10
            <phase>deploy</phase>
11
        </execution>
12
    </executions>
13
    <configuration>
14
        <mainClass>com.yegor.micronaut.dynamodb.App</mainClass>
15
        <buildArgs>-H:Name=dynamodb-client</buildArgs> <!--Image name-->
16
        <buildArgs>-H:IncludeResources="logback.xml|application.yml"</buildArgs> <!--Resources to add to image-->
17
    </configuration>
18
</plugin>



When native image is ready we can build Docker image with it.

Dockerfile
 




xxxxxxxxxx
1


 
1
FROM frolvlad/alpine-glibc
2
RUN apk update && apk add libstdc++
3
COPY target/micronaut-dynamodb-client /srv/micronaut-dynamodb-client
4
EXPOSE 8080
5
ENTRYPOINT ["/srv/micronaut-dynamodb-client"]



Simple Comparision Between Dockerized Native Image and Dockerized Spring Boot App

As an example I’m gonna take spring boot application from this post which is basically doing the same stuff but with a help of Spring. 

Image Sizes 

First lets look at image sizes by running docker images 

Plain Text
 




xxxxxxxxxx
1


 
1
REPOSITORY                  TAG          SIZE
2
micronaut-dynamodb-native   latest       84.4MB
3
spring-boot-dynamodb        latest       364MB



Obviously, docker with native image uses less space, cause the native image removed everything that won’t be in use, including JVM.

App startup 

I run each image and just look at logs to get info when application completed startup. Micronaut-Native-Image started in 54 ms. Pretty impressive :) 

Plain Text
 




xxxxxxxxxx
1


 
1
io.micronaut.runtime.Micronaut - Startup completed in 54ms. Server Running: http://5330567cbd7c:8080



Spring Boot application took much longer to start:

Plain Text
 




xxxxxxxxxx
1


 
1
com.example.dynamo_spring.App   : Started App in 4.093 seconds (JVM running for 4.736)



Memory Footprint

To print Memory and CPU consumption run docker stats. But as I don’t do any requests to images - CPU numbers are irrelevant. 

Plain Text
 




x


 
1
NAME                            MEM USAGE                    
2
micronaut-dynamodb-native       12.63MiB    
3
spring-boot-dynamodb            152MiB   



Hurray, you made till the end!

Happy coding :)

Spring Framework Apache Maven GraalVM application Spring Boot integration test Plain text Java (programming language) Docker (software) Web Protocols

Published at DZone with permission of Yegor Bondarenko. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • Exploring Hazelcast With Spring Boot
  • Providing Enum Consistency Between Application and Data
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook