Friday, 22 March 2024

Event loop

 https://reflectoring.io/getting-started-with-spring-webflux/

 

 

Non-blocking request

Monday, 11 March 2024

spring security code

 https://medium.com/code-with-farhan/spring-security-2023-4110f1e33b47

Java interview question

 

Outline:

  1. What is Java?
  2. Explain the difference between JDK, JRE, and JVM.
  3. How does the ‘public static void main(String[] args)’ method work?
  4. What is bytecode in Java?
  5. Differentiate between overloading and overriding.
  6. What is the Java ClassLoader?
  7. Can we override the static method in Java?
  8. Describe the different access modifiers in Java.
  9. What is the difference between an abstract class and an interface?
  10. How does the ‘finally’ block differ from the ‘finalize’ method in Java?
  11. Explain the concept of Java packages.
  12. What are Java annotations?
  13. How does multi-threading work in Java?
  14. Explain the difference between throw and throws in Java.
  15. What is the significance of the transient keyword?
  16. How do you ensure thread safety in Java?
  17. Explain the Singleton pattern.
  18. What are Java Streams?
  19. What are the primary differences between ArrayList and LinkedList?
  20. How do HashSet, LinkedHashSet, and TreeSet differ?
  21. Differentiate between HashMap and ConcurrentHashMap.
  22. Describe the contract between hashCode() and equals() methods.
  23. What is Java reflection?
  24. How do you create a custom exception in Java?
  25. What is the difference between a checked and unchecked exception?
  26. What are generics? Why are they used?
  27. Explain the concept of Java Lambda Expressions.
  28. What is the diamond problem in inheritance?
  29. Describe the difference between fail-fast and fail-safe iterators.
  30. What is type erasure in Java generics?

Wednesday, 6 March 2024

Spring security tutorial

 https://reflectoring.io/spring-security/

Java with kafka

 https://www.sohamkamani.com/java/kafka/

https://developers.redhat.com/articles/2022/04/05/developers-guide-using-kafka-java-part-1#what_setup_do_you_need_to_get_started_

Tuesday, 5 March 2024

find 2nd max and min java8 stream api

 

public static void main (String arg[]) {

List<Integer>numbers= Arrays.asList(10,3,56,7,8);

System.out.println(numbers.stream().sorted(Collections.reverseOrder()).skip(1).findFirst().get());
System.out.println(numbers.stream().sorted().skip(1).findFirst().get());

}

Apache kafka springboot example

 https://www.javaguides.net/2022/07/event-driven-microservices-using-spring-boot-and-apache-kafka.html

Monday, 4 March 2024

Apache kafka setup

 

To run Apache Kafka from the binary distribution, you need to follow a series of steps. Below are the general steps for running Kafka on a Unix-like system (Linux or macOS). Make sure you have Java installed on your machine before proceeding.

Prerequisites:

  1. Java Installation: Ensure you have Java installed on your system. You can check your Java version using:

    bash
  • java -version

    If Java is not installed, you can install it by following the instructions for your operating system.

  • Download and Extract Kafka: Download the Kafka binary distribution from the official Apache Kafka website. Once downloaded, extract the contents of the tarball:

    bash
    1. tar -xzf kafka_2.12-3.7.0.tgz cd kafka_2.12-3.7.0

    Start ZooKeeper:

    Kafka uses ZooKeeper, so you need to start it before starting Kafka.

    bash
    bin/zookeeper-server-start.sh config/zookeeper.properties

    Start Kafka Server:

    In a new terminal, start the Kafka server.

    bash
    bin/kafka-server-start.sh config/server.properties

    Create a Kafka Topic:

    In another terminal, you can create a Kafka topic to test your setup.

    bash
    bin/kafka-topics.sh --create --topic mytopic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1

    Produce and Consume Messages:

    You can now use the Kafka producer and consumer to test your setup.

    Producer:

    bash
    bin/kafka-console-producer.sh --topic mytopic --bootstrap-server localhost:9092

    Consumer:

    bash
    bin/kafka-console-consumer.sh --topic mytopic --bootstrap-server localhost:9092 --from-beginning

    Type messages in the producer terminal, and you should see them being consumed in the consumer terminal.

    These are basic steps to run Kafka locally for testing and development. In a production environment, you may need to configure Kafka appropriately for your specific use case and environment. Additionally, consider creating systemd service files or using other deployment tools based on your needs.

    Two version of exception hadling by controller advice

     

    //    @ExceptionHandler({MethodArgumentNotValidException.class})
    // public ResponseEntity<ApiFail> handleValidationExceptions(
    // MethodArgumentNotValidException ex, HttpStatus httpStatus) {
    // Integer error_code_global = ex.hashCode();
    // Map<String, String> errors = new HashMap<>();
    // ex.getBindingResult().getAllErrors().forEach((error) -> {
    // String fieldName = ((FieldError) error).getField();
    // String errorMessage = error.getDefaultMessage();
    // Integer error_code = error.hashCode();
    // errors.put(fieldName, errorMessage);
    // });
    // Map data = new HashMap();
    // data.put("field", errors);
    // return new ResponseEntity<ApiFail>(new ApiFail(error_code_global, data), HttpStatus.BAD_REQUEST);
    // }
    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
    Map<String, Object> errorMessageBody = new LinkedHashMap<>();
    Map<String, Object> resBody = new LinkedHashMap<>();
    ex.getBindingResult()
    .getFieldErrors()
    .stream().forEach(error->{
    errorMessageBody.put(error.getField(),error.getDefaultMessage());
    });
    resBody.put("success", false);
    resBody.put("message_code", status.value());
    resBody.put("data", errorMessageBody);
    return new ResponseEntity<>(resBody, headers, status);
    }

    Friday, 1 March 2024

    Java ENUM

     // import static org.junit.jupiter.api.Assertions.assertEquals;

    // import org.junit.jupiter.api.Test;
    import java.util.*;
    import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;


    public class Main {
        public static void main(String[] args) {
          System.out.println("Objects are equal:"+Color.REDP.getHexCodevs());
          System.out.println("Objects are equal:"+Color.REDP.getHexCode());
        }
      }

     enum Color {
        REDP("FF0000","opp"),
        GREEN("00FF00","opl"),
        BLUE("0000FF","niop");

        private final String hexCode;
       private final String vs;

        // Constructor for enum constants
        Color(String hexCode,String vs) {
            this.hexCode = hexCode;
            this.vs=vs;
        }

        // Getter method
        public String getHexCode() {
            return hexCode;
        }

       public String getHexCodevs() {
           return vs;
       }

        // Custom method
        public void displayInfo() {
            System.out.println("Color: " + this.name() + ", Hex Code: " + hexCode);
        }
    }