Monday, January 30, 2023

Layered architecture

Layered architecture means that we spread our code across multiple layers doing different tasks. What are the advantages of making code in layers?

Simplicity

Since we spread our code across the layers and different classes representing the layer, our application and the classes themselves will be very simple, small and easy to understand.

Separation of concerns

There will be clear separation of concerns. E.g. the presentation layer will present the data, service layer will have business logic and data access layer manages the data source and retrieving the data for the application, and integration layer responsible for communicating or integrating with other applications. So, each layers have their own function or concerns.

Easy Maintenance

Once our application goes to production, if we have any issue with our application, we can just jump into a layer according to what the problem is. The separation of concern makes clear division between the code

  • If database is causing some problems, investigate data access layer.
  • If Business logic is to be changed or it is creating some issues, we can easily look to service layer.
  • If we have some issues with how data is presented to the user, we can directly investigate presentation layer.

 

Four layers of application and corresponding Classes in Microservices

Any java enterprise application have multiple layers built in them for easy code management and segregation of concerns. These layers are made up of Java interfaces and classes that we create

Data access layer (DAO)

Independent layer, responsible for performing the CRUD (Create, Read, Update, Delete) operation against the data store or databases.

Service layer

Services provided by the Data access layer is used by the service layer to perform any business logic in the application. All the business logic will reside in the service layer

Presentation layer

Services provided by the service layer is used by presentation layer. It presents the data in appropriate format to the end users.

Integration Layer

Services provided by the service layer is also used by the integration layer. It access other’s APIs or exposes one’s APIs to others. It integrates the application with another service using API and other third-party services. Other applications can also use integration layer to communicate with current system. Integration layer is typically made up of web services.

 

There are 9 or 10 important interfaces and classes that falls under these layers.

Type

Filename

Functions

Layer

1) Model class / Entity Class (C)

___.java

Address.java

Student.java

Represent our domain model or Entity model. It typically uses the database model or table structure.

 

2) DAO Interface

I___DAO(I)

IStudentDAO.java

Interface for accessing data

Data Access

3) DAO Implementation

___DAOImpl (C)

StudentDAOImpl.java

Implementation of data access, extending DAO interface.

Data Access

4) Service Interface

I___Service(I)

IStudentService.java

Interface for applying business logic

Service

5) Service Implementation

___ServiceImpl (C)

StudentServiceImpl.java

Extends Service interface and implements the business logic for that specific application

Service

6) Controller

___Controller(C)

StudentController.java

Extends Service Interface and presents data to end users

Presentation

7) Utility Class

Utility(C)

Perform utility operations across the layers

Some or all

8) Validator Class

Validator(C)

Validate the data that comes from the user and goes to the user

 

9) Service provider/ Service Consumer

Service provider (C)

Service Consumer (C)

Web services exposed to the third party or accessing web services of third party. Mostly RESTful services

Integration Layer

10) Special View

View (C)

Special view that presents the data as excel sheet or pdf or similar.

 

 

 

Layer wise Technologies

Data Access Layer

This layer is used to connect to underlying database. We use ORM (Object Relational Mapping) in data access layer. Which in turn uses JDBC (Java Database Connector). In modern software we don’t use JDBC directly. Hibernate is a ORM technology that we mostly use with Spring Boot. Spring Data dependency makes it very easy to use this.

Services Layer

Services classes are made with plain java interfaces and classes. We write business logic using plain java code. We don’t need any framework there. But it might require third-party dependencies according to the job description.

Presentation Layer

Presentation layer generally uses MVC (Model View Controller) pattern. For that it uses Spring MVC. This dependency helps creating application in MVC pattern. Which in turn can be used by client applications to access our website.

Integration Layer

Integration layer is used to communicate with other applications. It uses web services, and now RESTful web services are used more than SOAP. We use Spring Data REST or Spring MVC dependency for this.

 

Key Class names for Java EE or Spring Boot Applications

We derive all the names from module name. For example in our College Information System, we can have Student module, Teacher module, Subject module. We derive the class name from the module name. For simplicity, we take module name as Student.

1) Modal Class

Represents the data of our domain. Directly mapped to our database model.

____.java

Student.java

Admin.java

2) DAL (Data Access Layer)

Here we have an interface and a class. The implementation and interface has IS-A relationship.

2.1) Interface

I___DAO

IStudentDAO.java

IAdminDAO.java

2.2) Implementation

___DAOImpl

StudentDAOImpl.java

AdminDAOImpl.java

Here,

StudentDAOImpl IS-A IStudentDAO.

StudentDAOImpl à implements à IStudentDAO

 

3) SL (Service Layer)

Here we have an interface and a class for service layer too. The implementation and interface has IS-A relationship. But it also has relationship with IStudentDAO. It is HAS-A relationship

3.1) Interface

I___Service

IStudentService.java

IAdminService.java

3.2) Implementation

___ServiceImpl

StudentServiceImpl.java

AdminServiceImpl.java

Here,

StudentServiceImpl IS-A IStudentService

StudentServiceImpl àimplements à IStudentService

StudentServiceImpl  HAS-A IStudentDAO

StudentServiceImpl àUses à IStudentDAO

We use dependency injection of IStudentDAO into StudentServiceImpl and use it.

 

4) PL (Presentation Layer)

Last important component in our application is a controller

___Controller

StudentController.java

AdminController.java

Here,

___Controller HAS-A I___Service

StudentController HAS-A IStudentService

 

List of files

  1. ____.java
  2. I___DAO.java
  3. ___DAOImpl.java
  4. I___Service.java
  5. ___ServiceImpl.java
  6. ___Controller.java

This file structure is used by every Java EE application. But spring provides us some tools that simplifies this for us.

Microservices Architecture

Microservices

To understand microservices, we first need to understand what monolithic architecture is. Monolithic application in that in which all the modules are in the same application. That can be run as a single unit. Although the codebase can be little bit smaller, there are different inherent problems. For our example, lets consider College management system.

College Management system modules

  • Registration module – managing all student registration process
  • Accounting module – managing all the money related transactions
  • Staff management – Manages all the staffs, attendance, etc.
  • Examination management – Managing student examinations
  • Library management – Manages books and reading resources for the college
  • Records management – Managing old records and current records about students

All the above modules and more if required are present in the single application. Overtime, as this application grows, this will generate a very large codebase. It will be very difficult to fix a simple bug

  • Huge codebase – Code from all the modules in single place
  • Hard time to fix a bug – Searching for bug is very hard in huge codebase
  • Deploy entire application – Every time we fix a bug or roll out new features
  • Difficult to add new features – Other modules may be impacted. We must make sure it will not break other features.

On the contrary, microservices are small and focused services. We gather the smaller pieces which perform the same feature of monolithic application. We can create smaller services of each of different modules. Code boundaries for each of the modules are defined by the business boundaries or the business problems they are solving. That way we can avoid them to grow too large to maintain.

They are

Small and focused

Focused on single business problem and focused on the specific goal of that modal. Code does not spill beyond the business boundaries.

Autonomous

Every microservice is autonomous i.e. they are full fledged application on their own. Independent of other services.

Independent servers

Microservices are Packaged and deployed to their own machines or servers.

  • Registration module – Server1
  • Accounting module – Server2, Server3
  • Staff management – Server4
  • Examination management – Server5
  • Library management – Server6
  • Records management – Server7, Server8

API

Expose out the API, so that other services consume that API to communicate.

Network calls

All Communication between them occurs through network calls to the exposed API. Accounting, staff, examination, Library management uses information from the registration module. Accounting and paycheck management uses information from records management and staff management and so on

If we can change and deploy our application without changing or impacting other services that use our services, then it is a good microservice

Properties of Microservices

Heterogenous

Each of our microservices can be heterogenous. That means each of the microservices can be written in a different programming language and can be deployed in different platform and operating systems. They can communicate through API they expose, which usually are REST. For example, in our college management system, with registration module, examination module, accounting module, staff management module, library management module etc. One of them can be written in Java, another in Python, some other in .net, some in NodeJs, Some in PHP and so on.

College Management system modules can be coded and deployed in different languages, technologies, and platforms. We can choose a technology that suits a particular requirement. Use whichever system is better suited to the given job.

  • Registration - Python
  • Accounting - Java
  • Staff management - .Net
  • Examination management - NodeJs
  • Library management - Perl
  • Records management - Php
  • We cannot do that in monolithic applications. We have to write all the modules in same language and deploy to a same server.

    Migration to better technology

    It is harder to migrate to a new and better technology which is out in the market in the future, because we have to migrate the entire application. But in Microservices, we can migrate module by module basis without impacting the whole application.

    Robustness

    Robustness means if some part of the application fails, it will not bring down the entire application. In monolithic application, a small failure can bring down entire application. But in monolithic architecture, we can gracefully degrade that specific service and still run all other services.

    If the examination management module goes down, entire college management system does not go down. Other services will run as usual. They can make registration, run accounting system, lend books in library and so on.

    Scalable

    In monolithic applications, if some part needs to serve a large amount of traffic, then we have to deploy whole system to multiple and better server.

    For example in the registration season, lot of registration is happening. During examinations, examination management system is used exclusively and there is load in library management too. But other parts can have low levels of usage.

    In microservices we can deploy only those specific modules to more servers and leave others, depending on the number of users that are using that module or microservices.

    • Registration module – Server1, Server2 … more servers
    • Accounting module –Server3
    • Staff management – Server4
    • Examination management – Server5, Server6 … more servers
    • Library management – Server7
    • Records management – Server8

    Easy to deploy

    In monolithic application, we have to deploy entire application even to fix a small bug fix. In microservices architecture however, we can deploy the specific service. Even a new feature can be added and deployed to the server quite easily. This will not impact the whole application and it continues to run as usual.

    Reusable and replaceable

    As each of microservices use one another it should be easy to replace them. For example, registration module is used by examination, library, accounting system. Microservices are highly reusable

    Microservices are easily replaceable. If we have the same service from different vendor, or new and better system developed for registration, we can easily replace the old one with the third party system.

    String Manipulation in Java

    Strings are one of the simplest datatypes in Java, but allows great flexibility. I present the mostly used methods used in string manipulation in Java. Strings are immutable - meaning it cannot be changed. If we need to change anything, we create new String object, with a change. String object support many string manipulation functions. The most commonly used methods are described in this post.

    Let us define a string first

    Strings are defined with String keyword.  We must know the difference between char and string. The primitive type char is one character and the class String is zero or more characters long. We use single quotes for char, and double quotes for String. We define character and string as follows

    char chr = 'a';
    String str = "Hello";

    Like in any other programming language, string indexes start at 0. The first letter is at 0th position.
    String s = 'Doing things in Java'
    //Doing things in Java
    //01234567890123456789





    To define more then one characters, we must define character array.
    char[] str1 = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
     
    To convert it back to string, we can use following line 
    String str2 = new String(str1);

    Accessing any character of string

    The programmers who come from C/C++ background, use string as array of chars. But in Java, String is a separate class. We can also define and use array of chars for strings, but String class supports many string manipulation functions, not available to array of chars. The Strings are immutable, and cannot be changed. New String should be created in every step. To get single character, we use get() method. Index of the character is passed to the get() method to get the character at specified position.
    s.get(6);
    //Returns 't' 
     

    Find the length of string 

    Length of string can be found with length() function. This function can be useful to avoid index out of bounds problem while manipulating strings. If we need to loop over each character in string, then we have to know the length of string first.
    //Length of string s
    int x =s.length() //value = 20


    Iterate over each characters in the String

    Let us now iterate over each character in the string. For that, we can use simple for loop, while loop.
    for (int i=0; i < x; i++){
        system.out.println("Char at position " + i + " is " + s.get(i));
    }

    It also works with advanced for statement introduced in Java 5. It works like foreach in other programming language:
    for (String s1 : s){
        system.out.println(s1);
    }

    The String class also uses iterator, and we can use iterator

    Find position where substring starts

    To find the index of the first occurrence of substring inside a string, we use indexOf() function. We can pass substring as only one argument in the string. It simply returns the position of string
    //First Occurrence of the search string
    s.indexOf("program") //value = 0
    //Another use
    s.indexOf("g") //3
    s.indexOf("f") //-1 (when it doesn't find, it returns -1)
    s.indexOf("g",4) //10 (second parameter is index Where to start search)

    //Test whether string starts or ends with specific string
    s.startsWith("Prog") //true
    s.startsWith("java") //false
    s.endsWith('Java')   //true

    //Return all characters starting at first parameter
    //and ending at second parameter,
    //but not including index at second parameter
    //length of substring in following = 7 - 4 = 3
    s.substring(4,7)     //ram (consist chars 4,5,6)

    //Change case
    s.toUpperCase() //PROGRAMMING IN JAVA

    Tuesday, January 24, 2023

    Java Persistence API - JPA, ORM, SQL, Hibernate

    Java persistence API is very useful. Data is the most important part of our application and it is the heart of our application. JPA is almost completely configured. It is a ORM (Object Relational Mapping) tool. ORM is the middle layer which manages communications to database with the application. Java is object oriented application, so there are objects in our application. Whenever we make any changes in object, that needs to be stored in database using SQL (Structured Query Language). But ORM tool automatically maps the objects to the relational database. So we don't have to write SQL queries ourselves.
    • It will create objects from database rows and give it to java application.
      While saving java application gives java objects to ORM and orm takes values from the objects and save to the database.
  • For example our table user in the database has 4 fields user (id, name, city, profile) Now to tell ORM how it needs to convert table to object, we create a class Class User { int id String name String city String profile } Now the ORM converts this class to table. and maps each variable in class to table column name. JPA J - Java P - Persistence A - API JPA is used to persist (make permanent) any data that has been handled through the application. JPA is only a specification given by oracle and we can use any implementation of JPA. Hibernate is one of the most popular ORM tool, which is implementation of JPA. other implementations include Eclipse link, Open JPA etc.

    JPA provides two important interfaces

    • EntityManagerFactory
    Provides Entity Manager object
      EntityManager
    Provides many methods to create, read, update, delete records
  • Spring boot makes easy to perform operation with JPA. For that, we need to add a new dependency spring-boot-starter-data-jpa When you add this dependency, all the settings to run spring boot jpa gets autoconfigured. In spring framework you need to specify configuration manually. Now we need User object, and create UserRepository If we want to handle object User, First extend UserRepository to CRUDRepository. This CRUDRepository interface in Spring boot provides all functionality to manage data. We inject this UserRepository anywhere we require, then all the implementation will come with this. Insted of CRUDRepository we can also extend JPARepository, which is child of CRUDRepository. JPARepository provides extra functionalities like pagination, flush database etc. If we want to do any operation, - Create entity of that operation, e.g. User, Product, employee, client etc. - Create repository out of that entity e.g. UserRepository, ClientRepository, ProductRepository etc. - Now extend that repository to CRUDRepository or JPARepository - Now we get basic functionality of CRUD operation. No need to do any other coding. - If we need some extra features, which is not available in CRUDRepository or JPARepository, we can customize later.

    Do it yourself

    Create a project from start.spring.io Select Java Maven Project with appropriate names. Add dependencies MySQL Driver Spring Data JPA If you want to make web based app, use Spring web too

    Sunday, January 22, 2023

    Spring Boot configuration through - application.properties

    Spring boot comes with preconfigured application. We don't need to change anything to simply run the code. But if we want to change some of those default configurations to some customized configurations, we can use a file this method. For example if we want to connect to database and access the data, Spring Boot automatically provides configuration for almost anything, but, we also need to provide our database credentials, context path, etc. to run the program. So in that scenario, we need to provide those information to the application. There is a file named application.properties which can be used to store all the configuration properties in the spring boot application. These properties can include the port number on which we can run spring boot program, view settings, database and JPA settings and more. This file is in the folder /src/main/resources. There is also another file named application.yml which can be used to change the configuration. But the most common is using application.properties. There are different types of properties that can be changed through this file
    1. Core properties
      Cache properties
      Mail properties
      JSON properties
      Data properties
      Transaction properties
      Data migration properties
      Integration properties
      Web properties
      Templating properties
      Server properties
      Security properties
      RSocket properties
      Actuator properties
      Devtools properties
      Testing properties
  • We describe wome common properties we encounter while coding spring boot application.

    Changing port number on which we run spring boot application

    Spring boot comes with preconfigured port number on which the application is run. The default port number is 8080. But if the port is already being used or we want to use other port. e.g. 9000, then we can use this property to change it. server.port = 9000

    Change the view suffix and prefix

    The default folder containing view files is /src/webapps. If we want to put our view files in /src/webapps/views, then the first command does this. The second command tells spring to take .jsp as the defailt suffix or default extension that is used as view file spring.mvc.view.prefix=/views/ spring.mvc.view.suffix=.jsp

    Context path

    By default the context path is not available. If we want it, we can use following server.servlet.context-path=myapp

    Database url configuration

    We also need to use database confituration to use database with our spring application. Following properties are used to change the database credentials, and jpa properties. spring.datasource.url=jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update

    How to write new spring boot application - Step by step guide for beginners

    Java is a great language that can run on any ecosystem. Spring framework was a great addition to java ecosystem, but it was built on philosophy of configuration over convention. That made the program to be configurable to great extent, and make any application that programmer liked. But at the same time, made it extremely difficult to learn, code, manage, migrate, deploy for beginners with limited knowledge. Getting help over specific topic was very hard as anything can be done through several methods, and lot could go wrong. Spring boot makes it easier for us to write java program with ease, using the flexibility of spring framework. Spring boot is built on top of spring framework with philosophy of convention over configuration. Spring boot made easier for people to learn, get help when stuck, manage, migrate and deploy. It came in full package, from dependencies to the embedded server, software metrics, status etc. Beginners could make smart production ready software with less time they can use auto configuration, which reduces the hassle of setting up ton of configurations to get it run.

    Steps to follow to learn Spring Boot

    1. Introduction to Spring Boot—Goals and Important Features
    2. Create Spring Applications Prior to Spring Boot
    3. Creating a Spring Boot Application with Spring Initializr
    4. Creating a Simple REST Controller
    5. What is Spring Boot Auto Configuration? Spring-based applications have a lot of configuration. When we use Spring MVC, we need to configure component scan, dispatcher servlet, a view resolver, and web jars(for delivering static content), among other things. When we use Hibernate/JPA, we would need to configure a datasource, an entity manager factory, and a transaction manager, among a host of other things. The Spring Boot brings in a new thought process around this: Can we bring more intelligence into this? When a spring mvc jar is added into an application, can we auto configure some beans automatically?
    6. Spring Boot vs. Spring vs. Spring MVC
      • Spring is about Dependency Injection. It makes it easy to develop loosely coupled applications. It makes applications testable.
      • Spring MVC brings loose coupling to web MVC application development with features like Dispatcher Servlet, View Resolver, etc.
      • Spring Boot eliminates the need for manual configuration with Spring and Spring MVC. You can use Spring and Spring MVC without needing a lot of configuration.
      • Spring Boot aims to enable production-ready applications in quick time.
      • Actuator : Enables Advanced Monitoring and Tracing of applications.
      • Embedded Server Integrations : Because the server is built into the application, I don’t need to install a separate application server on the server.
      • Default Error Handling
    7. Spring Boot Starter Projects: Starter Web and Starter JPA Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.
    8. A Summary of the Different Spring Boot Starter Projects As we see from Spring Boot Starter Web, starter projects help us to quickly get started with developing specific types of applications.
      • SOAP Web Services and RESTful applications
      • Unit Testing and Integration Testing
      • Add HATEOAS features to your services.
      • Authentication and Authorization using Spring Security
      • Spring Data JPA with Hibernate
      • Enabling Spring Framework’s caching support
      • Expose Simple REST Services using Spring Data REST
      • To add advanced features such as monitoring and tracing to your application right away.
      • To pick your specific choice of Embedded Servlet Container
      • For Logging using Logback
      • Logging using Log4j2
    9. Spring Boot Actuator The Spring Boot starter actuator actually exposes a lot of REST services, and these services are compliant with the standard called the HAL standard. And we would use a hal explorer so that we can browse through the data which is provided by these services. The Spring Boot Actuator exposes a lot of data-application info: metrics, dump, beans, env, config properties, audit events, heap dump, loggers, trace, health mappings, and auto config. The Actuator provides more metadata about your application.
    10. Spring Boot Developer Tools