The Technical Musings’ Tweetlog: Microservices

This article is a compilation of tweets from @technicalmusing related to microservices. When I started learning about microservices I came across many new concepts and jargons, some of those I tweeted about are listed below. Most of these quotes are from the book Building Microservices by Sam Newman.

Sam Newman on how microservice architecture affects an organization’s structure:

 

Some of the common terms often use while discussing application security:

 

An interesting term for a known security vulnerability:

 

How we are changing the way we are measuring the productivity:

 

An application deployment strategy used for microservices:

 

A term devOps people should be aware of:

 

Caching is an important aspect to improve performance of a software. Here is a definition of cache miss:

 

Should you develop common libraries and share across your microservices!

 

The most popular way of handling transactions in a microservice based application:

 

Another suggestion from Sam Newman on shared code across microservices in case you are worried you code duplication:

 

Right, integration is the key:

 

An architectural solution to handle multiple UI clients:

 

Following three tweets are related to HATEOS, a recommended method to develop self discoverable REST APIs.

 

Interesting term:

 

Hope you liked the quick bites of concepts and jargons related to microservices. I will be updating this post with more similar stuff as I learn.

Please follow @technicalmusing on twitter for updates.

Enjoy Learning,

Kamlesh

Advertisements

Spring JDBC Prepared Statement & Char Datatype

I spent couple of hours figuring out this, hence thought to have a quick post.

I was trying to pass a string value as a parameter of prepared statement. I was using Spring JDBCTemplate for the same. This code was supposed to query a legacy database. When I was using the value in SQL directly it was working. But when I was using the prepared statement it was not. E.g.

// Following was working

String id = getJdbcTemplate().queryForObject("SELECT ID FROM SOME_TABLE WHERE TABLE_FIELD = 'abc'", String.class);

// Following was not working

String value = "abc";

String id = getJdbcTemplate().queryForObject("SELECT ID FROM SOME_TABLE WHERE TABLE_FIELD = ?", String.class, value);

The reason was that the TABLE_FIELD was of type CHAR(50) and hence the issue.

Since I did not have any control on changing the datatype itself. In order to fix the issue I changed the SQL to trim the char field as shown below:

String value = "abc";

String id = getJdbcTemplate().queryForObject("SELECT ID FROM SOME_TABLE WHERE RTRIM(TABLE_FIELD) = ?", String.class, value);

So, that’s one of the reasons why we should prefer using VARCHAR2 or equivalent datatype. Lesson learnt 🙂

Happy Learning,

Kamlesh

Spark-Shell Startup Errors

I have downloaded Spark binaries for Windows and tried running spark-shell batch file and ran into some issues. Following are the issues and the respective solutions:

Problem 1

ERROR Shell: Failed to locate the winutils binary in the hadoop binary path
java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
at org.apache.hadoop.util.Shell.getQualifiedBinPath(Shell.java:379)
at org.apache.hadoop.util.Shell.getWinUtilsPath(Shell.java:394)
at org.apache.hadoop.util.Shell.(Shell.java:387)
at org.apache.hadoop.hive.conf.HiveConf$ConfVars.findHadoopBinary(HiveConf.java:2327)
at org.apache.hadoop.hive.conf.HiveConf$ConfVars.(HiveConf.java:365)
at org.apache.hadoop.hive.conf.HiveConf.(HiveConf.java:105)

Solution

Download the winutils.exe from http://public-repo-1.hortonworks.com/hdp-win-alpha/winutils.exe and copy it to some directory say c:/hadoop/bin

Define the environmental variable HADOOP_HOME as C:/hadoop

Problem 2

ERROR SparkContext: Error initializing SparkContext.
java.lang.AssertionError: assertion failed: Expected hostname
at scala.Predef$.assert(Predef.scala:170)
at org.apache.spark.util.Utils$.checkHost(Utils.scala:931)
at org.apache.spark.util.RpcUtils$.makeDriverRef(RpcUtils.scala:31)

Solution

Set environmental variable SPARK_LOCAL_HOSTNAME as localhost

An excellent guide on Java Garbage Collection

Couple of days back I came across the Plumbr’s handbook for Java Garbage Collection. I went through it and found it very useful and easy to understand. Best think is that the book very nicely explains how to read and understand GC logs.

You can download this handbook here.

Enjoy Learning,

Kamlesh

What is Monkey Patching?

I came across this buzz word while I was reading something on unit testing. The concept is not new but the term is new for me. Monkey Patching is a term mostly used in context of Python and unit testing. This means modifying the code in a dynamic programming language at run time without affecting the source code.

Please refer the following wiki page for details about Monkey Patch.

Monkey Patch

Do share and comment about similar buzz words you know of in Software Development.

Enjoy Learning,

Kamlesh

Add/Update a Class File in a Jar

Updating a class file directly in a jar comes very handy when you are working on a remote server and you need to update some classes in your deployed jar file. With update feature of the jar command, we can directly update the selective classes in the jar rather than building and moving the jar file itself to the remote server.

Let us assume we have abc.jar and we need to update the file Foo.class in the jar. Foo.class is in the package com.tm.example.

In order to update the jar we need to create a directory structure reflecting the package structure of the class which we need to replace and place the class there. In this case we need to place the class, Foo.class, in the directory com/tm/example. Also, let us keep the directory com is on the same level as that of abc.jar.

Now we can execute the following command to update the class file into the jar file.

        jar uf abc.jar com/tm/example/Foo.class

The above command works for both Linux and Windows.

Please note if we do not provide the directory structure and try to add the class like

        jar uf abc.jar Foo.class

this will add the Foo.class in the jar on the root level.

In case we need to add/update multiple files in jar, we can do it by separating the file name with spaces like:

        jar uf abc.jar com/tm/example/Foo.class com/rm/example/another/Bar.class

For more details visit this java tutorial.

Enjoy Learning,
Kamlesh

AspectJ: Applying Advice to Methods Based on an Annotation

I am new to AspectJ (not new to AOP though) and have been recently doing some R&D on using AspectJ for intercepting methods annotated with an annotation.

What I Tried …
Use AspectJ to intercept all the public methods (belonging to classes of specified packages) based on an annotation applied.

What I Needed …
Aspect J Development Tool plugin for eclipse. [equipped my eclipse 3.7 with AJDT plugin]
Searched for AspectJ getting started tutorials available on the internet. [Visit this link  for a good AspectJ getting started tutorial]
Searched how to write a pointcut for intercepting a method based on annotation.

This is how I did it …

Created a simple annotation named MyAnnotation.

MyAnnotation.java

package com.technicalmusings.examples.aspectj.annotation;

public @interface MyAnnotation {}


Created three classes Hello1, Hello2 and Hello3 having public and private methods annotated with MyAnnotation. The intent was to apply a before advice to all the public methods (annotated with MyAnnotation) present in the package com.technicalmusings.examples and its sub-packages. Therefore these three classes have been kept in three different packages. Please refer to the class organization in the screenshot below:

AspectJProjectOrganization

Hello1 class has a method without annotation as well to ensure that the pointcuts are doing exactly what they are supposed to.

Hello1.java

package com.technicalmusings.examples.hello1;
import com.technicalmusings.examples.aspectj.annotation.MyAnnotation;

public class Hello1 {

    public static void main(String[] args) {
        methodOfHello1WithoutAnnotation();
        greetPubliclyFromHello1();
    }

    public static void methodOfHello1WithoutAnnotation() {
        System.out.println("Greetings from a method without annotation from Hello1 class!");
    }

    @MyAnnotation
    public static void greetPubliclyFromHello1() {
        System.out.println("Greetings from public method of Hello1 class!");
    }
}


Hello2.java

package com.technicalmusings.examples.aspectj.hello2;
import com.technicalmusings.examples.aspectj.annotation.MyAnnotation;

public class Hello2 {

    public static void main(String[] args) {
        greetPubliclyFromHello2();
        greetPrivatelyFromHello2();
    }

    @MyAnnotation
    public static void greetPubliclyFromHello2() {
        System.out.println("Greetings from public method of Hello2 class!");
    }

    @MyAnnotation
    private static void greetPrivatelyFromHello2() {
        System.out.println("Greetings from private method of Hello2 class!");
    }
}

Hello3.java

package com.technicalmusings.partofexamples;
import com.technicalmusings.examples.aspectj.annotation.MyAnnotation;

public class Hello3 {

    public static void main(String[] args) {
        greetPrivatelyFromHello3();
        greetPubliclyFromHello3();
    }

    @MyAnnotation
    public static void greetPubliclyFromHello3() {
        System.out.println("Greetings from public method of Hello3 class!");
    }

    @MyAnnotation
    private static void greetPrivatelyFromHello3() {
        System.out.println("Greetings from private method of Hello3 class!");
    }
}

Now the important part, the AspectJ stuff…

Created an aspect, HelloAspect, with one after advice and one before advice (just to differentiate between the two scenario)
a) The after advice is applied to all the methods annotated with MyAnnotation (irrespective of package)
b) The before advice is applied to all the mehods anotated with MyAnnotation belonging to classes of specified package or subpackages

HelloAspect.aj

package com.technicalmusings.examples;

public aspect HelloAspect {

    // Pointcut for all public methods
    pointcut publicPointcut(): execution(public * *(..));

    // Pointcut for all the methods with the specified annotation
    pointcut annotationPointcutAll()
        : execution(@com.technicalmusings.examples.aspectj.annotation.MyAnnotation * *(..));

    // Pointcut for all the methods with the specified annotation within specified package
    pointcut annotationPointcutPackage()
        : execution( @com.technicalmusings.examples.aspectj.annotation.MyAnnotation
         * com.technicalmusings.examples..*(..));

    after() : annotationPointcutAll() && publicPointcut() {
        System.out.println("annotationPointcutAll: Intercepted the after call of : " 
        + thisJoinPoint.getSignature());
    }

    before() : annotationPointcutPackage() && publicPointcut() {
        System.out.println("annotationPointcutPackage: Intercepted the before call of : "
        + thisJoinPoint.getSignature());
    }
}

By executing the classes Hello1, Hello2 and Hello3 individually, we can see how the advices are being applied. The after advice which does not have the package filter should include the method of Hello3 class whereas the other should not.

Below snapshot from ‘Cross References’ view of eclipse shows how the HelloAspect is advising the methods.

HelloAdvice

Enjoy Learning,
Kamlesh