Posts

Showing posts with the label JAVA

Java - Key Features in Last few LTS Releases

Java - Key Features in last few Long term support release. Java-8 Lambda expression Stream API Date & Time API (java.time) Optional Class for null handling Default & Static Interface Methods Java-11 HTTP Client Standard (For RestAPI call) Local Variable Syntax for Lambda Parameters String Improvements Optional HTTP/2 Client Java-17 Sealed Classes and Interfaces Pattern Matching of Instance of Records Switch - Case expression (Enhanced) Removed Experimental AOT and JIT compiler Java-21 String Templates (Preview) Sequenced Collections Pattern Matching for Switch & Records Virtual Threads Foreign Function & Memory API (Incubator) Java 22 Unnamed Variables & Patterns Statement before super (...) Streams for Primitive Types Statement Templates (Preview) Java-25 Primitive Types in Pattern Matching (Enables pattern matching with primitive in switch and instanceof ) Module Import Declarations (Allow import module syntax for better modularity) Flexible Constructor Bodies (...

Top 10 Microservices Patterns

Image
Mostly used Top 10 Microservices Patterns: 1] API Gateway Pattern Single entry point for all client requests, which then routes those requests to the appropriate microservice 2] Circuit Breaker Pattern used to handle failures in a microservices architecture when a microservice fails or becomes unresponsive, the circuit breaker tips and redirects requests to a fallback service 3] Service Registry Pattern used to keep track of all the services in a microservices architecture. The registry acts as a central directory for service discovery 4] Service Mesh Pattern that involves adding a layer of infrastructure between microservices to handle cross-cutting concerns such as service discovery, load balancing and security  5] Event Driven Architecture Pattern that involves using events to communicate between microservices. Each microservices can publish events and subscribe to events published by other microservices. 6] Saga Pattern used to manage transactions that span multiple microservic...

Java - Best Practice - FAQ

1] Here  myListConsistOfSQL is the List<String> which consist of SQL scripts for execution PreparedStatement ps=null; try{          conn=getMyDBConnection(); for(String sql: myListConsistOfSQL) {          ps = conn.prepareStatement(sql);          ps.addBatch();          ps.executeBatch();     } } Catch(Exception e) {     throw new e; } finally {     if(ps!=null) {          ps.close();      }    if(conn!=null) {         conn.close();     } } 2] Map object to print the key and values using for each loop myDataMap.forEach((key, value) -> log.info("Details:: " + key + ":" + value)); myDataList.forEach(msg -> log.info(msg)); 3] Logger message utility class to hold details in list import java.util.ArrayList; import java.util.List; public class MessagesUtil { ...

Maven Command

Maven Command 1. Create the batch file with extension as ".bat" 2. Copy below contents into the file for build/clean install maven command echo Hello %USERNAME% echo Starting building component for your application set PATH="C:\apache-maven-3.6.0\bin";%PATH% cd C:\PROJECT_APP_PATH\ mvn clean install -P local echo DONE! timeout /t 15 Explain: Path variable will contain your exact location with bin directory of apache maven. -P {} -> Here based on your profile configuration like local, sit, uat, prod etc. build will automatically start

Java & Spring, Spring Boot Learning Links

Use below website links to understand basics of Java & Spring: 1] Beginner  https://www.geeksforgeeks.org/java/ https://howtodoinjava.com/java/basics/java-tutorial/ 2] Java-8 - Learning with examples & concepts https://mkyong.com/tutorials/java-8-tutorials/ 3] Spring Basics https://howtodoinjava.com/java-spring-framework-tutorials/ 4] Spring Boot https://howtodoinjava.com/spring-boot-tutorials/ https://www.javainuse.com/spring/sprboot

Apache POI - Read from Excel

Read from Excel with APACHE POI Library: Add below library reference in pom.xml: <dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi</artifactId>     <version>3.17</version> </dependency> <dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi-ooxml</artifactId>     <version>3.17</version> </dependency> //Read from excel by row and each cell as column by passing file input path (.xls or .xlsx) private void readFromExcel(String xslFilePath) throws Exception { Workbook wb= null; try{     wb = WorkbookFactory.create(new File( xslFilePath)); //Get data sheet at index 0 (First Tab)     Sheet sh = wb.getSheetAt(0);     Iterator<Row> rowItr = sh.iterator();          while( rowItr .hasNext()){             ...

Secure Coding in Java

Injection Attacks: Interpreted code User input formed maliciously System interprets input as a part of normal operation Unanticipated behavior Common Types SQL LDAP (Lightweight Directory Access Protocol) XSS/CSS (Cross site scripting) CRLF (Carriage Return and Line Feed) XPath SMTP/IMAP Code injection OS Command injection Host header injection SQL Injection: SQL Injection Situation SQL statement formed with variables String concatenation Malicious input repurposes SQL statement Example: foo ' or '1' = ' 1 SQL Injection Prevention Use the concept of PreparedStatement SQL statements accept variable as "?" placeholder Bind variable attached to statements, not query LDAP Injection: Caused by lack of sanitizing input (&(sn=<USERSN>(userpassword=<USERPASSWORD>)) Consider f* for USERSN with * for USERPASSWORD XPath Injection: Caused by lack of sanitizing input Vey similar to SQL injection in function Can be dangerous in injecting and manipulating dat...

Data Structure in Java

Image
Types of Data Structures: A data structure is way of collecting and organizing data Choosing the right data structure impacts efficiency Data comes from many sources e.g. Database, Files etc. Many data structures are implemented using as Linked list (Stack, queue etc.) Array List: Stores objects and can grow or shrink Linked List: Uses pointers to keep track of elements Vector: Can grow or shrink, It provides synchronization Stack: Operates on Last In , First Out (LIFO) Queue: Operates on First In, First Out (FIFO) Array List and Vectors: Advantages: Provide fast access using indexing  Memory Coherence Provide an initial size (optional) User internal array for storage, which makes random access fast Disadvantages: Can be time consuming to add elements in the middle Waste space if array is not full Need to be resized when they reach capacity Slower when deleting elements from the middle Linked List: Advantages: Insertion and deletion operations are easily implemented Elements are ef...

Java-8 Interview Questions

1)  What are new features which got introduced in Java 8? There are lots of new features which were added in Java 8. Here is the list of important features: Lambda Expression Stream API Default methods in the interface Functional Interface Optional Method reference Date API Nashorn, JavaScript Engine 2) What are main advantages of using Java 8? More compact code Less boiler plate code More readable and reusable code More testable code Parallel operations 3) What is lambda expression? Lambda expression is anonymous function which have set of parameters and a lambda (->) and a function body .You can call it function without name. Structure of Lambda Expressions 1 2 3 4   (Argument List) ->{expression;} or (Argument List) ->{statements;}    Let see a simple example of thread execution: 1 2 3 4 5 6 7 ...