A brief overview of Java-related backend technologies.
Java 8 to Java 11 Changes
Java 8 (LTS)
Below are the summary of changes. More details are provided on Java in a Nutshell article.
- Functional interface: An interface with exactly one abstract method is called functional interface. Also known as Single Abstract Method Interfaces (SAM Interfaces).
@FunctionalInterfaceannotation ensures about the restrictions and also we can use default methods in FI’s since they’re not abstract.
- Lambda expressions: Lambdas are nameless functions. Lambdas provide more brief and readable code. It eliminates the need of creating anonymous classes (source).
- Default methods: With default methods we can define default implementation of a method in an interface, so classes that extend the interface can directly use the method without providing any extra implementation information
- Streams API: A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
- DateTime API changes: The problematic
Dateclass has even become obsolete (ancient). The new classes intended to replace Date class are
- Collections: A Java collection framework provides an architecture to store and manipulate a group of objects.
- Project Jigsaw / Java Platform Module System (JPMS): The aim is to enhance modularity. The problem they’re working on is how to allow coarser-grained modules to interact while shielding their internals.In Jigsaw’s case, the coarser-grained modules include Java classes, packages, and their dependencies.
- Jshell: Interactive programming Read-Eval-Print-Loop (REPL) tool
- G1 Garbage Collector: Switching from parallel GC to G1 as default.
- Private methods in interfaces
- New methods to Optional class
- Improved parallellism of G1
Java 11 (LTS)
- Local variable syntax for Lambda variables: Now we can use “var” in lambdas
- Launch single file java programs: Now it is easier to run single file java programs. For example, “java myProgram.java” is enough.
Java 11 is not a huge improvement but it’is LTS like Java 8, thus it will be a substitute for Java 8.
- Hibernate is an ORM technology. Object-Relational Mapping (ORM) is a programming technique to provide mapping between objects and relational databases.
- Hibernate is a JPA implementation. Without a JPA implementation, nothing will happen. “JPA is the dance, Hibernate is the dancer. JPA is the art, Hibernate is the artist”.
- The Java Persistence API provides a specification for persisting, reading, and managing data from your Java object to relational tables in the database.
- Java Persistence API (JPA) is only a specification and that it needs an implementation to work. JPA includes EntityManager which contains common Create, Read, Update and Delete (CRUD) operations.
@Entitywe say that the object instance (entity) will be persisted in the database. The entity class is a Plain-Old-Java-Object (POJO) class. POJO is, basically, a simple class consisting of instance variables and setter/getters, it does not include any complex method.
- We use
@GeneratedValuefor id columns.
- We use
@Transientto not persist that field in the database. We also have
- JavaServer Faces is a standard Java framework for building user interfaces for Web applications. It is a server-side Java framework for web development.
- Most important, it simplifies the development of the user interface.
- JSF provides inbuilt AJAX support. So, you can render application request to server side without refreshing the web page.
- Xhtml is a extension of html and used to create facelets (simply put, JSF tags) page.
- JavaServer Faces provides rich set of components library to define the architecture of application.
- Maven is a build management tool.
- Project Object Model (POM) is a XML file that tells details to Maven on how to build the project. POM contains information related to dependencies, plugins, life cycles, phases, goals and profiles (development, test, local-dev).
- Thanks to Maven, we don’t need to download dependency JARs manually and we can easily configure our project build.
Spring is a popular framework that makes it easier to build web applications.
- It is a framework of frameworks. It has a number of modules that can work independently and adding only the needed ones is possible.
- Most important feature of Spring Framework is Dependency Injection. At the core of all Spring Modules is Dependency Injection or Inversion of Control.
Inversion of Control and Dependency Injection
Spring frameworks two important concepts are Inversion Of Control (IOC) and Dependency Injection (DI).
- Inversion of Control means, instead of objects being controlled, they will have control over their implementation. In IOC, components should only depend on abstractions of other components and are not be responsible for handling the creation of dependent objects.
- Dependency Injection is the process where the Spring Framework looks for the beans and identifies the dependencies and creates an instance of beans and autowires them.
- In the example below, we don’t use
userLister = new UserListerDB();We use injection and UserLister will have freedom over its implementation. Dependency Injection (DI ) is one way to achieve IOC. So when we use
@Injectannotation (another way is using XML file) the related constructor of the dependency will be called.
So basically we prevent the class from instantiating the dependencies (manually), using the annotation we call the related constructor automatically (DI). Thus the dependency class will have more control over its implementation (IOC).
- IOC & DI Benefits: We can create loosely coupled applications, make it easier to write unit tests (easy mocking) and let the dependency class have more control.
In a nutshell, Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
It provides a simpler and faster way to set up, configure, and run both simple and web-based applications. It smartly chooses your dependencies (starter projects), auto-configures all the features you will want to use (auto configuration), and you can start your application with one click.
- Auto configuration: Which problem does auto configuration solve? Spring based applications have a lot of configuration. When we use Spring MVC, we need to configure component scan, dispatcher servlet, a view resolver, 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, a transaction manager among a host of other things. Spring Boot intelligently configures all with
- Starter projects: Spring boot have starter projects that are all-in-one dependencies for our needs. For example, for a RESTful Web application development we just add
spring-boot-starter-webwhich includes 20+ dependencies.
Using Spring Initializr, we can easily set up our Spring Boot projects.
The Spring Web MVC framework provides Model-View-Controller (MVC) architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.
- The Model encapsulates the application data and in general they will consist of POJO.
- The View is responsible for rendering the model data and in general it generates HTML output that the client’s browser can interpret.
- The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.
REST (Representational State Transfer) refers to an architectural model for the design of web services. REST compliant systems are called RESTful. RESTful services are stateless and seperates client and server.
- Stateless: Server does not store history of requests. Each request has enough information. A client request provides two things: WHAT to do (
DELETE) with WHICH resource (URI, Uniform Resource Identifier).
- Seperated Client and Server: Implementation of client and server can be independent. As long as they know how to communicate, they can be modular and seperate.
In REST, a representation of object state can be in JSON, XML, HTTP or in Plain Text format.
REST vs SOAP: SOAP (Simple Object Access Protocol) uses XML. Parsing XML is more complex than JSON. SOAP exposes services interfaces while REST exposes URI endpoints. SOAP requires more resources than REST. REST is faster and requires less bandwidth. REST is easy to integrate to project as there’s no need to refactor the existing structure. With REST, a multiple data formats is available, while for SOAP we need to use XML only.
API (Application Programming Interface)
- Definition: API is a software intermediary that allows two applications to communicate with each other. It’s a bridge between two software.
- Definition: A web service is a software that provides functionalities for other software over the web.
API vs Web Services
- In an API, if the communication is over web, it’s called web service. All web services are APIs but not all APIs are web services.
- An endeavor to collaborate work of development and operations.
- DevOps has multiple definitions and various tools for that aim.
- Continuous Integration: publishing code to shared repository multiple times a day.
- Continuous Deployment: every change goes through the pipeline and deployed automatically
- Continuous Delivery: ability to deliver software to any given environment at any given point of time
- Continuous Testing: Executing automated tests after each change in the code
- Well-known DevOps tools are Docker, Jenkins, Ansible and Puppet. Docker provides self-sufficient cotainers for applications that work on DevOps fashion. Jenkins is a CI tool that analyzes code and spots bugs.
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker is a set of platform-as-a-service (PaaS) products that use operating-system-level virtualization to deliver software in packages called containers.
- Docker container: A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
- Container vs VM: Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Virtual machines (VMs) are an abstraction of physical hardware turning one server into many servers. The hypervisor allows multiple VMs to run on a single machine. Each VM includes a full copy of an operating system, the application, necessary binaries and libraries — taking up tens of GBs. VMs can also be slow to boot.
- Docker for DevOps: Simply put, Docker reduces time to deploy code to the production environment. It also ensures that if the code runs on dev-local that will run in the production too (thanks to containers being self-sufficient). It also allows developers to get a head start by using one of thousands of programs already designed to run in a Docker container as a part of their application. For operations staff, Docker gives flexibility and reduces resource usage.
- Kubernetes is a container management tool.
- Kubernetes takes care of deployment, scaling and management of containerized applications.
Monolithic vs SOA vs Microservices
- Briefly: All the software components of an application are assembled together and tightly packaged in monolothic architecture.
- Definition: Monolithic architecture pattern is the traditional architectural style that many systems utilize, with the monolith application built as a single, autonomous unit.
- Benefits: Simpler development and deployment and better performance. Details about the benefits; Simpler because, all actions are performed in one directory (shared codebase). Monolith is more are generally better performant than microservices (MSA) because while MSA might require 40 API calls whereas in monolith it’s faster communication due to the shared code and memory.
- Disadvantages: While this style has been an integral part of many businesses, its numerous limitations and issues are motivating more and more to make the switch to microservices. Monolithic structures make any changes to the application extremely slow as it often affects the entire system. It can require a completely rebuilt and deployed version of software whenever a modification is made to a small section of code.
Service Oriented Architecture (SOA)
- Briefly: SOA is essentially a collection of services.
- Definition: Service Oriented Architecture is a architectural design with the collection of services that communicate each other over a network.
- A service is a discrete unit of functionality that can be accessed remotely and acted upon and updated independently, such as retrieving a credit card statement online. A service is a black box to its customers.
- Different services can be used together to provide a larger functionality.
- While APIs are generally associated with REST/JSON and SOA is associated with XML and SOAP.
- Briefly: Microservices, is an architectural style that structures an application as a collection of small autonomous services modeled around a business domain.
- Benefits: Applications built as a set of independent, modular components are easier to test, maintain, and understand. Due to the services independency (loose coupling), its enables parallel development and also makes the system more scalable. Further, fault tolerance is increased since a failure on a service won’t crash the whole application.
XML and JSON are the two most common formats for data interchange in the Web today.
- XML stands for Extensive Markup Language, which is a data standart that defines data in a structured way.
- XML is used by SOA (Service Oriented Architecture).
- JSON is used by REST.
- Compared to XML, it’s easier to read, write and parse JSON.
- Definition: Dropwizard is a Java framework to build RESTful Web Services faster. It’s an alternative to Spring Boot.
- Dropwizard’s components: Jetty as HTTP Library (injects HTTP server to our project), Jersey as REST API implementation, Jackson for JSON-Object mapping, Jdbi for DB access with Hibernate support.
Dropwizard vs Spring Boot Comparison on Libraries
- HTTP: Jetty vs Tomcat
- REST: Jersey vs Spring
- JSON: Jackson vs Jackson, GSON, json-simple
- Testing: Both use Junit and Mockito
- Definition: Message broker is a software medium to establish communication between a sender and a receiver.
- Benefit: It’s useful to manage messaging especially when there’s a large amount of sender and/or receiver.
- There are various message broker products: RabbitMQ, Kafka, ActiveMQ…
What is AMQP?
Before discussing the message brokers, we should know about AMQP (Advanced Message Querying Protocol).
- AMQP is a messaging protocol with publishers, consumers and a message broker. Publishers publish messages, consumers consume the messages and it’s the job of the message broker to ensure the right message goes to the right consumer.
- RabbitMQ is a technology that implements AMQP in its core.
- RabbitMQ is a message-queueing software called a message broker or queue manager. Simply said; It is a software where queues can be defined, applications may connect to the queue and transfer a message onto it.
- Apache Kafka is a messaging system that uses publish-subscribe model. Publishers append events to distributed logs (topics) and consumers pick topics they will consume.
- Consumers decide what to consume. Thus, Kafka routing is simple. Kafka cluster typically consists of multiple brokers to maintain load balance. Kafka brokers are stateless, so they use ZooKeeper for maintaining their cluster state.
- Compared to RabbitMQ, Kafka has a simpler routing. Kafka is stateless, RabbitMQ is stateful. They are scalable, Kafka is horizontal scalable (scaling by adding more machines), RabbitMQ is vertical scalable (adding more power).
- Bootstrap is the most popular CSS Framework for developing responsive and mobile-first websites.
- Bootstrap also gives you the ability to easily create responsive designs
- Apache Cassandra is a column family based NoSQL DBMS.
- MongoDB & CouchBase are document based NoSQL DBMS. Documents can contain key-array pairs or key-value pairs or even nested documents.
- Redis is key, value based NoSQL DBMS. It’s also used as cache and message broker.
Netflix OSS (Eureka, Hystrix, Ribbon) & 12-factor APP, Kubernetes, AWS, Elasticsearch, TDD, Redis and MongoDB, JDBC