Skip to content

Instantly share code, notes, and snippets.

@hakanilter
Last active July 7, 2017 15:10
Show Gist options
  • Save hakanilter/3e3ccc8b8c16fac5a647cd3540b39beb to your computer and use it in GitHub Desktop.
Save hakanilter/3e3ccc8b8c16fac5a647cd3540b39beb to your computer and use it in GitHub Desktop.
/**
<properties>
<sqlite4java.version>1.0.392</sqlite4java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>sqlite4java</artifactId>
<version>${sqlite4java.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dsqlite4java.library.path=${basedir}/target/dependencies</argLine>
<argLine>-Duser.language=en</argLine>
<argLine>-Duser.country=US</argLine>
<argLine>-Dfile.encoding=UTF-8</argLine>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
*/
package com.devveri;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.local.embedded.DynamoDBEmbedded;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import com.amazonaws.services.dynamodbv2.model.*;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class EmbeddedDynamoDBInstance {
private AmazonDynamoDB dynamoDB;
private DynamoDBProxyServer server;
public void start() throws Exception {
System.setProperty("sqlite4java.library.path", "target/dependencies");
// Create an in-memory and in-process instance of DynamoDB Local that skips HTTP
dynamoDB = DynamoDBEmbedded.create().amazonDynamoDB();
createHttpProxy();
createTables();
}
public void stop() {
if (server != null) {
try {
server.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
if (dynamoDB != null) {
dynamoDB.shutdown();
}
}
private void createHttpProxy() throws Exception {
final String[] localArgs = {"-inMemory"};
server = ServerRunner.createServerFromCommandLineArgs(localArgs);
server.start();
dynamoDB = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
"http://localhost:8000", "eu-west-1")).build();
}
private void createTables() throws Exception {
createNotification();
}
private void createNotification() throws Exception {
List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("userId").withAttributeType("N"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("createdDate").withAttributeType("S"));
List<KeySchemaElement> keySchema = new ArrayList<>();
keySchema.add(new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName("userId"));
keySchema.add(new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName("createdDate"));
CreateTableRequest request = new CreateTableRequest().withTableName("notifications")
.withKeySchema(keySchema)
.withAttributeDefinitions(attributeDefinitions)
.withProvisionedThroughput(new ProvisionedThroughput()
.withReadCapacityUnits(5L)
.withWriteCapacityUnits(5L));
CreateTableResult result = dynamoDB.createTable(request);
System.out.println(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment