Wednesday, 29 December 2021

port already in use issue docker container

 https://www.curiousm.com/labs/2020/10/08/resolving-bind-address-already-in-use-when-starting-a-docker-instance/




sudo /etc/init.d/apache2 restart 
or 
sudo apachectl -k restart

Monday, 27 December 2021

aws cognito get user infromation based on token

 https://www.archerimagine.com/articles/aws/aws-cognito-tutorials.html



var params = { AccessToken: 'STRING_VALUE' /* required */ }; cognitoidentityserviceprovider.getUser(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response });

Thursday, 16 December 2021

java find value from json

 https://mkyong.com/java/jackson-tree-model-example/


http://tutorials.jenkov.com/java-json/jackson-jsonnode.html

Thursday, 25 November 2021

print prime number between 1 to 100 without for loop

 class Main {  

  public static void main(String args[]) { 

    System.out.println("Hello, world!"); 

    Main m =new Main();

    m.printPrimeNumber(100);

    m.printPrimeNumber1();

  }

  public void printPrimeNumber(Integer number) {

    if(number%2==0) {

      System.out.println("number is prime"+number);

    }

    number--;

    if(number>0){

        printPrimeNumber(number);

      }

  }


public void printPrimeNumber1() {

    System.out.println("Hello, world!"); 

  }

}

Thursday, 7 October 2021

Tuesday, 7 September 2021

git remove remote branch chnages

move to your current branch 

then fire rest comment like below

commit id from you want to revert changes.

b6789988d5d043d47569 your commit1

b6789988d5d043d4756 your commit2

b6789988d5d0b6789988d5d043d47 your commit3

b6789988d5d043d47 changes removed from hare 

 git reset --hard b6789988d5d043d47


then push your reset changes

git push -f origin branchname


https://gitcheatsheet.org/?gclid=Cj0KCQjwwNWKBhDAARIsAJ8Hkhdlxai30BVPljj5WDd9xd6uFTe7HIr406NPx7WvT7WCvSWfEeF3R6EaAmxJEALw_wcB


Tuesday, 22 June 2021

Redis clustering

 https://medium.com/@pubuduboteju95/deep-dive-into-redis-clustering-1d71484578a9

Thursday, 6 May 2021

deny and allow access to s3 bucket

 https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies.html



{ "Id":"PolicyId2", "Version":"2012-10-17", "Statement":[ { "Sid":"AllowIPmix", "Effect":"Allow", "Principal":"*", "Action":"s3:*", "Resource": [ "arn:aws:s3:::DOC-EXAMPLE-BUCKET", "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*" ], "Condition": { "IpAddress": { "aws:SourceIp": [ "54.240.143.0/24", "2001:DB8:1234:5678::/64" ] }, "NotIpAddress": { "aws:SourceIp": [ "54.240.143.128/30", "2001:DB8:1234:5678:ABCD::/80" ] } } } ] }


https://s3tools.org/kb/item10.htm



How to restrict access to a bucket to specific IP addresses

To secure our files on Amazon S3, we can restrict access to a S3 bucket to specific IP addresses.

The following bucket policy grants permissions to any user to perform any S3 action on objects in the specified bucket. However, the request must originate from the range of IP addresses specified in the condition. The condition in this statement identifies 192.168.143.* range of allowed IP addresses with one exception, 192.168.143.188.

