Posts

Showing posts with the label java8

Microservices: Service Registry and Discovery

Image
In my previous blog, I have already talked about Netflix Hystrix Circuit Breaker Microservices and explained it by creating two applications i.e. Product Service and Price Service. In this post, I’ll use both applications to explain what is the Service Registry and Discovery Server. Circuit Breaker and Microservices Architecture Netflix Hystrix Circuit Breaker   What is Service Registry and Discovery and why do we need it in the first place? In our monolithic application, mostly service invoke one another through language methods or procedure calls and even in traditional distributed system deployment, services run at fixed, well-known locations (hosts and ports) and so can easily call one another using HTTP/REST. Over here the network locations of service instances are relatively static. With the microservice architecture system, this is a much more difficult problem as service instances have dynamically assigned network locations. Service instances change dynamicall...

Java 8 - Optional

Java 8 - Optional In this blog, I’ll discuss about Optional class which was introduced in JDK8 under java.util.package. Optional class is used to represent a value is present or absent. With Optional class, developers have to less worry about NullPointerException and can work on neat and clean code. It contains at most one value. Advantages: · No need to check for null · No more NullPointerException at runtime Public final class Optional<T> extends Object { } Let’s understand all its usage with the help of example 1. Optional.empty() To create an empty Optional object we simply need to write // How to create empty Optional              Optional<String> optional = Optional. empty ();              System. out .println( "1. Optional: " + optional );     Output:             1. Optional: Optiona...

Supplier Functional Interface - JAVA 8

Supplier Functional Interface Supplier is an in-built functional interface introduced in Java 8 in the  java.util.function  package. Supplier does not expect any input but returns the output. This interface can be handy where you want to generate some data like OTP, currentTime or even GUID The functional method of Supplier is get(T t). Here is a simple source code of java.util.function.Supplier @FunctionalInterface public interface Supplier<T> {     /**      * Gets a result.      * @return a result      */     T get(); } Where T get () is an abstract method where T is the return type of the function. Example 1: import java.util.Date; import java.util.function.Supplier; public class SupplierExample {        public static void main(String[] args ) {    ...