Skip to content

Commit

Permalink
feat: Carts service checks DynamoDB connectivity at startup (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
niallthomson authored May 7, 2024
1 parent 1f8c4e4 commit 1f0e8c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.amazon.sample.carts.diagnostics;

import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

public class DynamoFailureAnalyzer extends AbstractFailureAnalyzer<DynamoDbException> {
@Override
protected FailureAnalysis analyze(Throwable rootFailure, DynamoDbException cause) {
return new FailureAnalysis(
"An error occurred when accessing Amazon DynamoDB: \n\n"+cause.getMessage(),
"Check that the DynamoDB table has been created and your IAM credentials are configured with the appropriate access.", cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
import java.util.List;
import java.util.Optional;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;

@Slf4j
public class DynamoDBCartService implements CartService {
public class DynamoDBCartService implements CartService, ApplicationListener<ApplicationReadyEvent> {

private final DynamoDbClient dynamoDBClient;
private final boolean createTable;
Expand All @@ -58,6 +61,11 @@ public DynamoDBCartService(DynamoDbClient dynamoDBClient, DynamoDbEnhancedClient
this.table = dynamoDbEnhancedClient.table(tableName, CART_TABLE_SCHEMA);
}

@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
this.items("test");
}

@PostConstruct
public void init() {
if (createTable) {
Expand Down
1 change: 1 addition & 0 deletions src/cart/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.diagnostics.FailureAnalyzer = com.amazon.sample.carts.diagnostics.DynamoFailureAnalyzer
2 changes: 1 addition & 1 deletion src/cart/src/main/resources/application-dynamodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ carts:
dynamodb:
endpoint:
createTable: false
tableName: Items
tableName: Items

0 comments on commit 1f0e8c9

Please sign in to comment.