Posts

Observability Done Right: Best Practices and Anti-Patterns for Effective System Monitoring

Image
  WHAT Observability is a concept that refers to the ability to gain insights into the behavior and performance of complex systems. In the context of software engineering, observability involves the collection, analysis, and visualization of data from software applications, infrastructure, and other components of a system. In the animal kingdom, observability plays a critical role in survival, allowing animals to monitor their surroundings, detect threats, and find food. Dolphins use echolocation to observe their surroundings. They emit high-frequency sounds that bounce off objects, allowing them to create a 3D map of their environment. Thanks for reading Knowledge Cafe! Subscribe for free to receive new posts and support my work. Subscribed WHY In today's era, architectures are becoming increasingly large, complex, and fast-paced due to the faster development and deployment of software by distributed teams with the help of DevOps, continuous delivery, and agile development methodo...

JDBC Interview Question

Java Database Connectivity API contains commonly asked Java interview questions. A good understanding of JDBC API is required to understand and leverage many powerful features of Java technology. Here are few important practical questions and answers which can be asked in a Core Java JDBC interview. [expand title="What are available drivers in JDBC?"] JDBC technology drivers fit into one of four categories: A  JDBC-ODBC bridge  provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. For information on the JDBC-ODBC bridge driver provided by Sun, see  JDBC-ODBC Bridge Driver . A  native-API partly Java technology-enabled driver  converts JDBC calls into calls on the client API for...

Java Collection Interview Questions-2

Java Collections Framework are the fundamental aspect of java programming language. It’s one of the important topic for java interview questions. Here I am listing some important questions and answers for java collections framework. [expand title="What is an Iterator"] Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.[/expand]   [expand title="Difference between HashMap and HashTable? Compare Hashtable vs HashMap?"] Both Hashtable & HashMap provide key-value access to data. The Hashtable is one of the original collection classes in Java (also called as legacy classes). HashMap is part of the new Collectio...

Java Collection Interview Questions-1

Java Collections Framework are the fundamental aspect of java programming language. It’s one of the important topic for java interview questions. Here I am listing some important questions and answers for java collections framework. [expand title="What is Java Collections API?"] Java Collections framework API is a unified architecture for representing and manipulating collections. The API contains Interfaces, Implementations & Algorithm to help java programmer in everyday programming. In nutshell, this API does 6 things at high level Reduces programming efforts. - Increases program speed and quality. Allows interoperability among unrelated APIs. Reduces effort to learn and to use new APIs. Reduces effort to design new APIs. Encourages & Fosters software reuse. To be specific, There are six collection java interfaces. The most basic interface is Collection. Three interfaces extend Collection: Set, List, and SortedSet. The other two collection interfaces, Map and Sorted...

Spring Interview Questions

Image
Spring interview questions  is in rise on J2EE and core Java interviews,  As Spring is the best framework available for Java application development and now  Spring IOC container  and Spring MVC framework are used as de-facto framework for all new Java development. We will be adding the more number of questions from readers request. If you are looking for any specific questions and doubts, please post your queries in the comments section of this article. We will update the questions and send you the remainder about the update.  Also don’t forget to leave the feedback about the questions in this article and provide suggestions. [expand title="(1)What is Spring Framework?"] Spring  is a lightweight inversion of control and aspect-oriented container framework. Spring Framework’s contribution towards java community is immense and spring community is the largest and most innovative community by size. They have numerous projects under their portfolio and have their own spring dmSer...

How to create Immutable Class in Java

Writing or creating immutable classes in Java is becoming popular day by day, because of concurrency and multithreading advantage provided by immutable objects. Immutable objects offers several benefits over conventional mutable object, especially while creating concurrent Java application. Immutable object not only guarantees safe publication of object’s state, but also can be shared among other threads without any external synchronization. In fact JDK itself contains several immutable classes like String, Integer and other wrapper classes. For those, who doesn’t know what is immutable class or object, Immutable objects are those, whose state can not be changed once created e.g. java.lang.String, once created can not be modified e.g. trim, uppercase, lowercase. All modification in String result in new object, see why String is immutable in Java for more details. In this Java programming tutorial, we will learn, how to write immutable class in Java or how to make a class immutable. By ...

