Monday, 14 December 2020

Run springboot task from command line

 https://stackoverflow.com/questions/54338297/how-can-i-execute-a-command-line-task-for-a-spring-boot-application-like-a-rake


package com.example.tasks;

@Component
public class WeeklyReport implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        Arrays.asList(args).stream().forEach(a -> System.out.println(a));
        if (!(args.length > 0 && args[0].equals("-task report:weekly"))) return;
        System.out.println("weekly report");
        System.exit(0);
    }

And running it from a 'cron' job with

$ mvn spring-boot:run -Drun.arguments="-task report:weekly"

Wednesday, 2 December 2020

sonarube integration

https://github.com/SonarSource/sonarlint-eclipse/wiki/Connected-Mode

 https://docs.sonarqube.org/latest/setup/get-started-2-minutes/


Thursday, 12 November 2020

Reset file reader in java

 https://stackoverflow.com/questions/34086500/how-can-i-read-the-same-file-two-times-in-java#:~:text=Close%20the%20first%20Reader%2C%20then,to%20read%20a%20second%20time.&text=You%20are%20running%20through%20the,readline)%20!%3D



try { // try to read the file 
    br = new BufferedReader(new FileReader("movies.txt"));
    String line; int numberOfMovies = 0;
    while (br.hasNextLine()){
        numberOfMovies++;
    }
    br.close();
    Movie[] movies = new Movie[numberOfMovies];
    // store in a Movie
    // array every movie of
    // the file
    String title = "";
    int id = 0;
    int likes = 0;
    int icounter = 0;
    // count to create new movie for each line
    br = new BufferedReader(new FileReader("movies.txt"));
    while ((br.hasNextLine()) {
        line = line.trim();
        line = line.replaceAll("/t", "");
        line = line.toLowerCase();
        String[] tokens = line.split(" ");
        // store every token in a
        // string array
        id = Integer.parseInt(tokens[0]);
        likes = Integer.parseInt(tokens[tokens.length]);
        for (int i = 1; i < tokens.length; i++) {
            title = title + " " + tokens[i];
        }
        movies[icounter] = new Movie(id, title, likes);
        icounter++;
    }
} catch (IOException e) { e.printStackTrace(); }

Read same file two times.

https://stackoverflow.com/questions/17269329/how-to-read-a-bufferedreader-twice-or-multiple-times/34558791#34558791
BufferedReader br = new BufferedReader(new FileReader( "D:/log_2071-04-31.txt" ));
String strLine;
ArrayList<String> ans= new ArrayList<String>();

// Read rows
while ((strLine = br.readLine()) != null) {
    System.out.println(strLine);
    ans.add(strLine);
}

// Read again
for (String result: ans) {
    System.out.println(result);
}

Sunday, 1 November 2020

procedure call with mysql

 https://www.mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc/



Introducing to CallableStatement and stored procedure call syntax

To call stored procedures or stored functions in MySQL from JDBC, you use CallableStatement object, which inherits from PreparedStatement object. The general syntax of calling a stored procedure is as follows:

{?= call procedure_name(param1,param2,...)}

You wrap the stored procedure call within braces ({}). If the stored procedure returns a value, you need to add the question mark and equal (?=) before the call keyword. If a stored procedure does not return any values, you just omit the ?= sign. In case the stored procedure accepts any parameters, you list them within the opening and closing parentheses after the stored procedure’s name.

The following are examples of using the syntax for calling stored procedures in different contexts:

SyntaxStores Procedures
{  call procedure_name() }Accept no parameters and return no value
{ call procedure_name(?,?) }Accept two parameters and return no value
{?= call procedure_name() }Accept no parameter and return value
{?= call procedure_name(?) }Accept one parameter and return value

Tuesday, 27 October 2020

Aws lambda API getway and Dynamodb.

 https://iotdemos.wordpress.com/2017/04/24/building-simple-back-end-using-aws-dynamodb-lambda-api-gateway/



Handle payload and parse to Lambda function 


{
  "body" : $input.json('$'),
  "headers": {
    #foreach($header in $input.params().header.keySet())
    "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
 
    #end
  },
  "method": "$context.httpMethod",
  "params": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
 
    #end
  },
  "query": {
    #foreach($queryParam in $input.params().querystring.keySet())
    "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end
 
    #end
  
}

Tuesday, 13 October 2020

nested if in mysql

SELECT IF(500<1000, IF(10>12,'pawan','kumar'), IF(10>5,'oo','mm'));

Tuesday, 6 October 2020

How to create procedure in mysql

 DELIMITER $$

CREATE procedure getAnswerByQuestionId(IN questionID INT)

BEGIN 

SELECT * FROM sh_answers where question_id=questionID;

END$$

DELIMITER ;


call getAnswerByQuestionId(5);



https://www.codegrepper.com/code-examples/sql/sql+cursor+w3schools

https://www.javatpoint.com/mysql-cursor

Monday, 5 October 2020

synchronization keyword use in java

 https://stackabuse.com/synchronized-keyword-in-java/#:~:text=To%20avoid%20such%20issues%2C%20Java,the%20resource%20to%20become%20free.


public class NonSynchronizedMethod { public void printNumbers() { System.out.println("Starting to print Numbers for " + Thread.currentThread().getName()); for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } System.out.println("Completed printing Numbers for " + Thread.currentThread().getName()); } }



class ThreadOne extends Thread { NonSynchronizedMethod nonSynchronizedMethod; public ThreadOne(NonSynchronizedMethod nonSynchronizedMethod) { this.nonSynchronizedMethod = nonSynchronizedMethod; } @Override public void run() { nonSynchronizedMethod.printNumbers(); } } class ThreadTwo extends Thread { NonSynchronizedMethod nonSynchronizedMethod; public ThreadTwo(NonSynchronizedMethod nonSynchronizedMethod) { this.nonSynchronizedMethod = nonSynchronizedMethod; } @Override public void run() { nonSynchronizedMethod.printNumbers(); } }


public class TestSynchronization { public static void main(String[] args) { NonSynchronizedMethod nonSynchronizedMethod = new NonSynchronizedMethod(); ThreadOne threadOne = new ThreadOne(nonSynchronizedMethod); threadOne.setName("ThreadOne"); ThreadTwo threadTwo = new ThreadTwo(nonSynchronizedMethod); threadTwo.setName("ThreadTwo"); threadOne.start(); threadTwo.start(); } }



public synchronized void printNumbers() { System.out.println("Starting to print Numbers for " + Thread.currentThread().getName()); for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " " + i); } System.out.println("Completed printing Numbers for " + Thread.currentThread().getName()); }