AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

/** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file 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; import java.util.Map; import java.util.Random; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.amazonaws.Request; import com.amazonaws.Response; import com.amazonaws.handlers.RequestHandler2; import; import; import; import; import; import; public class FaultInjectionRequestHandler extends RequestHandler2 { private AmazonDynamoDBClient dynamoDBClient; private static final Random rnd = new Random(1234); private static final Log logger = LogFactory.getLog(FaultInjectionRequestHandler.class); public FaultInjectionRequestHandler(AmazonDynamoDBClient dynamoDBClient) { this.dynamoDBClient = dynamoDBClient; } @Override public void beforeRequest(Request<?> request) { /* Things to do just before a request is executed */ if (request.getOriginalRequest() instanceof PutItemRequest) { /* Throw throughput exceeded exception for 50% of put requests */ if (rnd.nextInt(2) == 0) {"Injecting ProvisionedThroughputExceededException"); throw new ProvisionedThroughputExceededException("Injected Error"); } } /* Add latency to some Get requests */ if (request.getOriginalRequest() instanceof GetItemRequest) { /* Delay 50% of GetItem requests by 500 ms */ if (rnd.nextInt(2) == 0) { /* * Delay on average 50% of the requests from client perspective */ try {"Injecting 500 ms delay"); Thread.sleep(500); } catch (InterruptedException ie) {; throw new RuntimeException(ie); } } } } @Override public void afterResponse(Request<?> request, Response<?> response) { /* * The following is a hit and miss for multi-threaded clients as the * cache size is only 50 entries */ String awsRequestId = dynamoDBClient.getCachedResponseMetadata(request.getOriginalRequest()).getRequestId();"AWS RequestID: " + awsRequestId); /* * Here you could inspect and alter the response object to see how your * application behaves for specific data */ if (request.getOriginalRequest() instanceof GetItemRequest) { GetItemResult result = (GetItemResult) response.getAwsResponse(); Map<String, AttributeValue> item = result.getItem(); if (item.get("name").getS().equals("Airplane")) { // Alter the item item.put("name", new AttributeValue("newAirplane")); item.put("new attr", new AttributeValue("new attr")); // Add some delay try { Thread.sleep(500); } catch (InterruptedException ie) {; throw new RuntimeException(ie); } } } } @Override public void afterError(Request<?> request, Response<?> response, Exception e) { // TODO Auto-generated method stub } }

Sample Details

Service: dynamodb

Last tested:

Author: AWS

Type: full-example

On this page: