Skip to main content

Posts

Showing posts from 2022

pages in next.js

Introduction Next.js contains a feature called as Pages that helps us to maintain a clear isolation of the page level components and their layouts. This helps us in having page and routing implementations separate from the components, helping in isolation of the application layout / presentation with that of the functional components This segregation helps us in having slim components and isolate the layout / page level actions. Also, since there are pages, the components that comprise a page atleast at the higher level can be easily found out in this system. Problem Statement We have been using react.js for building SPA (Single Page Applications) for a while and we experienced that some features like pages and routing are not very comfortable with react.js Compared with other frameworks, react does not have option to fully perform the routing and having pages. There are times where we have to duplicate the layouts to all components that act like pages in the react application.

How to make table schema changes and restore data after appropriate transformation

Introduction In many cases we might have to encounter scenarios where we need to perform backup of data or perform a schema change like data type transformation etc. This blog post illustrates the steps that can be used as a checklist to perform the operation at ease Steps The below sql query creates a new table with the same schema as given in the like field create table <table_name>_old like <table_name>; Now that we have the schema ready, we can copy the data for backup using the below command insert into <table_name>_old select * from <table_name>; Once the above command succeeds, we can drop the old table drop table <table_name>; Then we can create the new table with the same name as the one given in <table_name>   so that we can allow applications to still use the same table name. CREATE TABLE `<table_name>` (   `id` bigint NOT NULL AUTO_INCREMENT, ...); Now that the table with new schema is ready, we can migrate the data

using mysqldump command to backup the data from MySQL Database table

Introduction In this blog post, we will take a look at the mysqldump command that is shipped as part of the MySQL clients. Data Backup We normally deal with backup of data by exporting the data as JSON / CSV or as SQL Files. This is required so that any change that we are working on might have a impact, we can immediately use the backup to fix any unexpected database operation. When working with JSON / CSV at times, we have to deal with the issues in the data formats. Further as SQL queries, we get the option to rewrite the query, insert them into a local database, perform the required transform or operations and then move to the server. The MySQL Workbench provides the export option through which we can generate the SQL scripts, but there are issues like the target table names are empty and there is a new insert statement for each row in the table. Example Command mysqldump.exe --host=servername --port= 3306 --default-character-set=utf8 --user=(db_user_name) -p --protocol=tcp --colu

http/3

Introduction HTTP/3 is the 3rd major version of the HTTP protocol that powers the internet. In comparison with the previous version of http which relied on TCP, http/3 relies on QUIC. QUIC Specification published at 6th June, 2022 Developed initially at Google in 2012, had its way to IETF and public by 2022. A decade for completion and standardization !!! Are the semantics like requests, methods, status codes still the same like previous versions of http/2? yes, they still remain the same The underlying mechanisms are changed, like http/3 uses space congestion control over UDP (User Datagram protocol) Head-Of-Line Blocking (HOL) This is a performance problem where there is a queue of packets built due to the first packet that is yet to be consumed. We have browsers that have limits on the number of parallel requests that it can send to a server, when they are used up, as we anticipate, a Queue is formed for the newer requests that start accumulating the newer requests till the former

Split a very large file in to chunks

Introduction Many a times we are having to deal with the very large files, be it the logs from the server like catalina.out    or any other huge sql files or csv or text files. These are some artifacts that could have been generated in the server or might contain the data. There might not have been detailed logging systems that can do a RollingFile like appender that can get the files into smaller chunks for us to use easily. In some cases these could be large data which is hard to split into multiple chunks in an export mechanism. Problem Statement When we try to open them in an editor like  Notepad / Notepad++ / VS Code , we normally run into memory issues and the application gets frozen. At times, in case of windows, we might have to go to  Task Manager  , find the application and then  End Task  so that we can use the other apps or to allow that particular application to be usage Solution In this post, we explore on using git bash a tool. We normally use a git bash command line in

Upgrade from http1.1 to http2 for Java spring boot applications hosted in tomcat

In this post, we will list down the tasks to be done for enabling the HTTP 2.0 support in spring boot applications which are hosted in Apache tomcat webserver Application Level Changes Spring boot Application Configuration Changes server.http2.enabled=true In the spring boot application's application.properties file, we have to add the above line so that Spring boot can add the support for http2 Tomcat server configuration In the tomcat web server, we should have SSL enabled before doing the below change. To start with, we have to shutdown the tomcat server instance that is running CD to the directory that has tomcat installed and cd to the bin directory and run the below command sh shutdown.sh We have add the UpgradeProtocol  which adds the respective Http2Protocol handler classname to the connector pipeline that enables support for http2.0 <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" /> The above UpgradeProtocol can be added to the connec

Jsonview in Spring boot

Introduction There are scenarios where we have a DTO or a model class that contains a collection of properties and there might be some endpoints that require a subset of them and some other endpoint that might need all of them. Example: Consider a scenario where we need a list of Categories in a dropdown and we have an API that gets all the categories from the static masterdata and need to send to the UI in response to an API call. Problem Statement  We might choose to use a `Map` data type so that we send out the key-value pairs to the database or to choose to build a custom DTO with only the Id, name of the categories. In a longer run, if we opt to use multiple DTO in the source, sooner there will be a very large collection of DTO classes that represent subset of a static data. Solution Rather than ending up with a large number of such DTO classes, we can use a JSON View. This helps us have a single class and control which fields are part of which view.  Example: We can have a dropd

