Java tutorial
/* * Copyright 2016 Greg Whitaker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.ignitr.dispatchr.manager.core.data; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import com.amazonaws.services.dynamodbv2.util.TableUtils; import io.ignitr.dispatchr.manager.domain.internal.Subscription; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import rx.Observable; import javax.annotation.PostConstruct; import java.util.Arrays; /** * Repository responsible for managing data about subscriptions in DynamoDB. */ @Component public class SubscriptionRepository { private static final Logger LOG = LoggerFactory.getLogger(SubscriptionRepository.class); private static final String SUBSCRIPTION_TABLE_NAME = "Dispatchr_Subscription"; private static final String TOPIC_SUBSCRIPTIONS_TABLE_NAME = "Dispatchr_TopicSubscriptions"; private final AmazonDynamoDBClient dynamoDBClient; private final DynamoDB dynamoDB; @Autowired public SubscriptionRepository(AmazonDynamoDBClient dynamoDBClient) { this.dynamoDBClient = dynamoDBClient; this.dynamoDB = new DynamoDB(dynamoDBClient); } @PostConstruct private void initialize() throws Exception { LOG.info("Initializing DynamoDB tables for SubscriptionRepository..."); // Dispatchr_Subscription CreateTableRequest subscriptionRequest = new CreateTableRequest( Arrays.asList(new AttributeDefinition("id", ScalarAttributeType.S)), SUBSCRIPTION_TABLE_NAME, Arrays.asList(new KeySchemaElement("id", KeyType.HASH)), new ProvisionedThroughput(4L, 1L)); // Dispatchr_TopicSubscriptions CreateTableRequest topicSubscriptionsRequest = new CreateTableRequest( Arrays.asList(new AttributeDefinition("topic", ScalarAttributeType.S), new AttributeDefinition("subscriptionId", ScalarAttributeType.S) ), TOPIC_SUBSCRIPTIONS_TABLE_NAME, Arrays.asList(new KeySchemaElement("topic", KeyType.HASH), new KeySchemaElement("subscriptionId", KeyType.RANGE)), new ProvisionedThroughput(4L, 1L)); if (TableUtils.createTableIfNotExists(dynamoDBClient, subscriptionRequest)) { LOG.info("Creating DynamoDB table '{}'...", SUBSCRIPTION_TABLE_NAME); } else { LOG.debug("DynamoDB table '{}' already exists! Skipping table creation.", SUBSCRIPTION_TABLE_NAME); } if (TableUtils.createTableIfNotExists(dynamoDBClient, topicSubscriptionsRequest)) { LOG.info("Creating DynamoDB table '{}'...", TOPIC_SUBSCRIPTIONS_TABLE_NAME); } else { LOG.debug("DynamoDB table '{}' already exists! Skipping table creation.", TOPIC_SUBSCRIPTIONS_TABLE_NAME); } LOG.info("Waiting for DynamoDB table '{}' to become active...", SUBSCRIPTION_TABLE_NAME); TableUtils.waitUntilActive(dynamoDBClient, SUBSCRIPTION_TABLE_NAME); LOG.info("DynamoDB table '{}' is active", SUBSCRIPTION_TABLE_NAME); LOG.info("Waiting for DynamoDB table '{}' to become active...", TOPIC_SUBSCRIPTIONS_TABLE_NAME); TableUtils.waitUntilActive(dynamoDBClient, TOPIC_SUBSCRIPTIONS_TABLE_NAME); LOG.info("DynamoDB table '{}' is active", TOPIC_SUBSCRIPTIONS_TABLE_NAME); } /** * * @param topicName * @return */ public Observable<Subscription> findAllForTopic(String topicName) { return Observable.create(subscriber -> { Table table = dynamoDB.getTable(TOPIC_SUBSCRIPTIONS_TABLE_NAME); table.query("topic", topicName).forEach(item -> { Subscription metadata = new Subscription(); metadata.setId(item.getString("subscriptionId")); metadata.setClientId(item.getString("clientId")); metadata.setTopicName(item.getString("topic")); metadata.setTopicArn(item.getString("topicArn")); metadata.setQueueArn(item.getString("queueArn")); metadata.setCreateDate(item.getLong("createDate")); metadata.setLastSubscribeDate(item.getLong("lastSubscribeDate")); subscriber.onNext(metadata); }); subscriber.onCompleted(); }); } }