Wednesday, December 31, 2014

Using Krawkraw To Find Broken Links

I recently tweeted that I did just that: used Krawkraw to fish out broken links on the company's website.


From all the 187 pages, I was able to find the 8 broken links: the rogues!


Tuesday, December 30, 2014

Blowing a Gasket

It was the first time I ever sought out Alcohol to drown out the pain…

It was the period starting somewhere in the middle of 2013 to the early part of 2014. Life happened! Yes, life happened and I found myself in precarious situations I had little or no control over. It came with an unimaginable bout of anxiety and the feeling of helplessness. The anxiety was so much it seems it began to take a crack at my mind...

This was when I was made acutely aware of a distinct part of our well being as humans...our mental well being, and that it is as susceptible to malfunctioning and in need of care just as our physical well being...Not that I have never been through situations that were extremely stressful, I have...and I have my occasional bout of the blues, and fatigue...but none has ever matched the intense craziness of that period…

Things started happening…

It literally felt as if fire has been set to my brain...and then at some other times, it feels as if cold hands made of steel, were mercilessly being inserted into my cranium…

Then I became mildly scared of sleeping! Yes! The thought of letting go of my consciousness to be enveloped, by the brief oblivion that comes with sleep became such a terrifying thought…What if? What if what? I did not know. It just felt terrifying.

Thinking and concentration became a laborious task also, it felt I needed to apply as much as 10x the effort to perform simple cognitive tasks…remembering stuffs also literally hurts...

...and with all these pain going on inside, on the outside, I looked and acted absolutely normal!

And this was the most scary part. The fact that my mind had such capacity to unleash that amount of distress and pain, and yet it remains all invisible...at that point I understood how putting a gun to your temple could feel like the way out to some folks, how swallowing pills or slashing your wrist could feel like the escape yearned…

I had a brief empathy with certain dark corners of our humanity...I understood.

It was a period I would never have a repeat of. It has taught me the need to be gentle with life, with myself, to devout as much attention to mental well being as we do to our physical well being...we eat good, exercise, avoid toxic habits, so also is the need to avoid ultra intense situations, the benefit of surrounding oneself with positive thoughts and family, learn not to give a fuck, cast your cares, and even if life happens, learn how to still not give a fuck, or better still, if you can, know how to stick a middle finger back at life...just do about everything you can to shield yourself...

We owe ourselves this.


Friday, December 26, 2014

A web scraper/crawler in Java: Krawkraw

UPDATE: Krawkraw is now referred to as just Webmuncher.

Nobody sets out to write a web scraper, but here I am posting about just that, a general purpose web scraper I wrote…

How did this happen? Well, it started with wanting to play around with ElasticSearch a couple of months back. And in search of some data to index, I looked to websites' contents (with hindsight a more structured data format should have been sought, but well, the lesson has been learnt)...

So the question then was, how do I easily retrieve all the html pages of a site and have it thrown into ElasticSearch. Any easy web scraper in Java, out there that I could just use? I can’t even remember actively searching for such a tool...which explains why I somehow decided it would be much fulfilling if I throw a web scraper together...and in a couple of weekends, Krawkraw came to be.

Krawkraw is a tool that can be used to easily retrieve all the contents of a website. More accurately contents under a single domain. This is its perfect use case which reflects the original need for which it was written. So you can’t start at one edge of the web with Krawkraw and expect to crawl to another edge...Nope.

Krawkraw is available via Maven central, and you can easily drop it into your project with this coordinates:


com.blogspot.geekabyte.krawkraw
krawler
${krawkraw.version}


Or you can download the jar files here, and have it included in your classpath.

For more information on the API and its usage, The README should be your friend! If you happen to use Krawkraw and found it missing some features, or you have some ideas on some features that it should have? Then drop them in the issue tracker.

Wednesday, December 17, 2014

Hello Logging, Hello SLF4J

This is not a full blown guide, tutorial or introduction to logging in Java with the SLF4J abstraction, but a post that captures some of the nuances and components I personally had to get familiar with when I stopped seeing logging as an activity that happens only during development, to something that is actively build into software so as to have an insight into its state in production.

Personally...In the early days it was var_dump() and alert("It works"). Then I discovered the console…(thanks to Firebug and much later, Chrome DevTool) and I dropped alert in favor of console.log() And that was it... This was what "logging" was mainly about: A process of ascertaining the correctness of the software as we develop…

But as soon as I moved into building software systems on the JVM using Java, production logging became more upfront and also, the need for me, as a software developer, to proactively build logging into my application. It was not a complex switch, but looking back now, I realized that I unintentionally, for a while, approached writing production logging as if it were the ones that took place while development.

They are in no way the same...

First, there are more moving parts, second, the purpose differs: Including the ability for an application to write out log messages in production is to have insight into the internal state of the application and also, so as to provide a good source of forensics in case something goes wrong. It has even been estimated that any trivial system would have about 4% of its code base dedicated to logging. This shows how important production logging should be.

This post thus captures the new eyes with which I had to view production logging compared to the type I had long been familiar with; which is logging during development. It is written with logback in mind, which is an implementation of SLF4J, what has grown to be the standard abstraction when it comes to logging in Java

Almost all of what is mentioned would apply to any other SLF4J implementation. The difference that may exist would mostly likely be in the area of configurations...

...with that said, let’s start.


Saturday, November 15, 2014

Difference between BeanFactory and FactoryBean in Spring Framework

tl;dr A FactoryBean is an interface that you, as a developer, implements when writing factory classes and you want the object created by the factory to be managed as a bean by Spring, while a BeanFactory on the other hand, represents the Spring IoC container, it contains the managed beans and provides access to retrieving them. It is part of the core of the framework which implements the base functionality of an inversion of control container.

This post clarifies the difference between BeanFactory and FactoryBean in Spring Framework, and in the processes sheds some light on how the core component of Spring Frameworks stack together.

The Spring Framework has grown to be a lot of things, but at its core, it is a very versatile Dependency Injection container, upon which other things get built on. This core, this dependency injection container is actually built from 4 components.

1. The org.springframework.core package. In which, base functionalities that cut across the whole framework is implemented, e.g. exceptions, version detection etc.

2. The org.springframework.beans package. In which we have interfaces and classes for managing Java beans. The BeanFactory interface is found here.

3. The org.springframework.context Package. This builds on the beans package. An example of an interface out of this package that you would have used is the ApplicationContext (it implements the BeanFactory interface)