Handling exceptions in the Executor service threads in Java

Introduction This is a continuation post on the exception handling strategies in the threads in Java. For Introduction, please read this post The second post is available here This post addresses the problem statement "How to use the exception handlers in the threads spawned by the Executor Service in Java?" Not all times, we will be using Thread  classes to run our threads because we have to manage a lot of the underlying logic for managing threads. There is ExecutorService in Java which comes to the rescue for the above problem. In the previous posts, we have discussed on how to handle the exceptions in plain threads. However, when using executor service, we do not create / manage threads, so how do we handle exception in this case. We have a ThreadFactory   as an argument which can be used to customize the way threads are created for use within the ExecutorService . The below snippet of code leverages this feature to illustrate the exception handling, wherein we create a

Handling exceptions at thread level in Java

Introduction This is a continuation to the post on exception handling on threads. For those interested in the background, please visit this link This post discusses on the topic "Can I use separate exception handlers for threads performing mission critical tasks vs non trivial tasks?" Per-Thread Exception Handler Let's us assume that we built a handler that wants to notify the support team that something has gone wrong in some special thread processing, like the one below (not really notifying anyone but helps correlate the context) class SupportNotificationExceptionHandler implements Thread . UncaughtExceptionHandler { @Override public void uncaughtException ( Thread t, Throwable e) { notifySupport (t, e); } private void notifySupport ( Thread t, Throwable e) { System . out . println ( "Notified support team on: " + e. getMessage () + " raised from : " + t. getName ()); } } Let us now apply the above special handle

Handling Exceptions in Threads in Java

Introduction Exception handling is very critical to any application and any language that was used to develop. This helps the following stakeholders Stakeholders End-User: A graceful message that indicates that something has gone wrong in the system due to some wrong input or due to some internal fault Developer A clean way to get to know which line of code caused the exception and the stack trace that can help us reach to the points impacted Support Team A helpful hint that they can try to verify and resolve if it is something within their reach or to escalate to the product team SRE (Site Reliability Engineering) An indication of how far the application is performing w.r.to being reliable to the end-users and how faults are handled or tolerated etc.. Problem Statement When we use threads to get the job done for performing some intensive task (computation or Network intensive tasks), it becomes difficult to track down on the exceptions. This post is one in a series of posts that will

reified type in Kotlin

Introduction In Java, we have a type erasure problem where the runtime will not be able to preserve the type of the generic type that was intended and that will result in many times we having to pass the name of the class like Class<clz> ..  to provide the context information to perform the operations in generics. Type Erasure fun < T > getOnlyInts ( list : List < Any >): MutableList < T > { var result : MutableList < T > = mutableListOf () for ( item in list ) if ( item is T ) { result .add( item ) } return result } The above snippet of code illustrates the problem of type erasure where the type information for T is lost at runtime. this results in the type check failure in the line item is T Since the type data is erased , the runtime cannot figure out if it is performing the right type check. Hence the compilation fails. reiefied In Kotlin, we have the reified types which helps us to preserve the type information

sequences in Kotlin

Introduction Sequences in Kotlin are similar to that of the stream s in Java, where in the evaluation happens lazily and the volume of data processed in each step in the chain are reducing based on the met criteria. Sample Let us consider the below given sample data class data class Car ( val model : String , val year : Int ) We can create a collection of cars so that we can evaluate both and understand the difference between collections and sequence   in Kotlin var cars = listOf ( Car ( "Toyota" , 2021), Car ( model = "Tesla-S" , 2022), Car ( "Mercedes - Maybach" , 2022) ) Sequence In the below given snippet of code, we are performing the filtering of cars by name and then printing the year println ( cars . asSequence (). filter { it . model == "Toyota" } . map { it . year } This produces the output as given below kotlin.sequences.TransformingSequence@b97c004a This is because the sequence is lazy and is not evaluated as there is

when blocks in Kotlin

when This post shows multiple ways in which we can use the when  block in Kotlin We can start with a typical use of when (like in switch statements) enum class Season { SPRING , SUMMER , FALL , WINTER } To execute logic based on the season, we write the below snippet of code when ( timeOfYear ) { Season . SPRING -> println(" Flowers are blooming") Season . SUMMER -> println("It's hot!") Season . FALL -> println("It's getting cooler") Season . WINTER -> println("I need a coat") } In case we wanted to get the value rather than print based on the timeOfYear, we can use the below snippet of code, where we return the value and it can be returned in the overall when block val str = when ( timeOfYear ) { Season . SPRING -> "Flowers are blooming" Season . SUMMER -> "It's hot!" Season . FALL -> "It's getting cooler" Season . WINTER -> { "

Destructuring in Kotlin

Introduction This is a process in which we can be able to extract properties of interest from an object  / collection. We have been used to using destructuring in javascript and other functional languages whereas in Java / C# we have not been able to do this. However, kotlin has added the functionality to destructure classes by properties, collections, maps etc. Destructuring classes To enable a class to be destructured, should we do anything in kotlin? The answer is yes and no.. looks puzzling ain't it. Destructuring a class Below is a regular class (non data class) which needs implementation to allow it to be destructured class User ( var mail : String , var deptId : Long ) { operator fun component1 () = mail operator fun component2 () = deptId } Destructuring a data class Below is a declaration of a data class which comes with the support for destructuring for free as like toString, hashcode etc.. data class CompanyUser ( var mail : String , var deptId : Long ) Now that