Solution

  1. You can change HashMap to ConcurrentHashMap. ConcurrentHashMap is designed to be thread-safe, so it operates stably in a multi-threaded environment and has slightly better performance than Hashtable and synchronized map. ConcurrentHashMap locks the map when you put it, so you can prevent the same key put at the same time.

  2. Open the SingletonRepo.java file in Cloud9. cloud901

  3. Change the HashMap below to ConcurrentHashMap.

    +   import java.util.concurrent.*;
        ...
    -   private HashMap<Integer, Concurrency> map = new HashMap<>();
    +   private ConcurrentHashMap<Integer, Concurrency> map = new ConcurrentHashMap<>();
    
    
  4. The final SingletonRepo.java source code

    package com.example.concurrencysample;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.concurrent.*;
    
    import com.amazonaws.auth.AWSCredentials;
    import com.amazonaws.auth.AWSStaticCredentialsProvider;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
    import com.amazonaws.services.dynamodbv2.document.Item;
    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;
    
    public class SingletonRepo {
        private static String ACCESS_KEY = "";
        private static String SECERET_KEY = "";
        private ConcurrentHashMap<Integer, Concurrency> map = new ConcurrentHashMap<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        private static SingletonRepo singletonrepo = null;
        static AWSCredentials ac;
        static AmazonDynamoDB dynamodb;
    
        private SingletonRepo() {
        }
    
        public synchronized static SingletonRepo getInstance() {
            if (singletonrepo == null) {
                singletonrepo = new SingletonRepo();
    
                ac = new BasicAWSCredentials(ACCESS_KEY, SECERET_KEY);
    
                dynamodb = AmazonDynamoDBClientBuilder
                        .standard()
                .withRegion("us-west-2")
                .withCredentials(new AWSStaticCredentialsProvider(ac) )
                .build();
                System.out.println("singleton repo instance created");
            }
            return singletonrepo;
        }
        public Concurrency get(int product_code){
            return map.get(product_code);
        }
    
        public Concurrency putKey(int product_code ) {           
            Date date = Util.getRandomDate();
            if (map.containsKey(product_code)) {
                return map.get(product_code);
            }else{
                return map.put(product_code, new Concurrency(product_code,"test" , date));
            }   
        }
        public void delKey(int product_code ) { 
            if (map.containsKey(product_code)) {
                map.remove(product_code);
            }
        }
        public int getMapCount() {
            return map.size();
        }
        public synchronized void clearHashMap() {
            map.clear();
            System.out.println("ClearMap!");
        }
        public Concurrency getKey(int product_number) {
            return map.get(product_number);  
        }
    }
    
  5. push the changes again.

    git add .
    git commit -m "fix singletonrepo.java"
    git push
    
  6. And if you check the concurrencysample of Build, you can see that the build was successful cloud901

  7. let’s also check the Report. If you select Concurrency-Unittest-Report in Report group of Build, you can see the result passed by 100%. cloud901

  8. it passed all tests, it was automatically deploy to the dev environment according to Pipeline’s process. As such, unittest is very useful for quickly testing software. You can use the CodeBuild Report to quickly visualize the results of unit tests to help fix bug.

  9. Let`s go to CodePipeline : https://console.aws.amazon.com/codepipeline

    Select Pipeline in left menu and click concurrencysample-develop. this pipeline triggerd committing develop branch in concurrencysamle repository. you can find recently successed execution result. cloud901

  10. binary deployed to staging environment EC2 while pipeline all stage and job, test sucessed. cloud901 if you useing pipeline, you can configure automatic each stage for build, test, deploy enviroment. this help to more fast devlop cycle, and reduce hand on error. you can find more detail info click each staging.

Now let`s create a pull-request for deployment code to production environment