4. The org.springframework.expression package. This provides the expression language that can be used to traverse and manipulate the object graph within the container at runtime.


Saturday, November 01, 2014

How to Use Multiple SSH Keys

This post is a quick How-To.

It shows how to create multiple SSH keys for accessing multiple remote servers from a single local machine.

The post would use git hosting services: Github and GitLab (which allows for SSH connection to hosted repos), to illustrate the steps. Say for example, at work you use Gitlab but for personal projects you use Github, and you want to be able to use SSH to interact with these two services from the same local machine.

Although Git hosting services are used here, the steps outlined is in no way tied to these git hosting services, it works just the same for any SSH connections.

First Step: Generating the SSH Keys.

The git hosting service would provide you the instruction for generating an SSH public/private key and how to add the public key to your account. For examples see Github's instruction.

It basically involves running this command:
ssh-keygen -t rsa -C "Comments you want attached to the key"


Tuesday, October 07, 2014

Injecting and Binding Objects to Spring MVC Controllers

I have written two previous posts on how to inject custom, non Spring specific objects into the request handling methods of a controller in Spring MVC. One was using HandlerMethodArgumentResolver
the other with @ModelAttribute.

An adventurous reader of these posts would have discovered, that even if you remove the implemented HandlerMethodArgumentResolver or the @ModelAttribute annotation the custom objects would still be instantiated and provided to the controller method!

Yes, try it!

The only problem is that the properties of the instantiated object would be null, that is they won't have been initialized (this won't be the case though, if these properties are instantiated in the default constructor).


Monday, October 06, 2014

Using Spring's Type Converter to Inject Objects into Spring MVC Controllers

The pattern of having the identity of a resource articulated in the URL is not uncommon; In fact it is one of the ways of expressing RESTful end points...for example in an application that displays details about people, you can have a URL expressed thus:

http://www.example.com/people/777

where 777 is the number that serves as the identifier used to fetch the resources, in this case that could be the corresponding Person Object with an identifier of 777.

It wont be taking it too far then, if we see the number 777, as a direct mapping to the Person object with an id of 777.

In fact the controller method that maps the the URL above may simply have logic that get's the 777 part of the URL and use it to fetch the corresponding entry in the database backing the application. for example:


Injecting Objects into Spring MVC Controller Using @ModelAttribute Annotation

In How to Inject Objects Into Spring MVC Controller Using HandlerMethodArgumentResolver, I showed how to implement the HandlerMethodArgumentResolver interface in other to have a custom object passed as a method argument of a Spring MVC controller.

This post looks at how to still pass objects into the Spring MVC controller, but using a some what different approach: The @ModelAttribute annotation.

The @ModelAttribute annotation can be used to designates operation that can both retrieve and put stuff into the model. Its functionality depends on where it is placed...two places actually: On controller methods and on controller's method arguments.

When @ModelAttribute is placed on a controller's method, it allows the putting of stuff into the Model and when on a controller method arguments, it allows retrieving stuff from the Model...

I thus, like to think of @ModelAttribute as an annotation driven mechanism for writing and reading from the Model.

This post would look at these two methods of using @ModelAttribute and how it can be used to inject objects into controller's methods.


Monday, September 29, 2014

Overview of Exceptions In Java and Some SonarQube Recommendations

...So I had a chat with SonarQube earlier today. She was a little unhappy with the way things were in a project I was working on, and since I can't push this conversation away forever, I decided to bite the bullet and just hear her out. I figure that, apart from hearing what she has to say and making her happy, I could learn one or two things from her recommendations...

And sure I did. Especially when it came to Exception Handling. Got reminded about stuff I already know. Also picked up some one or two good things to always keep in mind when thinking/working with Exceptions in Java.

Since I have been able to get her Rules Compliance to 99.3%, I guess I could penned down some of the wisdom I gleaned from her: some new, some already known.


Saturday, September 20, 2014

Two Life Hacks: Dump Your Smart Phone. Eradicate Facebook's News Feed

I latch on to things; mentally: sometimes, I suspect it bothers on being obsessive. If a concept catches my fancy, I could latch on to it till I wear myself out. If I am confronted by a problem which I can't immediately solve, I find it really hard to get it out of my mind until I have it figured out.

This tendency is not necessarily a bad thing; at least it guarantees the tenacity and tunnel vision often required to crack through problems and grok non trivial concepts...

But...

it also has it's downsides: I realized that, yes, I could develop tunnel vision easily and focus on stuff, paradoxical as it may sounds, it also leads to a state where I find it hard to concentrate. It seems I get so consumed over a thing, the patience required for the state of mind needed to concentrate flies off the window.  And as everybody soon get to discover, tunnel vision isn't always the answer: sometimes you need to let go, take a step back and look at things from a different point of view; a thing that may not come easily when locked in a latching mode I am quick to slip into.

And then when you find yourself latching on to activities that benefits little, then the mental and emotional exhaustion comes real quick and swift.


Saturday, September 06, 2014

Remote Debugging Of Tomcat Via Intellij

A couple of moons back, I put fingers to keyboard and punched out a post on how to debug, from asunder, a running tomcat application, from within Intellij IDEA.

The post shall remained chronicled, for as long as it is possible, over on the company's blog.

For your perusal and to gain of the wisdom shared, hesitate not, and proceed to HOW TO REMOTELY DEBUG APPLICATION RUNNING ON TOMCAT FROM WITHIN INTELLIJ IDEA

Saturday, August 23, 2014

How to Inject Objects Into Spring MVC Controller Using HandlerMethodArgumentResolver


This post shows how to implement HandlerMethodArgumentResolver in other to resolve and inject objects into Spring MVC controllers, but before we get to the actual implementation procedure, a little trivial overview of Spring MVC would be good.

Spring MVC is designed around the Front Controller Pattern which it uses to implement the Model View Controller pattern.

Since you are reading this post, I can rightly assume you already use Spring MVC and you know how it generally works: You register the DispatcherServlet -Spring MVC's Front Controller, write your controller class and map requests to controller methods using @RequestMapping, then put the necessary configuration in place which would enable Spring to pick the written controller class and have it as a spring managed bean.

A trivial controller class may look like this:

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class HelloController {


    @RequestMapping("/index")
    public String doGreeting(Model model) {
        model.addAttribute("greet", "Hello World");
        return "helloView";
    }
}

So when a request hits /index, doGreeting(...) method is invoked with an object of Model (the M in MVC) passed into it, which is populated with the greet property which can then be accessed in whatever view technology in used. The view is located using the "helloView" string returned by the controller.


