Posts

Showing posts from February, 2013

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...

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...