{
    "Version": "2012-10-17",
    "Id": "S3PolicyIPRestrict",
    "Statement": [
        {
            "Sid": "IPAllow",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::bucket/*",
            "Condition" : {
                "IpAddress" : {
                    "aws:SourceIp": "192.168.143.0/24"
                },
                "NotIpAddress" : {
                    "aws:SourceIp": "192.168.143.188/32"
                }
            }
        }
    ]
}

The IPAddress and NotIpAddress values specified in the condition uses CIDR notation described in RFC 2632. For more information, go to http://www.rfc-editor.org/rfc/rfc4632.txt

Tuesday, 4 May 2021

access S3 bucket without access key and secrete key

 https://stackoverflow.com/questions/44062813/aws-s3-upload-without-access-and-secret-key-in-java

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
              .withCredentials(new InstanceProfileCredentialsProvider(false))
              .build();

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_provider.html
https://stackoverflow.com/questions/52286306/using-ec2-metadata-credentials-in-the-laravel-filesystem

Thursday, 15 April 2021

string-concatenation-vs-stringbuilder-vs-string-format-performance

 https://kylewbanks.com/blog/java-string-concatenation-vs-stringbuilder-vs-string-format-performance



public class TestStrings {

    private static final String PROFILE_PICTURE_URL_BASE = "https://cdn.myapp.com/user/";
    private static final String PROFILE_PICTURE_URL_EXTENSION = ".png";
    private static final String PROFILE_PICTURE_URL = "https://cdn.myapp.com/user/%d.png";

    private static final int NUM_ITERATIONS = 1000000;
    
    public static void main(String[] args) {
        int userId;
        long startTime;
        
        // String Concatentation
        startTime = System.currentTimeMillis();
        for (userId = 0; userId < NUM_ITERATIONS; userId ++) {
            String profilePictureUrl = PROFILE_PICTURE_URL_BASE + userId + PROFILE_PICTURE_URL_EXTENSION;
        }
        System.out.println("String Concatentation: " + (System.currentTimeMillis() - startTime) + "ms");
        
        // StringBuilder
        startTime = System.currentTimeMillis();
        for (userId = 0; userId < NUM_ITERATIONS; userId ++) {
            StringBuilder sb = new StringBuilder(PROFILE_PICTURE_URL_BASE);
            sb.append(userId);
            sb.append(PROFILE_PICTURE_URL_EXTENSION);
            
            String profilePictureUrl = sb.toString();
        }
        System.out.println("StringBuilder: " + (System.currentTimeMillis() - startTime) + "ms");
        
        // String.format
        startTime = System.currentTimeMillis();
        for (userId = 0; userId < NUM_ITERATIONS; userId ++) {
            String profilePictureUrl = String.format(PROFILE_PICTURE_URL, userId);
        }
        System.out.println("String.format: " + (System.currentTimeMillis() - startTime) + "ms");
    }
}

Monday, 12 April 2021

Making dynamic query

 https://stackoverflow.com/questions/8549619/mysql-dynamically-build-query-string-in-a-stored-procedure-based-on-logic



CREATE  PROCEDURE `activate_games_pawan`(IN `gameId` INT(20))

BEGIN   

DECLARE whereCondition varchar(200);

CASE WHEN gameId>0 THEN

   SET @whereCondition =CONCAT(" ","sh_games.game_id=",gameId);

  SELECT @whereCondition;

  ELSE

  SET @whereCondition=1;

    SELECT @whereCondition;

  END CASE;

 SET @query=CONCAT("SELECT * FROM sh_games where ", @whereCondition);

  select @query;

PREPARE stmt FROM @query;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

END

Wednesday, 7 April 2021

Delete duplicate rows from mysql

  SET SQL_SAFE_UPDATES = 0;


    DELETE t1  FROM sh_learning_material AS t1

INNER JOIN sh_learning_material AS t2 

WHERE 

t1.id<t2.id AND

    t1.company_id = t2.company_id;


Thursday, 11 March 2021

Send email with attachment in spring boot

 https://howtodoinjava.com/spring-boot2/send-email-with-attachment/

public void sendMailWithAttachment(String to, String subject, String bodyTextMsg, String fileToAttach) 

{

System.out.println("file loc------------"+fileToAttach);

    MimeMessagePreparator preparator = new MimeMessagePreparator() 

    {

        public void prepare(MimeMessage mimeMessage) throws Exception 

        {

            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

            mimeMessage.setFrom(new InternetAddress(fromEmailId));

            mimeMessage.setSubject(subject);

            

            Multipart multipartContent= new MimeMultipart();

            MimeBodyPart mimeBodyPart =new MimeBodyPart();

            mimeBodyPart.setText(bodyTextMsg);

            MimeBodyPart mimeBodyPartAttachment =new MimeBodyPart();

            mimeBodyPartAttachment.attachFile(fileToAttach);

            multipartContent.addBodyPart(mimeBodyPart);

            multipartContent.addBodyPart(mimeBodyPartAttachment);

            mimeMessage.setContent(multipartContent,"text/csv; charset=utf-8");

            System.out.println("email send sucessfully");

        }

    };

     

    try {

        mailSender.send(preparator);

        System.out.println("email send sucessfully");

        

    }

    catch (MailException ex) {

        // simply log it and go on...

        System.out.println(ex.getMessage());

    }

}


https://coderanch.com/t/271609/java/Java-Mail-send-Multiple-recipients

Monday, 8 March 2021

Read only collection

 https://javarevisited.blogspot.com/2012/07/create-read-only-list-map-set-example-java.html#axzz6obBnjvcF

Monday, 1 March 2021

Generate and Load SSH Keys into SourceTree with PuTTY

 https://confluence.atlassian.com/sourcetreekb/generate-and-load-ssh-keys-into-sourcetree-with-putty-790629663.html

Tuesday, 23 February 2021

Is Node.js Really Single-Threaded?

 https://betterprogramming.pub/is-node-js-really-single-threaded-7ea59bcc8d64

Monday, 22 February 2021

PM2 setup on production

 https://www.tecmint.com/install-pm2-to-run-nodejs-apps-on-linux-server/


https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

Thursday, 4 February 2021

Integer to comma separated string

ShopCategoryIds.stream().map(String::valueOf)

        .collect(Collectors.joining(","));

Wednesday, 27 January 2021

Best practice for passing many arguments to method?

Pass a object that hold you parameters then pass that object to function.


Avoid longlist parameters  use Object or array of parameters

Advantage

 Consider you used longlist function on three place and you want to add more 1 then in this case you need to modify code at three places .

If you use object or array then once place change is required.



https://stackoverflow.com/questions/2432443/best-practice-for-passing-many-arguments-to-method


https://stackoverflow.com/questions/36904955/passing-single-object-vs-passing-multiple-parameters