Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Carts service checks DynamoDB connectivity at startup #537

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading