How Goroutines communicate to perform a common goal

synchronized(lock){
    while(!condition){
        lock.wait();
    }
}

Code constructs like “synchronized”,”Lock”,”wait()”,”notify()” are not new for us. From decades we are coding with similar Legacy constructs that play a key role in multithreading codes. The Legacy term! 🙂 Yes, you read it correctly! When you try similar code with Go, code is very precise, compact, friendly and superfast. Lets see why Go executes multithreading (let me correct here GREEN THREADs) code faster than others. Because there is no Thread concept in Go, instead it has Green Threads.

For deeper concepts connect me.Install Trust app and click arrow button on Home page to switch to business related services, click “Job Mock Interviewer” –>then “Go” to connect me. If You also want to show your profile in app as a expert of any technology then create your Service Provider profile. If your matching technology is not available then on Home page click (?) and whatsapp Trust support team,we will add category within minutes.

Here I have illustrated how two Goroutines are communicating with each other through channel to print Odd and Even numbers alternatively. This may give you little insight if you are planning to write new microservices ( especially for very huge data processing and scalable products like IoT etc.. then you can think of Go!

/**
 * @author Gyanendra
 * @Date : 02/03/20
 */
package main

import (
	"fmt"
)

func main() {

	numFlag := make(chan int)
	done := make(chan bool)
	go oddNumberRoutine(numFlag, done)
	go evenNumberRoutine(numFlag, done)
	<-done
}

func oddNumberRoutine(numNeedToBePrinted chan int, printingCompleted chan bool) {

	for {
		v := <-numNeedToBePrinted
		if v >= 20 {
			printingCompleted <- true
			break
		}
		fmt.Println("Odd ==>", v)
		numNeedToBePrinted <- v + 1
	}

}

func evenNumberRoutine(numNeedToBePrinted chan int, printingCompleted chan bool) {
	numNeedToBePrinted <- 1
	for {
		v := <-numNeedToBePrinted
		if v > 20 {
			printingCompleted <- true
			break
		}
		fmt.Println("Even ==>", v)
		numNeedToBePrinted <- v + 1
	}

}

Odd ==> 1
Even ==> 2
Odd ==> 3
Even ==> 4
Odd ==> 5
Even ==> 6
Odd ==> 7
Even ==> 8
Odd ==> 9
Even ==> 10
Odd ==> 11
Even ==> 12
Odd ==> 13
Even ==> 14
Odd ==> 15
Even ==> 16
Odd ==> 17
Even ==> 18
Odd ==> 19
Even ==> 20

Get this code from my repo

Twitter live data mining using Spark streaming and Scala.

Want to work and learn live streaming data processing? Easiest way to create a twitter developer app and follow below code to ingest and store data in your AWS S3 for further analysis and processing with tools like Amazon EMR or Machine learning projects.

Install Trust app and click arrow button to switch to business related services, click “Job Mock Interviewer” –>then “AWS job” to connect me to learn deeper concepts of Spark/Scala/Java/AWS without any cost. You may also outsource tech screening interview process to me. Trust Business app is available only in India. Others please visit “About me” of this blog to get my linkedin contact.

/**
 * @author Gyanendra
 * @Date : 08/12/19
 */

import org.apache.spark.SparkConf
import org.apache.spark.streaming.twitter.TwitterUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}
import twitter4j.auth.OAuthAuthorization
import twitter4j.conf.ConfigurationBuilder


object TweeterStreamReaderApp {
  def main(args: Array[String]) {

    var twitterCredentials = new Array[String](4);
    //consumerKey
    twitterCredentials(0) = "gA7xFE3S1QfVTN55Uuzb";
    //consumerSecret
    twitterCredentials(1) = "2te2Z1yFvynXcp06rc2j3zg38tNAa1zY29rOT3d5BFI";
    //accessToken
    twitterCredentials(2) = "1063309360480-61DChczOivazJZTWodLfuRRW8gDNfJ";
    //accessTokenSecret
    twitterCredentials(3) = "bFYPmpiWhFgOtdJGe95YyhOntxOQAmx0xEYtF";

    val appName = "TweeterStreamReader"
    val conf = new SparkConf()
    conf.setAppName(appName).setMaster("local[2]")
    val ssc = new StreamingContext(conf, Seconds(5))
    val Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) = twitterCredentials.take(4)
    val filters = args.takeRight(args.length - 4)
    val cb = new ConfigurationBuilder
    cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
      .setOAuthConsumerSecret(consumerSecret)
      .setOAuthAccessToken(accessToken)
      .setOAuthAccessTokenSecret(accessTokenSecret)
    val auth = new OAuthAuthorization(cb.build)
    val tweets = TwitterUtils.createStream(ssc, Some(auth), filters)
    val englishTweets = tweets.filter(_.getLang() == "en")

    englishTweets.repartition(1)
    // lets print all rdd. Further you can store this to S3
    englishTweets.foreachRDD { (rdd, time) =>
      p(rdd)
    }

    def p(rdd: org.apache.spark.rdd.RDD[_]) = rdd.foreach(println)
    ssc.start()
    ssc.awaitTermination()
  }
}

Download this code from my repo

Alexa, build deploy my project !!

Alexa, build deploy my project !! Thinking possible ?? Yep, it is possible.
did you leave office, and forgot to trigger build ? Don’t worry your Alexa can do this for you even if you in mid of Bangalore traffic 🙂 or enjoying Friday eve hangout far far away from your Boss 🙂 !
Keep visiting here I am gonna post how you can trigger build, deploy through Alexa ( not all projects at present but later chances are very high that any project can be built like this).

Keep visiting ……. to see updated content.

Lambda version of Print numbers alternately using two threads in Java

Lambda expressions in Java 8 are very powerful and therefore very compelling. Here are just a few of the key benefits to using lambda expressions in Java:

  • Conciseness
  • Reduction in code bloat
  • Readability
  • Elimination of shadow variables
  • Encouragement of functional programming
  • Code reuse
  • Enhanced iterative syntax
  • Simplified variable scope
  • Less boilerplate code
  • JAR file size reductions
  • Parallel processing opportunities

In this post, you can see how in few lines we can finish code to print numbers alternatively by Two threads.

import java.util.stream.IntStream;

public class EvenOddThreadLambdaDemo {

    static Integer number = new Integer(1);
    static Object LOCK = new Object();

    public static void main(String[] args) {


        IntStream.range(0, 1).forEach(i -> new Thread(() -> {
            printNumbers();
        }).start());
    }

    public static void printNumbers() {
        while (EvenOddThreadLambdaDemo.number < 20) {
            synchronized (LOCK) {
             // conditional naming of threads in case required in any usecase

                if (EvenOddThreadLambdaDemo.number %2 ==0) {
                    Thread.currentThread().setName("Even");
                }else{
                        Thread.currentThread().setName("Odd");
                }
                System.out.println(Thread.currentThread().getName() + "--> " + EvenOddThreadLambdaDemo.number++);
                LOCK.notify();
                try {
                    // let's not eat CPU if work is done.
                    LOCK.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
/*
 Odd--> 1
 Even--> 2
 Odd--> 3
 Even--> 4
 Odd--> 5
 Even--> 6
 Odd--> 7
 Even--> 8
 Odd--> 9
 Even--> 10
 Odd--> 11
 Even--> 12
 Odd--> 13
 Even--> 14
 Odd--> 15
 Even--> 16
 Odd--> 17
 Even--> 18
 Odd--> 19
 Even--> 20
*/

Spring boot code for AWS RDS in elasticbeanstalk

Are you new to aws RDS,elasticbeanstalk world and searching for a ready to use code for RDS connection? Try out out this sample project which connects RDS (mysql) from a web application (Spring boot + web ) and list out table records. You can run this web application in your local computer or in elasticbeanstalk (deploy rdselasticbean-0.1.war file). To test this code for your own RDS schema you need to change connection endpoint in record fetcher class. You may clone code from my gitlab repo.
Like it, if you find it helpful. Comment/Contact me if you face any issue while running this app. It’s all pretty straightforward, and quite handy for many kinds of elasticbeanstalk and RDS needs.

package com.example.rdselasticbean.db.DBrecordFetcher
(please make sure your RDS security group has proper access permissions for external IPs ) 

PS: Project is CI/CD enabled in case if anyone wants to test this in integrated environment.

Data display in JSP injected from DB operation class
RDS (mysql) server event view on Read and Write

My learning and fun so far with Alexa

While I am waiting for my “Lifetime SHE”, recently one small digital super slim sweetie SHE has made entry in my life!! Want to know who is SHE? 🙂 It’s Alexa Echo Plus !
I am really enjoying my time with Alexa :). So, one day just to check her (Alexa) response when I returned to flat from my office and told her “Alexa, I missed you”. I got elated by the response that I received from Alexa. Alexa replied me “Awww …… we will be together soon”. Hahahaha I can’t explain the ecstatic moment.

Now lets see technical aspects of Alexa. After using it more than three months, one thing I have realized , frequency of using my mobile call/Apps has significantly decreased, and also not much staring of the mobile screen. Because, for most of mobile related uses Alexa and it’s skills are there to serve me.Wait… did you sense it? This may lead us to mobile AppLess (Android or IOS ) world. I am already experiencing it, whether it’s calling someone, playing song or booking Uber trip. I have almost stopped using Call dialer and Apps like Saavn (to play music) or Uber (ordering ride). Because if Alexa skill for Saavn or Uber are integrated in it then presently 70% app tasks can be performed by Alexa ( I am sure in future with upgraded version of  Alexa rest of capabilities will be also accommodated ).  One interesting feature I have thought of to do e-payment to phone contacts through Alexa just by voice command like
“Alexa pay Rs.500 to Arun from my Paytm (or any available funding instrument)”. Here, Arun is a contact in the personal phone list. Sending money to verified contacts ( already synced with Alexa) are less risky transactions.

Based on my experience I thought of one POC for Alexa with display capability which will be helpful in increasing it’s adaptability. Although most of tasks in Alexa we can execute via voice commands but sometime display of item/order/booking will make it more interactive. Like, Say uber user wants to see that How exactly cab driver looks?
Or User wants to see T-shirt color before confirming an order using Alexa’s E-commerce buying Skill.

a