Tuesday, August 05, 2014

Configuring Maven to Fetch Artifacts from Multiple Repositories

It is quite common to have a situation where a project needs to source for artifacts from more than one source. This post quickly explain 3 different ways to achieve this.

Repositories settings in pom.xml


In pom.xml, you have the <repositories/> tag which can be used to specify the repositories a project should search and download artifacts from. For example:


Sunday, July 20, 2014

Common Date-Time Operations In Java - Formatting and Parsing Date-Time

In Date and Time With Java, a general overview of date/time related API was given. This post walks through formatting and Parsing Date-Time in Java.


Parsing and Formatting Date and Time.


What is the difference between December, 04, 1984 and 12/04/1984, and 04/Dec/1984 and ... or difference between 13:00hr and 1PM and...

Formating. They all represent the same date and time but are formatted differently.

Formatting date/time values as string, and parsing date/time values expressed in string into objects, are one of the common tasks when dealing with date/time. 

Formatting allows you to take a Java object that represents date or time and convert it into a string represention using a predefined formatting.

Conversely, with parsing, you take date or time values expressed as strings and have it converted it into valid Java date or time objects.

We quickly go over how to accomplish these tasks:


Saturday, July 19, 2014

Date And Time With Java

How do you deal with dates and time in Java and perform common operations like finding difference between two dates or translate dates into a particular string formats?

This post seeks to provide an answer to those questions.

I would start off by giving a brief description of the available date related classes and packages you might encounter on the Java platform and then do follow up posts exploring some of the common date/time operations like how to format date-time, dealing with time zones and locale, finding differences between dates/times etc.


Sunday, July 06, 2014

Hooking Into Container and Bean Life Cycle in Spring.

"What is the difference between BeanFactoryPostProcessor and BeanPostProcesser" This, said one of my colleagues, was part of his interview question, a question to which he promptly replied: "I don't know".

Once you pass the stage of actually getting Spring to work, passing the configuration huddle, in no time, the next couple of things you would probably run into would involve using or hooking into the various life-cycles in Spring and you start seeing things like BeanFactoryPostProcessor, BeanPostProcessor, DisposableBean, InitialzingBean, PostConstruct etc, which all seems to be callbacks mechanism that enables you plug in, somehow, into all the magic going on in Spring.

...And probably just like me, it all starts feeling like too much to grasp...Also, sooner or later, you run into the *Aware sets of interfaces too...

Dang! What are all these, How do they fit into the picture? I just want to write a web application, why do I have to grapple with all these what not?

I initially had all these concepts swirling all about without having any concrete mental model to attach them to or a good understanding of what actually is going on. I knew about the @PostConstruct annotation and maybe what it does, I also have come across various *Post Processors and maybe the IntializingBean interface a couple of times, but actually grokking and understanding where they fit did not happen until after spending some time working with other parts of Spring which allowed for a broader view of what is going, helping create some sort of mental model.


Monday, June 09, 2014

How to Autowire a Bean That Requires Constructor Argument In Spring

Spring allows you to inject a managed object (bean) as a dependency into another object via the @Autowired annotation.

For example, if I have a UserService that has a dependency on UserRepository, I can have the UserRepository injected using @Autowired annotation like this:

UserRepository Class...
class UserRepository {
UserRepository () {}
}


UserService Class...
class UserService {

@Autowired
private UserRepository userRepository;

UserService () {}

}

This is done using Field Injection. The same thing can be accomplied using Setter Injection:

class UserService {

private UserRepository userRepository;

UserService () {}

@Autowired // Using setter injection
public void setUserRepository(
UserRepository userRepository) {
this.userRepository = userRepository
}

}


or via Constructor Injection:

class UserService {

private UserRepository userRepository;

@Autowired // Using constructor Injection
UserService (UserRepository userRepository) {
this.userRepository = userRepository
}

}


There are different opinions on which method is "the right way" but since this post is not about that debate. I would point you to this post instead: Why I changed My Mind About Field Injection

Which ever method you choose to use, you are essentially doing the same thing: instructing Spring to supply an object's dependency by using the @Autowired annotation.

But what happens when the Object you want injected in has a constructor that requires an argument?


Making Aliases Permanent


Aliases provides you with short cuts to commands or group of commands.

..For example, instead of always having to type ls -la to get the long listing format of a directory's content, you can set up a short form, say ll and type that instead. You do this via aliases by typing in an open terminal this:

alias ll='ls -la'

It would set up an alias of ll that equates to 'ls -la'. As you see the format of setting up an alias is:

alias alias_name="command"

The only issue with this, is that the alias is lost once you end the terminal session. i.e. once you close the terminal. To make the aliase permanent you need to add it in a place where it gets loaded and available at the start of a terminal session.

The most appropiate place to put the aliases in other to make it permanent is in an hidden file called .bashrc in the user's home directory. Or better still, put it in a file usually named as .bash_alaises and have it refereced from .bashrc

This is straight forward.

with vim ~/.bashrc




and you add the aliases in the .bashrc file as shown above. The other, more prefered way is to create another file, usually named .bash_aliases and put your aliases inside instead.

The reason this works is due to the presence of a code section in .bashrc that loads the content of the .bash_alaises.

The portion of the .bashrc looks like this:


If you do not have this piece of code, feel free to add it.

All this has been easy, straight forward and clear enough. The confusing part to all this, especially if you are new to linux, is the seemingly plethora of other files you can put your aliases in and all mysteriously seem to get the job done.

If you do a google search you may find results that points to other files where you can define aliases. /etc/profile, ~/.profile, ~/.bash_login, .bash_profile

The reason these all seems to work is because they are all special kinds of files that have their contents sourced by the shell at different point in time. Some are sourced at login, some are sourced when the shell is invoked interactively.

To really understand how they all fit, lets go over some basic stuff here:


Friday, April 25, 2014

Configuring Aspect With Spring AOP

tl;dr Using AOP involves you identifying cross cutting concerns (Advice), extracting them out into a separate module and have them called at certain points in your code execution (Join Point) using certain expressions (Point-cut) to select these points where you want your cross cutting concern logic to run. Configuring thus involve telling Spring where your extracted logic is and the point-cut required to know where to apply them.

This post is part of the Configuring Spring Series.

Develop a sizable application and you would soon find yourself repeating some operations: certain logic; function calls that keeps recurring now and then. An example of such a recurring operation is logging. At different places, through out your application, you may find the need to have some information logged; either for debugging purposes or for auditing or for whatever reasons logging was invented. After a while you would soon find out that the log calls are sprinkled about the place in your code base. The drawback of having code scattered this way is even more if the code being repeated is not as trivial as log calls; Not good.

I have worked on a code base where this was the case. This is generally not advisable for various reasons: One, it fast become tedious having to repeat the same boring logic over and over again. Two, it leads to code entanglement as you have the flow of business logic being interrupted by mundane things that does not add to the business logic at hand. Third, it leads to code scattering. Same logic scatted all over the place. Even though the logic might be encapsulated into a method or function call, the method or function call would still be scattered all about the place. Not too good.

The explicitly of having to repeat these recurring calls might seem like a good thing, but with scale, it becomes not that much of a good thing.

Would it not be nice if it were possible to extract out, these recurring method calls, have it in some sort of separate module and then have it magically called when those operations that require them are reached during your code execution?

These kind of problems/situations is what Aspect Oriented Software Development seeks to solve/mitigate with Aspect Oriented Programming. It allows you to extract operations that cut across different aspect of your application and have them called when needed.

These kind of operations are usually called cross-cutting concerns, because, well they cut across various concerns in your application. :)

Apart from logging, operations that you would most likely run into, that can easily be classified as being cross-cutting concerns includes: Authorization, Error Handling, Performance Monitoring etc.

This post describes the process of configuring Spring framework in other to be able to use AOP in your application. As you might have guessed, the topic of Aspect Oriented Programming is a very broad one. And since these posts are mainly focused on configuration activities in Spring, I would not go into details of programming with Aspects; but instead explain basic concepts needed to get started and provide an overview of how AOP can be configured in Spring Application.

If you are familiar with event driven languages like Javascript (or any UI framework in most languages), you won't be totally wrong if you think the concept of Aspect sounds very familiar to what you do when you define functions as event handlers. And with event delegation, you can have this function registered to be executed when certain operations/event occurs within your application. The same reason why doing this makes sense also applies to why Aspects are recommended.

So let's get started with some of the basic concepts.


Saturday, April 19, 2014

A Tale of Bicycles, Complex and Stereotypes

"You Africans please listen to me as Africans...and you non Africans, listen to me with open mind"

So began Fela Anikulapo Kuti in Shuffering and Smiling before he lunched into a musical rendition that describes a reality only mostly Africans might be able to relate with...

And in that same spirit, I write this post...

Of late I have become more acutely aware of certain thought patterns the social system I live in has unconsciously etched in my mind. And it reeks of acceptance of stereotypes and of inferiority complex.

I would explain with a story involving bicycles...

You Nigerian? Do you remember when one transport minister proposed the idea that we should all start riding bicycles?

I remember and I remember my reaction to it. It was that of disgust, disdain and incredulity.

Why? my God, we are in the twenty something century! Keke? This guy wants to take us to the stone age! Why do these leaders like to make us look so backwards! Imagine! Just look to the "developed countries" (whatever that means)...they have moved past riding bicycles! This dude wants to take us back! Uncivilized, uneducated baga! So we would now be the backward African country these "developed people" would now start looking down on as the place where everybody rides bicycles: the primitive transportation machine? How do these people get to become leaders sef...?

State of mind? Disdain!

Then I went to Togo and spent about 3 months working there. And saw that a large amount of the populace goes about on bicycles and scooters...I viewed this with a mixture of amusement and disdain. But not only these, there was some smugness also. See them...backward African country...even for 9ja, we don pass this stage. How can you be riding "keke" all about the place? So Ara'ko-ish!

State of mind? Smugness and disdain!

Only for me to get to the Netherlands and discover there are more bicycles than people.

And people jolly well go about their daily life using bicycles. It is not a strange sight to see people all dressed up in their suit and tie with their briefcase, riding their bicycles to work. It is no strange sight to see mothers strap their babies to a seat that has being specially attached to the back (or sometimes front) of the bicycle.

Just Google Dutch people riding bicycles.

...And all of a sudden riding bicycles stopped being uncivilized or backwards! In fact it is something to recommend! Look, it is nice on nature, you don't want to continue harming our dear planet Earth with all these locomotives that emits dangerous chemicals? With bicycles, you don't get to emit any of such, so it helps us fight this global warming thing.

And also, it is soooo good for the heart! You know, cycling is good exercise and helps keep body and heart in good shape!

State of mind? Let's do it!

Gbam!

What changed? The people riding the bicycle!

It is quite shocking and sad to observe how my emotional reaction towards the idea of riding bicycles moved from utter disgust and disdain to one of admiration and commendation to the point of recommendation due to the type of people involved.

It is a different thing if my initial negative reaction to populace adopting the bicycle as a prominent mode of transportation was based on valid points. Perhaps something in those locality that makes bicycle riding ineffective? costly? But no, that was not the case.

When I shared this with a very good friend of mine, he made the following statements

...One May question the media through which we rationalize some of these things- and how societies have ebbed some systematic prejudice to how we view things. We are all guilty, to strive beyond it has to be a conscious decision that is by no means easy...

There are different angles to this observation and a quick look at history can help explain why things are currently the way they are. But it is quite jarring how the world we live in and its social structures affects the stereotypes we end up holding of the world around us and most importantly of ourselves. The harmful thing with these inclinations is that it could go unnoticed. An unconscious lens set up inside our minds, through which we now view the world.

It is also harmful when you consider the implication. When you have a situation where a societies' choice of solutions to their problems, when a societies identity etc are all unconsciously measured and framed against a mental yardstick of what we think some people from another society entirely would think of it, then we have an insidious problem at hand.

Where then is self determination. self identity, self realization? Where then is the ability to look inward, tackle your problems squarely and apply relevant and local solutions as needed?

It would be good for my people to stop, take a while and try to be introspective and observe some of our reactions to things around us; how we frame our problems, how we frame our identity, how we consider the solutions we think is cool (appropriate) and see if it does not suffer from this unconscious tainting...

As my friend said. "...to strive beyond this state, has to be a conscious decision..."

And with that, I end with the words of another conscious musician: Bob Marley...emancipate yourself from mental slavery, none but ourselves can free our minds.

Friday, April 18, 2014

Configuring Spring Data JPA

tl;dr Spring Data JPA enables a Repository style of accessing data where the data source is a JPA powered data source. Configuration is simple. Have a working Spring JPA configuration, then add the Spring-Data-JPA as dependency to your project and enable the functionality by wiring the appropriate bean in your Spring Context.

