Sunday, August 30, 2015

Countdown latches: concurrency

Countdown latches are concurrency construct that cause one or more thread to wait until a set of thread execution completes

Using Countdown Latches:

A main thread creates a countdown Latch, that we can use as a starting gate to start all thread simultaneously
Main thread decrement this latch to let all workers proceed.

-Main thread waits for another countdown latch initialised with number of worker thread 
-When all worker threads completes, it decrease the countdown and when it reaches zero the main /waiting thread proceeds and gather results

Concurrency : Thread Executor basics

package : Java.lang.concurrent.ExecutorService

Thread Pool naming convention :pool-<N_GLOBAL_GROUP_COUNT>-thread-<M_THREAD_COUNT_WITHIN_A_GROUP>,

ExecutorService executor = Executors.newSingleThreadExecutor;
executor = Executors.newSingleThreadExecutor;//single thread size
 executor  = Executors.newFixedThreadPool(5);// Pool size fixed


executor.shutdown(); //  Will not take a new request ,


 does not wait for previously submitted tasks to

     * complete execution

executor.shutdownNow(); //  returns list of tasks that never commenced execution/ ignored and were in waiting state, attempt to stop the running thread;


Callable<T> callable= executor.submit(<Runnable >);
 Future<?> future = executor.submit(callable);
 Future<?> future = future.get(5, TimeUnit.SECONDS);//Timeout exception execution_time>5

Java-7 features : quick sample


1. Multi Catch statement:Multiple exceptions type to be handled by the same catch clause:
sample:
Catch(ParseException pe | IOException ioe){
//log error
}
Catch(Exception e){

}

Thursday, August 27, 2015

Why dockerizing your application is essential?

Running our app on virtual box is a bit costly operation, as VMs are heavy and slow.This also requires effort in setting upend provisioning.

Whereas in case of Docker, It is lightweight, fast and very easy to configure/provision using a docker file where application specific instruction can be added.

Docker 

-Has a central repository of docker images, which developer can pull from different location.
-Can run many container running on a single machine.

commands:
Creating image :Create image : docker build -t <image_name>:1.0 .
Running app :    docker run -d -p 8080:8080 <image_name>:<v_1.0>
get automatic assigned ip :docker-machine ip default

#Setting env variable

ENV abc=hello

Dockerfile sample:

FROM <docker_repository>
#Install java8
FROM java:8
#Install java7 + maven
FROM jamesdbloom/docker-java7-maven

#Install only Maven
RUN apt-get update && \
    apt-get install -y maven

ADD your_app_root/trunk /opt/your_app
COPY your_app_root/settings.xml /usr/share/maven/conf/

#changing working directory
WORKDIR /opt/your_app/

#Exposing port to be available to the external machine.
EXPOSE 8080

#How to start the image
RUN ["mvn", "install"]
CMD ["mvn", "jetty:run"]

Sunday, August 23, 2015

Thread Basics - I

Thread: Why to use thread?
  • To run task parallel and concurrently
  • To utilise the hardware resource efficiently multi core/mullti cpu+ core  processor

Thread name :
-Is mutable
-Multiple thread can share same name.

Volatile: When to use volatile?

shared variable: one thread read/write + multiple thread read .
Will not work for : thread 1 read/write + thread2 read//write 

Synchronization:
thread 1 read/write + thread2 read/write 

Thread Priorities:
  • Each java threads open a new native thread at OS level. Setting priorities translates to native priorities based on the OS platform
  • Not recommended to rely solely on priorities
Thread Local Storage:
-Exist as long as the thread is alive

public static class PaymentResponseData
{
public int transactionId;
public String transactionStatus;

public int userId;
}

public static final ThreadLocal<PaymentResponseData> globalData =
new ThreadLocal<PaymentResponseData>();

globalData.set(tranacationData) 
 globalData.get().

 myThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

   public void uncaughtException(Thread t, Throwable e) {
   PaymentResponseData paymentResponseData=  globalData.get()

System.err.println("Error occurred with transaction ID " + paymentResponseData.get().transactionId);   }
   });

Demon thread:
Provide service to the user thread. Eg gc
Collecting statistics and performing the status monitoring tasks 

Saturday, August 15, 2015

Http scripting



 curl is an open source http scripting tool that can be used to make/ gets the data.  it sends data and it retrieves the information.

curl --trace-ascii d.txt --trace-time http://thetechiehouse.blogspot.com/

curl -o blog.html http://thetechiehouse.blogspot.com/

http authentication curl --user name:password http://www.google.com
post curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi


 curl --basic --user-agent “<user_agent>"  —<user_name>:<pwd>  "<url>"

curl -i 
 -H "Accept: application/json"
 -H "Content-Type: application/x-www-form-urlencoded"
  -H "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
   -X POST "http://www.mysite.com/form" -d"param1=val1"

Getting data from secured service:
curl -H "Authorization: Bearer <service_auth_token>" 'secured_service_Url'

Getting IP configuration
ipconfig//win
ifconfig// mac

Querying DNS to get IP address mapping
nslookup google.com

network statistics
netstat -n

TCP packet dump
tcpdump -l -x
tcpdump allows us to save the packets that are captured, so that we can use it for future analysis. The saved file can be viewed by the same tcpdump command.



nslookup google.com
dig google.com
curl google.com
curl -L -V google.com
curl-L google.com

curl for sftp:
curl  -k ---user user:pwd sftp://abc.com/path

ping vs curl
Ping only checks if the remote host is answering ICMP packets, which (usually) means it's up and running;
- This doesn't give you any information about which services the host is actually offering.

An HTTP GET request checks that there is a web server running on the host, that it answers to a given IP/port/hostname combo
 that you asked it for a valid URL and that the web site is able to answer your request.


If IIS (or Apache) is stopped on the host, it will very well answer a Ping request, but a HTTP GET will fail.

Friday, August 14, 2015

Spring expression language

#{<bean_name>.<getMethodName>('<argument>')}" />