Saturday, November 14, 2015

Using VisualVm to find a thread leak

A leak is when a application does not release references to a thread object properly. Due to this some Threads do not get garbage collected and the number of unused threads grow with time. Thread leak can often cause serious issues on a Java application since over a period of time too many threads will be created but not released and may cause applications to respond slow or hang.

In this article I am going to show how to perform a routine inspection of an application to find a thread leak. The first thing we will need is some sort of tool that could help us monitor the virtual machine of the running application.  There is a very widely used tool, called VisualVm. It comes with the JDK but also can be downloaded separately in any OS.

Java visual vm is a profiling tool that comes with the jdk and can be used to monitor your running applications. But in order to be able to connect to your app using visualVm, you will have to include a few parameters to your start script.

-jar myApp.jar

So if the app is configured and running, all you have to do is use visualVm to remotely connect to the correct host and port.

Once you connect to your application via visualVm, you can browse the diferent tabs and look for relevant information related to the VM and the threads. It will be very useful to also install a plugin for visual vm, called thread inspector which will allow you to examine in depth the running threads. You will be able to find the plugin in Tools>Plugins>Search for Thread Inspector

When there is a thread leak, often we will see an unusual large amounts one same type of thread visualVm's threads tab. If we double click in any of those threads, the plugin will show us more details about the class creating that thread.

Go to your source codes and place a break point in the class constructor.

Now run your debugger in one of your acceptance tests and observe. The frames tab will highlight a bit brighter the methods in your source codes that are creating the thread.

Hopefully the debugger will help you pinpoint the cause of the issue so you can fix it.
In the given example, the cause of this leak was that a Jersey DI configured to create some instances of a pool per request and this was consuming the resources. The solution is either to make that injection Singleton or not do it at all if it is not needed(which hapen to be the case).

Finally, you could run visualVm again in your local running app to verify that there is no leakage before release. Running the acceptance test is probably not enough because you want to see if the  leakage is there, so best is to run the app in a local or test environment.

For further reading and more detailed usage instructions see:

Sunday, November 1, 2015

installing jenkins in ubuntu server 14.04

What better way of spending a beautiful Sunday than to trying to discover how to configure Jenkins in Ubuntu server 14.4 for 3 hours(Can you feel the sarcasm?...). Anyway this post is just to avoid similar frustrations in the future, this brief summary should do the job, just copy paste all the commands and you will have jenkins working in your server     

Jenkins installation steps

 wget -q -O - | sudo apt-key add -  
 sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'  
 sudo apt-get update  
 sudo apt-get install jenkins  

Running Jenkins
After installing instalation is complete.
There will be a hidden directory under your /home/username called ./jenkins
This directory contains all the jobs and other configurations.

When we want to run jenkins we need to run as our username(should not run as root because it would be taking the configs from /var/lib/jenkins/jobs), from the directory:
using the command:
nohup java -jar jenkins.war --httpPort=5001 &

You can less nohup.out to see the log of the nohup command

Tuesday, September 29, 2015

Discovering transitive dependencies in maven

Sometimes we discover a dependency in the project that we didn't put there. The reason for this is often a transitive dependency(a dependency of our dependency).
Keeping an eye on transitive dependencies and tidying up the pom files time after time is a good practice.
A way of discovering transitive dependencies is to use the command "mvn dependency:analyse".

This command comes from a set of tools inside the maven-dependency-plugin.
In order to use it, you have to include the dependency plugin in your project like this:

 <!- If you are just using it in a single module project->  

  <!-- If you are using a multi module project then use this in the parent POM -->  

This plugin can do multiple things, the analyze command is very useful for finding more information about dependencies.

Unused declared dependencies found: The dependencies are not used in the project
Used undeclared dependencies found: Transitive dependencies used in the project(not declared in the pom)

The output of the command will be presented as warning in the terminal, if you want to be very strict about keeping your your pom tidy,
you could add the analyze command in the build lifecycle and configure maven to fail the build if any warnings are found(Maybe I explain that in another
post sometime ;) ).

Another great tool that comes in the dependency plugin and that its worth mentioning, is
 "mvn dependency:tree".
It will help you visualise the dependencies tree of your pom and understand it better.

Just some more interesting stuff about maven if you want to read more:

Thursday, September 24, 2015

First class functions Vs Higher order functions

First class functions means the ability to treat a function as a value, assigning it to a variable, or passing it as an argument to another function.

 //example 1 assignment  
   Function<String,String> postfixFunction = s -> s + "-postfix";  
 //example 2 passing as parameter  
   public void someMethod() {  
     stringManipulator(postfixFunction, asList("A","B"));  

A higher order function is a function that accepts other functions as arguments, or returns another function.

  //example 1 accepting function as arguments  
   public List<String> stringManipulator(Function<String, String> manipulator, List<String> data) {  
   //example 2 returning a function  
   public Function<String, String> postfixFunction() {  
     return s -> s + "-postfix";  

Friday, September 18, 2015

Don't be afraid of chinese restaurants ;)

How many times in your life have you shout to a non english speaking Chinese waitress at a restaurant in Soho, something like: "HEY!!! Why are you swapping the card 4 times?!?!?!!"
In this post I will prove that Chinese waitress are honest and often people panic, because they don't understand what Idempotency is.

The debit cards payment gateways are the classic example of idempotency.

In distributed systems terminology, we say that a service is Idempotent, when its clients can repeatedly send the same requests while the service produces the same result.
In other words, multiple identical requests are treaded by the service as a single request.

Important to note that this doesn't mean that the response is identical, it is just the service result that will be identical.
While the state of the response may change between requests, the result will always be the same, for identical requests.
In other words, it is save to call the service multiple times.

Let's have a look at a simple example of an idempotent function in java:

Receipt process(DebitCard debitCard, Amount amount) { throws NoPatienceException 
      if(not(transactionSentFor(debitCard,amount))) {  
           return cardProcessor.process(debitCard, amount);  
      else {           
           return blankReceiptFor(debitCard, amount);  

In the example above, we can clearly see that the conditional statement is safeguarding us from starting probably a distributed transaction across multiple parts of a distributed system. Instead of that, if we try to process again,  a blank/identical receipt with the returned

Come on, stop being affraid of chinese restaurants. 

Sunday, July 12, 2015

Why big corporations don't like retrospectives?

Not too long ago, I used to facilitate retrospectives for some of the teams in the department.
But for some strange reason their interest in retrospectives just disappeared.
I thought a lot about it, but I couldn't come up with an answer. Why?

Is it that they don't understand the advantages of retrospectives?
Is it that they have no time for retrospectives?
Or is it that they feel powerless when they do a retrospective?

In this brief text I want to just share my opinion on why I think corporations, have no interest in retrospectives(A bit of an speculative opinion, but just an opinion).

A retrospective is well known Agile practice that aims to improve any aspect of work. During a retrospective a team debates about things that matter for them and for the company: ideas, solutions, process, collaboration, communication, quality, recruitment, salaries, etc...
This are just some topics.

Unfortunately retrospectives are not being taken serious in many companies:
- in some, retrospectives are discouraged and seen as a waste of time
- in others shyly implemented within a constraint environment without visibility or power to
take any action. Just as a way of ventilating frustrations. 

So why is this that they are so unpopular?

The Answer is simple:
Companies fear change but specially, they fear decentralisation and lose of control.

The fact is that well organised regular retrospectives, could easily challenge the stablish organisational structure by making evident and visible its inefficiencies.

Many young technical companies quickly gain terrain to the prehistoric corporates not just because their decentralized and autonomous nature but also because of their eagerness and necessity to communicate and improve. Most of the successful ones often practice retrospectives.

The old school of building software companies has its days counted. What employees have to say about process inefficiencies, technical challenges, etc ... Is as important, if not more than the absolutely false illusion of rush that the corporation sees in the market.

Big corporations are organisations with huge amounts of resources and great potential for making significant impact in industry. Helping them getting rid of their fears is key for building not just good companies but a
healthy industry.

Software is continuous change, so embrace change, embrace retrospectives, embrace the future

Thursday, July 9, 2015

Maven build in colours

Did you ever wonder, how you can make your maven build display colours?
In this quick post I will show you what you need to do to show colours in your maven build.

  1. Navigate to wherever you have installed maven and go into the lib folder ~/apache-maven-3.3.1/lib
  2. Delete slf4j-simple-1.7.5.jar
  3. Find on the internet the following jars and add them to the lib folder

  4. Get out of the lib folder and navigate into the conf folder once in there create a file called log4j2.xml and add the following content:

    <?xml version="1.0" encoding="UTF-8" ?>
        <Property name="maven.logging.root.level">INFO</Property>
        <Console name="console" target="SYSTEM_OUT">
          <PatternLayout pattern="%highlight{[%p] %msg%n%throwable}" />
        <Root level="${sys:maven.logging.root.level}">
          <Appender-ref ref="console"/>
  5. Go to the terminal and try building a maven project

Share with your friends