Configuring Spring Data JPA is part of the Configuring Spring Series.

This short post would show how to easily configure Spring Data JPA for use and some related concepts that should help explain the rational behind Spring Data.

Spring Data JPA itself is part of a larger umbrella project: Spring Data. Which seeks to provides an easier, less broiler plate way to accessing data. A lot of the projects under the Spring Data use the Repository pattern, while some don't. Spring Data JPA is one of the projects that implements the Repository Pattern.

Since the Repository Pattern is central to Spring Data JPA, it is recommended to, at least, have an overview of what is going on when it is being used. After this, we go ahead to look at the configuration procedure, which is very succinct.

Repositories can be viewed as abstraction over data. This explanation, although partly correct, does not do justice to the unique identity of the Repository pattern. The question might be, if it is an abstraction over data, how is it then different from DAO's?

There are quite a lot of varied opinion regarding the difference between Repositories and Data Access Objects; but I like this answer to the question on Stackoverflow a lot.

Most specifically the part where it says:

DAO is an abstraction of data persistence. Repository is an abstraction of a collection of objects.

DAO would be considered closer to the database...Repository would be considered closer to the Domain, dealing only in Aggregate Roots. A Repository could be implemented using DAO's, but you wouldn't do the opposite...


This explanation also touches on some key concepts out of Domain Driven Design: Domain and Aggregate Roots. Understanding these also would help groking the essence of Repository pattern itself. Here is a nice blog post that talks about Aggregates and Aggregate Roots in Domain Driven Design. It is a post in a series. The other posts should shed more light on other parts of DDD.

For the more adventurous regarding the subject of DDD; the bible itself is a recommended read. If its 506 pages look intimidating, then maybe a less voluminous guide would be more appropriate: InfoQ Free eBook : Domain Driven Design Quickly. It is just 104 pages.

In short, as can be seen from the explanation of the difference between DAO Pattern and Repository Pattern; the Repository is a collection.

The developer using the Repository is not concerned about the source of the objects. The source could be a Database, a Network, File system, or a Web service. It does not matter. DAO on the other hand, more often than not, deals essentially with the Database.

It is worth nothing here that DAO actually stands for Data Access Object and not Database Access Object. It seems the pattern evolved to mean Database Access Object. :|

So with Spring Data JPA, you work with Repositories which returns to you, objects or collection of objects that are sourced from a JPA based data store.

So with this fact established. The actual configuration is trivial.

Since it is Data JPA, configuring it requires that you have a working JPA configuration in place. You can read Configuring Hibernate JPA in Spring Framework if your JPA provider would be Hibernate. The configuration procedure applies if other JPA provider is being used.

To add Spring Data JPA, you need to do two things. Add it as a dependency in your project (pom.xml in this example as Maven is used) and enable it by wiring the necessary bean in your Spring context.

In pom.xml add:


            org.springframework.data
            spring-data-jpa
            1.4.3.RELEASE


and in your Spring Context add:



jpa:repositories is a Spring xml namespace that serves as a short cut for explicit bean definition needed to get Spring Data JPA running. You can read the section on Hiding Bean Definition via Namespaces for more about this.

The base-package tells Spring JPA Data where to find interfaces it should use in implementing Repository objects for your application. Yes, Spring JPA Data looks for interfaces you specify and provides the actual needed implementation for use at start up. Cool right? This is another way Spring Data JPA makes data access easier with less code. For a more detailed explanation on this, read Working With Spring Data Repositories.

More materials on Spring Data JPA in general can be found on the project page on Spring.io

Tuesday, April 15, 2014

Why The Hell Do I Need Types? Because Fahrenheit can't be less than −459.67

For quite a while I never got the need for a Type System in programming languages. Considering the fact that my first introduction to programming was via PHP and Javascript, and with little or no formal schooling in computer science itself, it is easy to see how I could have easily ignored Types altogether; or worse still consider them an unnecessary overhead.

Why do I need a Type system when all I want to do is to keep in memory the value of say the cost of an Ipad? Can't I just have:

$ipad_cost = 199;

And yeah, if someone reading the code wonders what currency this price is in? well isn't that what comments are for?

// cost is in dollars
$ipad_cost = 199;

Why Types? Why go define a class or Interface for something as basic as this? I may get the idea of Types, but...really?

This was largely my world view regarding Types (an unnecessary overhead and verbosity) until I encountered the situation in which I contemplated on the best way to hold the temperature of a region in Fahrenheit?

long temparetureInFahrenheit = 42L;

or

int temparatureInFahrenheit = 42;

...are temperature values not numerical values? so why not?

But with any of these examples, what is stopping my program from assigning the value −500.67 to the temparatureInFahrenheit variable?

Ha, then my code would be breaking a fundamental law of nature init? as the absolute zero in Fahrenheit is −459.67 which means you can not have temperature with numerical value less than −459.67.

See why then you can't model all numerical values as just an int, long or any of the primitive types that comes with almost all programming languages?

Would it not be nice then, to have a way to make my computer be aware of the fact that the variable temparatureInFahrenheit is actually of a type Fahrenheit and as such has certain rules it has to obey?

Yes, this is what Types are for and useful for.

They are not overheads but a great way to properly model your code and prevent erroneous slips like the one describe above, thereby reducing bugs.

Configuring Spring MVC

tl;dr The process of configuring Spring MVC involves configuring the DispatcherServler as a front controller in the web.xml and configuring the Spring context file where Spring MVC specific beans like Controllers, Handler mappings, View resolvers, Locale resolver and Theme resolver can be wired.

This post is about how to configure Spring MVC. It is part of the Configuring Spring Series.

Spring MVC allows you to put the Spring framework at the fore front of your web layer. As you probably know, it is possible to use Spring in a web application without Spring MVC. As mentioned in Configuring Beans In Spring Framework, you can use WebApplicationContextUtils to bring in the Spring context into your web application. In that scenario, Spring is not responsible for the whole Web layer, it just plays its part, mostly as a DI container. But with Spring MVC, Spring framework is put at a central and integral part of your web layer.

Dependencies


I would be using Maven for dependency management. To get Spring MVC, certain required artifacts needs to be added to your project. To get these dependencies with Maven, your pom.xml file may look thus:


Sunday, April 13, 2014

Configuring Hibernate JPA in Spring Framework

tl;dr To configure JPA in Spring via Hibernate, you wire up one of the classes provided by Spring that creates a bean of type EntityManagerFactory which then makes EntityManager available for you. There are two types of Entity Managers, and the class you wire up depends on which Entity Manager factory you would be using. We have Application Managed and Container Managed Entity Managers.

