Pages

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->  
     <plugins>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-dependency-plugin</artifactId>  
         <version>2.10</version>  
       </plugin>  
     </plugins>  

  <!-- If you are using a multi module project then use this in the parent POM -->  
   <pluginManagement>  
    <plugins>  
     <plugin>  
      <groupId>org.apache.maven.plugins</groupId>  
      <artifactId>maven-dependency-plugin</artifactId>  
      <version>2.10</version>  
     </plugin>  
    </plugins>  
   </pluginManagement>  

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:
http://blog.florian-hopf.de/2014/01/analyze-your-maven-project-dependencies.html
http://www.kyleblaney.com/maven-best-practices/
https://maven.apache.org/plugins/maven-dependency-plugin/

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) {  
     return data.stream().map(manipulator).collect(toList());  
   }  
   //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. 


Share with your friends