Skip to main content

Posts

Observer Design Pattern In Java

Greetings! Greetings! Observer design pattern falls into behavioral category. Observer design pattern is useful when we have multiple objects doing things according to another objects changes. This main object is callsed Subject and depending objects are called Observers. Subject and observers are loosely coupled and have no knowledge of each other. Subject(one) ----> Observer(many) Swing event listner, Spring listner are some examples for Observer desgin pattern. Observer design pattern according to GoF, "Define a one-to-many dependency between objects so that when one object changes state, all its dependencies are notified and updated automatically." Subject Maintain a list of observers who are interested in getting notified when something useful is happend. This provides methods to register the Observer, unregister the Observer and a method to notify all Observers at once. Observer Object which is interested in Subject's changes. Has ...

How to dockerize a spring boot application

Greetings! Spring boot helps us to create application very quickly. Docker provides us a way to "build, ship and run" our applications. In a world of Microservices, combining these two gives us powerful ways to create and distribute our Java applications. I assume you have enough Spring boot and Docker knowledge and want study further to dockerize Spring boot applications. https://github.com/slmanju/springtime/tree/master/spring-boot-docker Let's create a simple rest service and dockerize it. Step 1: Create Spring boot application Go to https://start.spring.io and fill it as you want. Then add Spring Web Starter as a dependency. This is enough for us create a simple rest service. Download and extract the application. Then add below controller (or anything you like). package com.slmanju.springbootdocker; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeC...

How to use Spring Boot with MySQL database

Greetings! Spring Framework simplifies working with databases by auto configuring connections, handling transactions, using ORM tool like hibernate, abstract sql by Spring Data Repository. We are going to focus on how to connect MySQL database with Spring Boot application. Sprint Boot has many defaults. For databases, H2 in-memory database is the default database. It auto-configures in-memory databases even without connection url. Those are good for simple testing. For production use we need to use a database like MySQL. Spring Boot selects HickariCP Datasource due to it is performance. When spring-boot-starter-data-jpa dependency in classpath it automatically pick HickariCP. complete source code this blog post is here . How to configure a database Obviously, to use a database in our application we need; Database driver to connect to database Connection url Database username and password In Spring Boot application we need to provide atleast connection url, otherwise i...

Microservices - Distributed tracing with Spring Cloud Sleuth and Zipkin

Greetings! Microservices are very flexible. We can have multiple microservices for each domain and interact as necessary. But it comes with a price. It becomes very complex when the number of microservices grow. Imagine a situation where you found a bug or slowness in the system. How do you find the root cause by examinig logs? Collect all the logs from related microservices. Pick the starting microservice and find a clue there using some id (userid, businessid, etc). Pick the next microservice and check whether the previous information are there. Keep going until you find which microservice has the bug. I have followed that practise in one of my previous projects. It is very difficult and takes a lot of time to track an issue. This is why we need to use distributed tracing in microservices. One place where we can go and see the entire trace. It helps us by; Asign unique id (correlation id) to all request. Pass unique id across all the microservices automatically. Re...

Cracking Java8 Stream Interview Question

Greetings! I have faced many interviews (too many to be frank) in my career as a software developer. In most of those interviews I have been asked to write a pseudo code for a given problem and implement it in Java. With Java8, this implementation mostly should be in Java8. When I look back all those questions it can be simplified as below. (difficulty may be vary though). Iterate over a given collection (stream) Filter the given data (filter) Transform into another format (map, reduce) Collect data into a collection (collect) or End the stream (forEach, min, etc) Is this familiar to you? This should be. This is what we do in our daily work. But, if you are blindly using it you will see it as a difficult question to answer. You need to have good understanding about intermediate and terminal operations. And, you need to really practise and use in daily work. It is meaningless to pass an interview without knowing these. java-8-streams-intermediate-operations java-8-stream...