This post gives an overview of how to configure Hibernate as a JPA implantation, for use in a Spring application. It is part of the Configuring Spring Series.


In Configuring Hibernate in Spring Framework, we saw that in other to talk to the database via Hibernate, your application code would do so via Spring's implementation of Hibernate's main interface: org.hibernate.Session which is made available via an implementation of  org.hibernate.SessionFactory. And the process of configuration is just a process of wiring up Spring's to provide this needed SessionFactory as a bean.

You have a similar procedure when it comes to configuring Hibernate JPA in Spring. 

Instead of working with a SessionFactory, you work with EntityManagerFactory. And instead of Hibernates' Session you have EntityManager  through which the actual database access is done.

The process of configuring Hibernate JPA in Spring hence, is all about wiring the Spring bean that would make available for you the EntityManagerFactory.

Before we get to the business of actual configuration, let's get some key concept's explained.


Wednesday, April 09, 2014

Structure, Work flow, Design

They say it's good to have separation of concern, and I agree, but I would suggest that PHP's nature which easily allows the muddling of logic across layers of application creates a more fluid workflow (in the short term) that leads to more fluidity and better aesthetic looking creations; while with languages like Java; what you mostly get, (unless you put extra effort not to), are designs that feels more like they were constructed instead of inspired. With things looking all so structured and "boxy".

Friday, April 04, 2014

Is That A Reg Flag?

I am adopting a slightly different meaning to the phrase "red flags"; which in colloquial usage refers to a warning signal or something that demands attention. Although my modified meaning also can be seen as a warning signal; the new meaning I am ascribing to the phrase is based off the Red Flag Traffic Laws

The Red Flag Traffic Laws were laws in the United Kingdom and the United States enacted in the late 19th century, requiring drivers of early automobiles to take certain safety precautions, including waving a red flag in front of the vehicle as a warning.

The law as defined from Wikipedia. Emphasis mine.

An interesting part of the enactment:
  ...[a person] shall carry a red flag constantly displayed, and shall warn the riders and drivers of horses of the approach of such locomotives, and shall signal the driver thereof when it shall be necessary to stop, and shall assist horses, and carriages drawn by horses, passing the same.

To people of today's world; this policy definitely sounds absurd and backwards. But at the time it was put in place, it was done so as to address a real concern; safety to others in a world where self propelled locomotive is about to become more common...

But the absurdity of it is not my primary concern but the limitation the policy would have placed on the expression of a new technology. For one, having someone always in front of the locomotive means that the locomotive theoretically is not allowed to move faster than a particular speed. A big advantage of having a self propelled locomotive is then lost! Why have a car when you can only drive at a much slow pace?

And this is where my new internal usage of Red Flags takes it meaning. It expresses propositions, way of thoughts, policies that stifles the expression of innovation not with the intention of doing so but as a result of taking actions that intends to protect a valid concern.

And some characteristics of these kind of thinking; "these Red flags" can easily be spotted by taking a look at some of the characteristics of the Red Flag Traffic Law itself:

  1. The law was put in place in good faith. The intention was not to stifle expression of an emerging technology; but to protect the populace. But the outcome, invariably  limited the expression of a new technology
  2. The law was focusing on established way of life instead of embracing and exploiting a new way.
  3. Although the concern expressed by the law is valid; it tried addressing these concerns from the same paradigm level.

Making sure all self propelled locomotive have a hoot would have been a solution that sufficed. This is how addressing the concern from a different (or higher) paradigm would have largely addressed the concern without having to put an artificial limitation on the workings of the self propelled locomotive.


So when we are confronted with any new concepts; innovations or technologies that has the potential of disrupting the safety of well established way of doing things and we have a strong urge to put things in place that would address these concerns; we need to be aware that our actions might lead us to just be enacting our own versions of the Red Flag Traffic law. We don't want to do that.



In Software, You Build To Change Too

There are moments when differently considering how a domain is captured and expressed leads to an epiphany of sort: A much clearer insight into the matter at hand.

It could be a slight or simple change of point of consideration, but the insight it leads to, could be quite profound.

The Earth moved. A simple change in the way the solar system is viewed but a change that led to a significant upheaval of not just how we understand the cosmos; but a change that also rattled the political and power structures at that time.

I recently had a similar change with regards to how I internally conceptualize Software Development (or construction) and Software Architecture. Not surprisingly, my internal modelling has largely been shaped by the type of architecture that I am most exposed to: which is the civil type: how buildings are made, roads, bridges etc. And in these domains, the primary concern is building to last. The success of the architecture of a bridge is judged on how long it can last without fail. Same goes for high rise buildings. Building to change is not primary in these domain. 

This view has been projected onto how I primarily perceive Software Architecture. A great Software Architecture is the one that creates software that can always keep performing no matter what. This is the building to last metaphor.

But just as building to last applies to software; building to change is (should) also be core too. In fact; how well a software facilitate easy change would determine how long it may last.

Software is meant to mutate, by its very nature, with time. And a good software architecture is one that enables for easy change to the system while ensuring the system continues to provides its utility.

So when approaching a software creation process; keep in mind that building to change is as much important as building to last and let this fashion the architectural decisions you make.



Monday, March 31, 2014

Configuring Hibernate in Spring Framework.

tl;dr to configure Hibernate in Spring, you need to configure a bean that implements org.hibernate.SessionFactory. Configuring this bean involves specifying the data source, the Object-to-Database mapping (if you using xml for the mapping) and the hibernate properties.

Last post in the Configuring Spring series looked into how to configure data sources, a basic requirement, regardless of the actual method in which your application would communicate with the database server: either via plain JDBC or via an ORM tool.

This post would look into how to configure Spring to use Hibernate. Hibernate is an object-relational mapping library for the Java language, that provides a framework for mapping an object-oriented domain model to a traditional relational database.

In how to configure data sources in Spring, we saw how the data source is the entry point to having your application communicate with a database server; how it is configured and retrieved for use in your application by classes that are specifically created to handle interaction with the database (DAO classes)

In configuring hibernate to work with Spring Framework, the data source still remains the primary entry point, but it would not be what your application code (or DAO class if you go with this pattern) would have to use directly in other to communicate with the database. With Hibernate, your application code requires something called the SessionFactory

So the process of configuring Spring to work with Hibernate is basically then all about configuration the bean that would make the SessionFactory available for you.


Sunday, March 30, 2014

Configuring Data Sources in Spring Framework

This is the second post in the configuring Spring series. It kickstarts the database centric configurations required when building an application that persist data with Spring framework. This post talks about configuring data sources.

There are various ways to enable the ability to persist data in your application. From using any of the available Implementation of Java Database Connectivity (commonly referred to as JDBC) to going the ORM route via any of the popular ORM frameworks (Hibernate, MyBatis, Eclipse Link etc )

But with any of the above mentioned method, a data source is always a requirement; because...well you need to have a source to the data if you want to persist and/or retrieve data. And in Spring the data source ends up as a Bean that you would configure.

Spring provides several options of configuring the data source. You can have:
  1. Data sources that are provided via JNDI.
  2. Data sources that work with JDBC drive.
  3. Data sources that are from a connection pool.
But before we go ahead to look at configuration proper, it would be useful to quickly define some basic concepts you would encounter when dealing with persistency:



Sunday, March 23, 2014

The Difference between ServletContext and ServletConfig

The servletContext is used to provide configuration that would be available to all servlets in a web application, while servletConfig is used to provide configuration per servlet level.


Specifying and retrieving servletContext configurations.

To specify servletContext configuration, you do so in your web.xml via the <context-param/> tag. For example:

<context-param>
<param-name>timeout</param-name>
<param-value>60</param-value>
</context-param>

and to retrieve these parameters in any of the defined servlets you have:

String value = getServletContext().getInitParameter("timeout");

Specifying and retrieving servletConfig configurations.

Since servletConfig are available per servlet, the definition is within the servlet definition in the web.xml. This is done using the <init-param/> tag.

<servlet>
    <servlet-name>Polling Servlet</servlet-name>
    <servlet-class>com.foo.pollServlet</servlet-class>
    <init-param>  
        <param-name>timeout</param-name> 
        <param-value>60</param-value> 
    </init-param> 
</servlet>

With this, the specified parameters are only available in the com.foo.pollServlet servlet and can be retrieved by:

String value = getServletConfig().getInitParameter("timeout");

Left to me, I would say the not so intuitive choice of words, used to describe these two configuration type, is the cause of confusion most people usually have. I would rather have had the configurations per servlet level called servletConext while configurations that are on the web app level (that are available to all servlet) called appContext or webappContext.

Configuring Beans in Spring Framework.

This is the first post in the Configuring Spring Series.

Beans in Spring framework are proxies implementation of your classes. And following the Inversion of Control way of doing things, they are managed by the Spring framework container and made available for you to use.

Meaning you basically write your classes like you would normally do when programming in Java. After which, you then need to go through a process of configuration that would guide Spring regarding how your classes would be transformed into managed beans which you can then use to form your business logic.

There are different ways to have this configuration set up. And this post, first in the series of many, that would cover some of the configuration tasks in Spring, covers how to configure Spring to take your classes and have it turned into beans. This process is normally referred to as Bean wiring.

We would look at
  1. Configuration via XML
  2. Configuration via Java Classes: JavaConfig
  3. Configuration via @Component and @Autowired Annotation


Configuring Spring Series

Configuring stuff can be overwhelming when you first start off with Spring framework. You spend an awful amount of time wiring things up and configuring before you actually get to start writing business code to solve the problem you are faced with...

Want to start using SpringMVC? there is a configuration for setting this up. Spring Security? There is a configuration for that. Want to use AOP, then you need to know how to have this configured. Want to access a database? You have to first wire up the datasource, the adapter etc etc

This can feel just too much.

It would fast became obvious to anyone using Spring that in other to get productive, configuration is something that is needed to be sorted out and have nicely filed away in a easily retrievable mental model so as to avoid losing precious time fighting the configurations or falling into configuration mishaps that could be a drag on productivity.

The posts that would follow under a series I called configuring spring is my attempt to do just that. It would be series of posts that covers some of the basic configuration hoop I have needed to jump over before I could get to actual development. And since I have a terrible, horrible memory, these posts would be a place I can also easily run to, to look up how a particular configuration needs to be wired.

This post would serve as an index and would be updated as I add to the series.

Posts so far:

1. Configuring Beans in Spring Framework
2. Configuring Data Sources in Spring for Database Access
3. Configuring Hibernate in Spring
4. Configuring Hibernate JPA in Spring Framework
5. Configuring Spring MVC
6. Configuring Spring Data JPA
7. Configuring Aspects with Spring AOP

Friday, March 21, 2014

Getting Personal

In the beginning, there was this blog. And it was largely a place where I ranted. Not just about technology but other stuff that life brings my way, which not surprisingly, was mostly about technology…it also doubled up as some sort of journal.

But somehow somewhere down the line, it stopped being an avenue for expression: a place to capture my thoughts. Somehow somewhere, my writings morphed into mostly technical, tutorial like posts.

For instance, looking at all the post in 2013, they were all technical related, except the April 30, 2013 post which was supposed to be an attempt to add some personal touch by doing monthly recaps. That did not go very far.

Why was this? Pondering on it for a while, it hit me. It was because of the inroad of social networking sites into my everyday life: because I was spreading myself too thin all over Facebook and Twitter, sharing everything that was on my mind as status updates, sharing everything on my mind as comments to other peoples posts. Liking pictures, re-sharing links etc...

Twitter also, was the perfect place to let out burst of thoughts and emotions that should or could have coagulated into well formed posts...

But I am fed up. Fed up with all these social networking sites. Especially Facebook. I have long realised that it was having detrimental effects on my productivity and emotional well being…I am also fed up with the forced terseness of 140 characters that Twitter imposes. If I want to express myself, I want the liberty of being able to do this with as many words I deem fit.

And as such…I am going, going, going...back...back, to blogging blogging (in Biggie smalls voice)…not that I actually stopped blogging, but I am going back to having my blog as my primary avenue for capturing my thoughts.

I am avoiding Facebook by all means now. I still keep using twitter though, but majorly as a source of discovery for things around my area of professional interests, and once in a while, a place to engage in ephemerial and disjointed rants. If I have something on my mind I really feel the need to share or capture, I write a coherent post instead. No, sorry: fusilade of tweets is no longer for me.

It is time to get back to blogging and get personal here again.

Thursday, March 20, 2014

When CSS position: sticky doesn’t work.

I spent a huge chunk of time at work yesterday trying to figure out why the hell webkits implementation of position: sticky, position: -webkit-sticky wasn’t working.

