diff --git a/lambada-maven-plugin/src/main/java/org/lambadaframework/aws/ApiGateway.java b/lambada-maven-plugin/src/main/java/org/lambadaframework/aws/ApiGateway.java index 47d1130..347f03c 100644 --- a/lambada-maven-plugin/src/main/java/org/lambadaframework/aws/ApiGateway.java +++ b/lambada-maven-plugin/src/main/java/org/lambadaframework/aws/ApiGateway.java @@ -1,7 +1,12 @@ package org.lambadaframework.aws; +import com.amazonaws.AmazonClientException; +import com.amazonaws.AmazonWebServiceRequest; +import com.amazonaws.ClientConfiguration; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; +import com.amazonaws.retry.PredefinedRetryPolicies; +import com.amazonaws.retry.RetryPolicy; import com.amazonaws.services.apigateway.AmazonApiGateway; import com.amazonaws.services.apigateway.AmazonApiGatewayClient; import com.amazonaws.services.apigateway.model.*; @@ -15,7 +20,9 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import java.io.IOException; -import java.util.*; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; public class ApiGateway extends AWSTools { @@ -86,7 +93,27 @@ protected AmazonApiGateway getApiGatewayClient() { return apiGatewayClient; } - return apiGatewayClient = new AmazonApiGatewayClient(getAWSCredentialsProvideChain()).withRegion(Region.getRegion(Regions.fromName(deployment.getRegion()))); + RetryPolicy.RetryCondition retryCondition = new RetryPolicy.RetryCondition() { + + @Override + public boolean shouldRetry(AmazonWebServiceRequest amazonWebServiceRequest, AmazonClientException amazonClientException, int i) { + if (amazonClientException instanceof TooManyRequestsException) { + return true; + } + return PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION.shouldRetry(amazonWebServiceRequest, + amazonClientException, i); + } + }; + + RetryPolicy retryPolicy = new RetryPolicy(retryCondition, + PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, + 10, true); + + ClientConfiguration clientConfig = new ClientConfiguration() + .withRetryPolicy(retryPolicy); + + apiGatewayClient = new AmazonApiGatewayClient(getAWSCredentialsProvideChain(), clientConfig).withRegion(Region.getRegion(Regions.fromName(deployment.getRegion()))); + return apiGatewayClient; } /** @@ -97,14 +124,16 @@ protected AmazonApiGateway getApiGatewayClient() { */ public void deployEndpoints() throws IOException { - if (log != null) + + if (log != null) { log.info("API Gateway deployment is being initialized."); + } List resources = getResources(); - if (log != null) + if (log != null) { log.info(resources.size() + " resources found in JAR File."); - + } createOrUpdateApi(); walkThroughResources(resources); @@ -242,8 +271,9 @@ protected List getResources() throws IOException { String jarFileLocation = deployment.getJarFileLocationOnLocalFileSystem(); - if (log != null) + if (log != null) { log.info("JAR File is being scanned. Used JAR File location: " + jarFileLocation + " Package: " + deployment.getPackageName()); + } JAXRSParser parser = new JAXRSParser().withJarFile(jarFileLocation, deployment.getPackageName()); return parser.scan(); @@ -252,7 +282,7 @@ protected List getResources() protected void walkThroughResources(List resources) { - if (resources.size() == 0) { + if (resources.isEmpty()) { if (log != null) { log.info("Not found any resources to deploy"); } @@ -345,7 +375,6 @@ protected String createResource(Resource jerseyResource) { protected boolean deployResource(Resource jerseyResource) { - com.amazonaws.services.apigateway.model.Resource amazonApiResource; String fullPath = jerseyResource.getPath(); if (log != null) { @@ -358,7 +387,7 @@ protected boolean deployResource(Resource jerseyResource) { log.info("Resource created: " + fullPath + " (" + createdId + ")"); } - amazonApiResource = getResourceByPath(fullPath); + com.amazonaws.services.apigateway.model.Resource amazonApiResource = getResourceByPath(fullPath); deployMethods(jerseyResource, amazonApiResource); return true; } @@ -397,11 +426,6 @@ protected void deployMethods(Resource jerseyResource, com.amazonaws.services.api .withResourceId(apiGatewayResource.getId())); - /** - * To prevent TooManyRequestsException errors from API Gateway - */ - Thread.sleep(1000); - if (log != null) { log.info(methodToDelete + " method deleted on resource id " + apiGatewayResource.getId()); } @@ -409,13 +433,6 @@ protected void deployMethods(Resource jerseyResource, com.amazonaws.services.api /** * Do nothing, continue */ - } catch (InterruptedException e) { - - if (log != null) { - log.error("A system error occured. Recovering"); - } - - deployMethods(jerseyResource, apiGatewayResource); } }