16

I am able to launch a local DynamoDB server from bash through this command:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb &

Is there not a pure-java way to start the server in one's code? I don't mean a java callout to the shell through the Process object but a way such that when I run my app, the server starts, and when my app is killed, the server is killed.

I can live with an embedded database if such a mode exists, though something that reflects server consistency semantics would be ideal.

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
  • 2
    I've exactly the same use case. The "Process" option also works for me, but how do I find the jar in the classpath, programatically. I mean the jar and its dependencies are inside a zip, which gets cached in ~/.gradle/caches. – Piyush Jajoo Jul 11 '15 at 12:19
  • @PiyushJajoo see my updated answer. Looks like I missed an announcement from them announcing the in-memory way! – mkobit Sep 24 '15 at 03:50
  • If you use embedded, please read: http://stackoverflow.com/questions/34137043/amazon-dynamodb-local-unknown-error-exception-or-failure/35353377#35353377 – Jayson Minard Feb 12 '16 at 03:08

2 Answers2

21

EDIT: September 23rd 2015

There was an announcement on Aug 3, 2015 that now adds the ability to have an embedded DynamoDB local running in the same process. You can add a Maven test dependency and use one of the ways below to run it.

<!--Dependency:-->
<dependencies>
    <dependency>
       <groupId>com.amazonaws</groupId>
       <artifactId>DynamoDBLocal</artifactId>
       <version>[1.11,2.0)</version>
    </dependency>
</dependencies>
<!--Custom repository:-->
<repositories>
    <repository>
       <id>dynamodb-local-oregon</id>
       <name>DynamoDB Local Release Repository</name>
       <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
    </repository>
</repositories>

And here is an example taken from the awslabs/aws-dynamodb-examples Github repository:

AmazonDynamoDB dynamodb = null;
try {
    // Create an in-memory and in-process instance of DynamoDB Local that skips HTTP
    dynamodb = DynamoDBEmbedded.create().amazonDynamoDB();
    // use the DynamoDB API with DynamoDBEmbedded
    listTables(dynamodb.listTables(), "DynamoDB Embedded");
} finally {
    // Shutdown the thread pools in DynamoDB Local / Embedded
    if(dynamodb != null) {
        dynamodb.shutdown();
    }
}

// Create an in-memory and in-process instance of DynamoDB Local that runs over HTTP
final String[] localArgs = { "-inMemory" };
DynamoDBProxyServer server = null;
try {
    server = ServerRunner.createServerFromCommandLineArgs(localArgs);
    server.start();

    dynamodb = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
        // we can use any region here
        new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
        .build();

    // use the DynamoDB API over HTTP
    listTables(dynamodb.listTables(), "DynamoDB Local over HTTP");
} finally {
    // Stop the DynamoDB Local endpoint
    if(server != null) {
        server.stop();
    }
}

Old answer

Like you said, there is currently no built-in way from DynamoDBLocal or the SDK to do this right now. It would be nice if there was an embedded DynamoDBLocal that you could start up in the same process.

Here is a simple workaround/solution using java.lang.Process to start it up and shut it down programmatically in case others are interested.

Documentation for DynamoDBLocal can be found here and here are the current definition of the arguments:

  • -inMemory — Run in memory, no file dump
  • -port 4000 — Communicate using port 4000.
  • -sharedDb — Use a single database file, instead of separate files for each credential and region

Note that this is using the most recent version of DynamoDBLocal as of August 5th, 2015.

final ProcessBuilder processBuilder = new ProcessBuilder("java",
        "-Djava.library.path=./DynamoDBLocal_lib",
        "-jar",
        "DynamoDBLocal.jar",
        "-sharedDb",
        "-inMemory",
        "-port",
        "4000")
        .inheritIO()
        .directory(new File("/path/to/dynamo/db/local"));

final Process process = processBuilder.start();

Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        System.out.println("Shutdown DynamoDBLocal");
        process.destroy();
        try {
            process.waitFor(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            System.out.println("Process did not terminate after 3 seconds.");
        }
        System.out.println("DynamoDBLocal isAlive=" + process.isAlive());
    }
});
// Do some stuff
johnnieb
  • 3,982
  • 4
  • 29
  • 32
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 1
    I'm unable to get this artifact from the specified repo (`http://dynamodb-local.s3-website-us-west-2.amazonaws.com/release`), and it's not available on any others. I'll post a separate question but any idea why I keep getting errors downloading it? Is this repo even up? – Sridhar Sarnobat Oct 06 '15 at 18:38
  • This answer could have problems, read: http://stackoverflow.com/questions/34137043/amazon-dynamodb-local-unknown-error-exception-or-failure/35353377#35353377 – Jayson Minard Feb 12 '16 at 03:09
  • current version in maven is `1.10.20` – Jayson Minard Feb 12 '16 at 03:09
  • @SridharSarnobat Mulesoft seem to have DynamoDbLocal hosted on their repo: https://mvnrepository.com/artifact/com.amazonaws/DynamoDBLocal – Alastair McCormack Oct 01 '19 at 11:22
0

Write a gradle task to extract the Dynamodb-Local zip and now you can use https://github.com/marcoVermeulen/gradle-spawn-plugin gradle plugin to launch the dynamodb local. It is very easy to use and no need to do any process builder magic.

Sample code -

// to start dynamodb-local
task launch(type: SpawnProcessTask) {
    println("Launching....")
    command "java -Djava.library.path=/location/to/dynamodb-local/DynamoDBLocal_lib -jar /location/to/dynamodb-local/DynamoDBLocal.jar -inMemory -delayTransientStatuses"
    ready "Initializing DynamoDB Local"
}

// to stop dynamodb-local process
task stop(type: KillProcessTask)
Piyush Jajoo
  • 1,095
  • 2
  • 18
  • 27