Remote config in MicroServices

We are in a world of microservices, an application typically uses one or more infrastructure and 3rd party services. Examples of infrastructure services include: multiple environments, cloud configurations, a message broker and a database server.Examples of 3rd party services include: payment processing, email and messaging, etc.Although every microservice instance is an independent unit, it is recommended an approach to externalized their configurations management from one central service/location.

The concept of using configuration server inside microservices architecture is visualized on my design below.It is based on basic Spring boot implementation, further, different layers and orchestration can be added to enhance it.

Feel free to contact me if you want to discuss this in detail.

RemoteConfig

 

How to achieve parallel code execution without user Threads

What comes to your mind when you have to run a piece of code in parallel? you will create some threads to do it, correct! But this is not only a way to achieve it. Recently I came across one interesting requirement where I had to run a piece of code of an API for three parallel calls (without Threads).My approach is not something new, many of you might be doing it already and others might have not noticed this approach while maintaining the legacy code 🙂 .

Well, so this approach is nothing but a combination of Bean loading in Spring container and corresponding mapped Java class execution, and hooking a API to support parallelism. when spring container loads a annotated/configured Bean it executes Java method associated with it. using this concept we can define and load the same bean with three different ids to execute the method ( the same code runnable implementation ) code rather than doing it through Three different threads.


<beans profile="cluster">
<bean id="mongoQueryServList" class="com.skilledminds.thirdPartyService.configure.db.connectionProvider.cacheQueryProcessor"/>
</beans>
<beans profile="cluster">
<bean id="cassandraQueryServList" class="com.skilledminds.thirdPartyService.configure.db.connectionProvider.cacheQueryProcessor"/>
</beans>
<beans profile="no_cluster">
<bean id="graphQueryServList" class="com.skilledminds.thirdPartyService.configure.db.connectionProvider.cacheQueryProcessor"/>
</beans>

Above piece of code is just a replacement of Three threads for  "cacheQueryProcessor" task, now to achieve parallelism/conditional task execution this method should be designed properly to meet the requirement. For more details on parallelism/conditional task execution please refer Spring-Batch Scaling and Parallel Processing or Java executor framework ( which is a base of every Parallelism implementation).

Still you want to take a deep dive of similar implementation then take a look at ongoing Spring Jira https://jira.spring.io/browse/SPR-8767

PS: For an experienced java professional this may look very simple to do but considering this in a bigger picture gives the insight of an option to design an utility tool/Jar without heavy Spring batch or similar APIs.

who will dance with Nancy ? Find in below code !

My new post is going to take little more time so thought of to post solution of this long pending problem posted by me on November 1, 2014

public class DecideDancePair {

DecideDancePair ddp1;
StringBuffer ddp2 = new StringBuffer();

public static void main(String[] args) {

new Thread(new Runnable() {

@Override
public void run() {
synchronized (DecideDancePair.class) {
System.out.println("Its weekend .... Hey Guys !! this is Nancy , who will dance with me? ");
}

}
}).start();

new Thread(new Runnable() {

@Override
public void run() {

synchronized (new DecideDancePair().ddp1) {
System.out.println("Hey Nancy !! this is Peter , I will dance with you.");
}

}
}).start();

new Thread(new Runnable() {

@Override
public void run() {
synchronized (new DecideDancePair().ddp2) {
System.out.println("Hey Nancy !! this is Rozer , I will dance with you.");
}

}
}).start();

}

}

==============================

Soln :

Nancy will always go for dance because she having Lock over a object which is not NULL. same with Rozer , he is also having a non-NULL lock. while poor Peter is caught with NULL object Lock so that piece of code will throw below exception and Hence Nancy and Rozer will be in dance 🙂 !!!

Code execution will looks like :


Its weekend .... Hey Guys !! this is Nancy , who will dance with me?
Exception in thread "Thread-1" Hey Nancy !! this is Rozer , I will dance with you.
java.lang.NullPointerException
	at BasicConcepts.DecideDancePair$2.run(DecideDancePair.java:26)
	at java.lang.Thread.run(Thread.java:745)

Effective use of IdentityHashMap and Flyweight Pattern

Imagine a online finance dashboard where a Finance consultant and his Client discuss, do some calculation , presentation, template and query filling like activities, and finally ends session of discussion. Such dashboard can have various tools and repetitive actions and forms which can be used through discussion. Lets take a case of 1000 customers at any time on dashboard, 100 tasks and forms , again 40 conditions for each task and form. So at least
1000*100*40= 4000000 Objects ( or near to it if consider some static design ) and corresponding GC() cycle we have to support to meet our requirement.

By using proper design and data structure we can reduce this problem and can speed up Dashboard loading and tool performance etc… use of Flyweight pattern and IdentityHashMap is perfect combination for use-case like above.

The Flyweight pattern is suitable for a context free and frequently used large object creations.
IdentityHashMap – This class implements the Map interface with a hash table, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)

Here is one sample code that you can refactor for your own similar use-case.


import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map.Entry;

public class HashedIdentityDashBoard {

	public static void main(String[] args) {
		HashMap<String, DashBoard> hm = new HashMap<String, DashBoard>();
		IdentityHashMap<DashBoard, String> idenHM = new IdentityHashMap<DashBoard, String>();

		hm.put("PendingTasks", new PendingTasks());
		hm.put("DailyTasks", new DailyTasks());
		hm.put("EscalatedTasks", new EscalatedTasks());
		// and 100 more repetitive tasks

		// identity map for repetitive tasks and their Templates and Query Forms
		idenHM.put(hm.get("PendingTasks"), "40 Templates and Query form of PendingTasks");
		idenHM.put(hm.get("DailyTasks"), "40 Templates and Query form of DailyTasks");
		idenHM.put(hm.get("EscalatedTasks"), "40 Templates and Query form of EscalatedTasks");

		// only for 1000 users we need 1000*100*40 = 4000000 Objects

		for (Entry<DashBoard, String> entry : idenHM.entrySet()) {
			DashBoard dashBoard = entry.getKey();
			System.out.println("Key : " + dashBoard + " : Value : " + idenHM.get(dashBoard));
		}

	}

}

interface DashBoard {

	public void setTitle(String widgetTitle);

}

abstract class AbstractUserDashBoard implements DashBoard {
	public String dashBoardName;

	abstract public void setTitle(String widgetTitle);

	@Override
	public String toString() {
		return "Title of DashBoard is :" + this.dashBoardName + " and hashCode is " + this.hashCode();
	}
}

class DailyTasks extends AbstractUserDashBoard {

	public DailyTasks() {
		setTitle("DailyTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}
}

class PendingTasks extends AbstractUserDashBoard {
	public PendingTasks() {
		setTitle("PendingTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}

}

class EscalatedTasks extends AbstractUserDashBoard {
	public EscalatedTasks() {
		setTitle("EscalatedTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}

}

Shared queue processing with Pub and Sub Threads

import java.util.Random;

public class PubSubDemo {

	public static void main(String[] args) {
		SharedQ sharedQ = new SharedQ();

		new Thread(new Consumer(sharedQ), "Consumer").start();
		// Delay publishing
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		new Thread(new Producer(sharedQ), "Producer").start();
	}

}

class Producer implements Runnable {
	SharedQ sharedQPro = null;

	public Producer(SharedQ sharedQ) {
		this.sharedQPro = sharedQ;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			produce();
		}
	}

	public void produce() {
		synchronized (sharedQPro.sharedQueue) {

			for (int i = 0; i < 3; i++) {
				sharedQPro.sharedQueue[i] = (new Random().nextInt(20));
				System.out.println(" ### Produced ### " + sharedQPro.sharedQueue[i]);
			}
			sharedQPro.sharedQueueIsEmpty = false;
			sharedQPro.sharedQueue.notify();
			try {
				sharedQPro.sharedQueue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

class Consumer implements Runnable {
	SharedQ sharedQCon = null;

	public Consumer(SharedQ sharedQ) {
		this.sharedQCon = sharedQ;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			consume();
		}
	}

	public void consume() {
		synchronized (sharedQCon.sharedQueue) {
			if (sharedQCon.sharedQueueIsEmpty) {
				try {
					sharedQCon.sharedQueue.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			System.out.println();

			for (int i = 0; i < 3; i++) {
				System.out.println(" *** Consumed *** " + sharedQCon.sharedQueue[i]);
				sharedQCon.sharedQueue[i] = 0;
			}
			sharedQCon.sharedQueue.notify();
			System.out.println(" --------------------------");
			try {
				sharedQCon.sharedQueue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}

class SharedQ {

	int[] sharedQueue = new int[3];
	boolean sharedQueueIsEmpty = true;

}

/*
Output of pub sub :

 ### Produced ### 1
 ### Produced ### 0
 ### Produced ### 3

 *** Consumed *** 1
 *** Consumed *** 0
 *** Consumed *** 3
 --------------------------
 ### Produced ### 12
 ### Produced ### 0
 ### Produced ### 14

 *** Consumed *** 12
 *** Consumed *** 0
 *** Consumed *** 14
 --------------------------
 ### Produced ### 13
 ### Produced ### 8
 ### Produced ### 17

 *** Consumed *** 13
 *** Consumed *** 8
 *** Consumed *** 17
 --------------------------
*/

Sequence generator tasks by Threads

Many times we face scenario where we need to execute two sequential tasks to perform a operation, like Tagging / Stamping serial Coupons , Producing serial Tickets at different Vending Machines. Below code ( generating Odd and Even in sequence) can be used for such requirement.

PS: This is a basic implementation, the same can be done using other Threading concepts.

				
package multithreading;

// print ODD and Even number alternatively

public class SquenceGeneratorTasks {

	public static boolean oddPrinted = false;

	public static void main(String[] args) {
		 Object lock =new Object();
		Thread oddThread = new Thread(new Runnable() {

			@Override
			public void run() {
				int i = 1;
				while (i <= 20) {

					synchronized (lock) {
						if (!oddPrinted) { // make sure printing starting with Odd number and each Odd is followed with a Even number
							System.out.println(i);
							i = i + 2;
							oddPrinted = true;
							lock.notify();
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}

				}
			}
		});

		
		Thread evenThread = new Thread(new Runnable() {

			@Override
			public void run() {
				
				System.out.println(" Though you have delayed Odd thread but printing gonna start with Odd number only !!");
				
				int i = 2;
				while (i <= 20) {
					synchronized (lock) {
						if (oddPrinted) {
							System.out.println(i);
							i = i + 2;
							oddPrinted = false;
							lock.notify();
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		});

		evenThread.start();
		
		// even if We apply some delay to Odd thread printing will start with ODD number only and 
		// Sequence of Odd-Even-Odd maintained
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		oddThread.start();

	}

}						
/*
Though you have delayed Odd thread but printing gonna start with Odd number only !!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 */

You can download the same code from : https://github.com/SkilledMinds/easyNuts/tree/master/code-25-09-2015

Go Gradle ! Servlets with Gradle

Gradle is build automation tool which is collection of all good things of ANT and Maven.For more reading on it follow https://gradle.org/

Today I am posting one sample Gradle Servlet project. Gradle easily supports Java web applications through the “war” and “jetty” plugins. My steps will show you how to quickly make a simple Java Servlet web application ( I am using here STS IDE).

Steps :
(1) Add Gradle plugin (easiest way to do it, goto STS dashborad, search and
add gradle plugin).you need to install Gradle and provide the path of its bin in PATH in case you want to run project from command prompt.
(2) Create a project folder “GradleServlet”
(3) create a file “build.gradle”
(4) content of file “build.gradle”

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'

repositories {
  mavenCentral()
}

dependencies {
  providedCompile 'javax.servlet:servlet-api:2.5'
  runtime 'javax.servlet:jstl:1.1.2'
}

(5) lets create the servlet at GradleServlet\src\main\java\com\HelloWorldServlet.java

package com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet
{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        resp.getOutputStream().write("Gradle is build automation tool which is collection of all good things of  ANT and Maven.".getBytes());
    }
}

(6) Finally let’s put web.xml at GradleServlet\src\main\webapp\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <servlet>
 <display-name>HelloWorldServlet</display-name>
 <servlet-name>HelloWorldServlet</servlet-name>
 <servlet-class>com.HelloWorldServlet</servlet-class>
 </servlet>

 <servlet-mapping>
 <servlet-name>HelloWorldServlet</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

(6) our Project is ready now, lets build and run it. Lets use commands

to build use —> gradle build
to crate .war of Application use —> gradle war
to run it by Jetty ( a light weight container ) use —-> gradle jettyrunwar
you should see similar lines on screen :

C:\sts_workspace\GradleServlet>gradle jettyrunwar
:compileJava
:processResources UP-TO-DATE
:classes
:war
> Building 80% > :jettyRunWar > Running at http://localhost:8080/GradleServlet

You can get the source code from https://github.com/SkilledMinds/easyNuts