Spring Interceptor for logging

Spring Interceptors has the ability to pre-handle and post-handle the web requests. Each interceptor class should extend the  HandlerInterceptorAdapter  class. Here we will create a Logger Interceptor by extending the  HandlerInterceptorAdapter  class. You can override any of the three callback methods preHandle() ,  postHandle()  and  afterCompletion() . As the names indicate the  preHandle()  method will be called before handling the request, the  postHandle()  method will be called after handling the request and the  afterCompletion()  method will be called after rendering the view. In each method we will log information using log4j. First instantiate the logger in the static context, then set up the basic configuration so that the log messages will be logged on the console. The  LoggerInterceptor  class is shown below. package sct.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.BasicConfigurator; impo...

Spring JDBC

In this example you will learn how the Spring JDBCTemplate simplifies the code you need to write to perform the database-related operations. The insertForum() method below shows the amount of code you need to write to insert data using JDBC. package com.sct.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import com.sct.domain.Forum; public class JDBCForumDAOImpl implements ForumDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void insertForum(Forum forum) { /** * Specify the statement */ String query = "INSERT INTO FORUMS (FORUM_ID, FORUM_NAME, FORUM_DESC) VALUES (?,?,?)"; /** * Define the connection and preparedStatement parameters */ Connection connection = null; PreparedStatement preparedStatement = null; try { /** * Open the connection */ connect...

Java 7-Precise Rethrow Exception

Previously, rethrowing an exception was treated as throwing the type of the catch parameter. For example, let's say that your try block could throw a ParseException or an IOException . To intercept all exceptions and rethrow them, you would have to catch Exception and declare your method as throwing an Exception . This is "imprecise rethrow", because you are throwing a general Exception type (instead of specific ones) and statements calling your method need to catch this general Exception . This is illustrated below: //imprecise rethrow. //must use "throws Exception" public static void imprecise() throws Exception{ try { new SimpleDateFormat("yyyyMMdd").parse("foo"); new FileReader("file.txt").read(); } catch (Exception e) { System.out.println("Caught exception: " + e.getMessage()); throw e; } } However, in Java 7, you can be more precise about the exception types being rethr...

New Java 7 Features: The Try-with-resources Language Enhancement

This article examines the use of the  try-with-resources  statement. This is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it.   The  try-with-resources  statement ensures that each resource is closed at the end of the statement. Any object that implements the java.lang.AutoCloseable or java.io.Closeable interface can be used as a resource. Prior to  try-with-resources  (before Java 7) while dealing with SQL Statement or ResultSet or Connection objects or other IO objects one had to explicitly close the resource. So one would write something like: try{ //Create a resource- R } catch(SomeException e){ //Handle the exception } finally{ //if resource R is not null then try{ //close the resource } catch(SomeOtherException ex){ } } We have to explicitly close the resource and thereby add a few more lines of code. There are few cases where the developer will forget to cl...

Code Refactoring

Continuous Refactoring  by Michael Hunger Code bases that are not cared for tend to rot. When a line of code is written it captures the information, knowledge, and skill you had at that moment. As you continue to learn and improve, acquiring new knowledge, many lines of code become less and less appropriate with the passage of time. Although your initial solution solved the problem, you discover better ways to do so. It is clearly wrong to deny the code the chance to grow with knowledge and abilities. While reading, maintaining, and writing code you begin to spot pathologies, often referred to as  code smells . Do you notice any of the following? Duplication, near and far Inconsistent or uninformative names Long blocks of code Unintelligible boolean expressions Long sequences of conditionals Working in the intestines of other units (objects, modules) Objects exposing their internal state When you have the opportunity, try deodorizing the smelly code. Don’t rush. Just take...