Applying it on the tablet site I was working on had no effect but when I viewed a demo site: http://html5-demos.appspot.com/static/css/sticky.html it works perfectly; so it was not a case of my browser not supporting the feature, It was definitely something in the markup or css that was screwing things up.

After some couple of hours hunting about, I found the culprit:

#bodyconstraint {
overflow: hidden !important;
}

A parent element has got overflow:hidden, and this, apparently throws position: -webkit-sticky off.

So in case you run into a situation where position: -webkit-sticky isn’t working, an heads-up: check to see if the element you want to position as sticky is not a child of a parent element that has had its overflow set to hidden.

Also, you need to set the top property of the element you want to position sticky. if not, it doesn't work too.

Tuesday, March 18, 2014

Now I understand...Death, Life, Celebration...

I never really understood that part of my culture where we throw a party at the demise of an aged person. I always thought that no matter how old someone was, death is death and should be something of sobriety not something that deserves fanfare.

I never got it. Until now.

My grandpa just passed away. And for the past couple of days, I have being trying to grapple with the reality of it all; he is really gone? It feels surreal…

Then I understood why celebration after death makes sense.

All of a sudden I feel this strong need to throw a party and proclaim to the whole world, what great man my grand father was. A man of character. A man of valour. A family man. A principled man who has guided not just me but a lot of people that were blessed to be under his tutelage. I have this strong desire to have a musician…not just any but a distinguished one come rain praises to his name. While everybody celebrates and acknowledge truly the greatness of his life.

The second reason why the tradition of throwing a funeral party now makes sense is how I see it bringing closure. I have been in a reflective mood ever since and deep down inside I feel a need for some sense of closure. A sense of having said the final farewell. And now I can just appreciate how the fanfare and merriment of a party can bring such a closure. The final farewell done with the utmost gusty celebration that we can muster.

I can appreciate how it all makes sense now and how therapeutic it can be. Some listen to music, some write, some cry, some spend time with cherished memorabilia...all we all yearn for is a way to deal with loss.

sigh...

Death is part of life I guess. And it adds yet another twist to this unexplainable miracle that our reality is.

Sunday, March 16, 2014

To Birth or not to Birth

From fairest creatures we desire increase,
That thereby beauty's rose might never die,
...and there goes the opening of Shakespeare's first sonnet. Where he waxed lyrical about the importance of procreation and its role in preserving and perpetuating beauty in nature. In this sonnet and the couple of sonnets that followed, Shakespeare was addressing a dear friend who was beautiful to the eyes, but whom to reasons best known to him, was unenthusiastic about finding a woman, settling down and start having children.

In the same sonnet, Shakespeare went further to chide him on this unexplainable reluctance to settle down, marry and procreate:
But thou, contracted to thine own bright eyes,
Feed'st thy light's flame with self-substantial fuel,
Making a famine where abundance lies,
Thyself thy foe, to thy sweet self too cruel...
You should read the complete sonnet here.

This poem is one of my favourite and one of the first few ones I got committed to memory; maybe because I sort of empathise with this unknown friend of shakespeare regarding his reluctance to procreate.

I got an email yesterday from one of my closest pal back in high school. Actually literally, he was the closest as we shared the same desk. And he told me that his wife just gave birth...

See, I have gotten to that age where questions from my parent and siblings about when I would marry and start having kids have stopped being subtle and are now outrightly blatant. I have also gotten to that age where not just my female friends from high school are getting married and having kids but also my male friends are birthing their first children...

So when I got this message you would assume that I would take some moment to reflect on this news and ask myself what's up with me? I did take a moment to have this reflection and the interesting thing is that I could not be bothered to feel perturbed. Neither did I have a sense of needing to have a child...or start a family...

...Because I don't know what this all means! Yes, this life! I don't know what it all means and why! I am yet to come to terms of it all, something I think I need to do before I can feel comfortable bequeathing life onto another...

Life feels more like a big mystery...a very haunting one and I am not sure of the advantage of bringing yet another life to come live through it.

It is a big conundrum we are all forced to live in (and through) which we do not really understand. Where do we all came from, why are we here? to what purpose? and where do we go after here? And the uncertainty of what lays after here, if anything at all, after death is decapitating.

Why do we have to live to die?

This is all really scary thought. A painful state of being. if feels existence is one big scary captivity. One we don't understand the reason behind, one in which no matter how we scream and yell, we can't get out of. We can't get answers, we can only hang on faith and believe or not and come to terms to the uncertainty and maybe hopelessness of it all.

And yes we lose ourselves to the hustle bustle of life and most of us try to push this gloom to the back our minds, but unfortunately I cant. I really cant help slip into this state of utter uncertainty now and then, and feel helpless over what this all means. I really find it incredulous that we all get so caught up in living this life and yet no one knows...

Is this what I would bring another life to come and experience? this unexplainable abject sense of hopelessness? This huge conundrum?

Life is a bitch and then you die?

Some hold the philosophy that if life is a bitch, then bang the hell out of her before you die. All good and dandy but I just cant get it out of my head that you still die? what happens next? Really?

Which is why I am in no way excited at the prospect of bringing another life in here yet. (at least not until I come to reconcile our fate) right now I have not. Which pushes me to contemplate the conclusion that maybe the best gift I can ever give my children is not to give birth to them?

Spare them all these haunting.

Between the time I started writing this post and the time I got it finished my Grand Daddy passed on to glory. A man who has had tremendous positive influence on my life and the life of people around him. A man of conviction, of strength of character, a family man. A source of confidence and stability. His passing somewhat changed the emotion I started off this post with...Maybe just for the family, maybe just for the sense of security it gives, the companionship as we go through this life...maybe just for that we have no choice but to keep bringing forth life as it makes passing through it a lot more bearable?...

Sunday, January 26, 2014

Effect of Setting setTimeout to 0 and Explanation of JavaScript's Single Threaded, Non Blocking I/O, Async Event Driven Model

If you have two functions:

var f1 = function () {    
   setTimeout(function(){
      console.log("f1", "First function call...");
   }, 0);
};

var f2 = function () {
    console.log("f2", "Second call...");
};

and then you call them:

f1();
f2();

Checking your console, this is what you see:


If you were expecting to see "f1 First function call..." first, then you are not alone. This is what I initially expected based on my assumption that having a setTimeout set to 0 milliseconds, well mean, execute right away. Apparently this is not how things work.

This post is about how this unexpected behaviour led me into having a refresher of some of the core concepts regarding how JavaScript executes.