Showing posts with label DONE. Show all posts
Showing posts with label DONE. Show all posts

Wednesday, 20 September 2017

Notes on Singleton Pattern and Immutable Objects

Where exactly the Singleton Pattern is used in real application?

  • ·       Logger
  • ·       DB Connection

 A Strategy for Defining Immutable Objects

  1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
  2. Make all fields final and private.
  3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
  4. If the instance fields include references to mutable objects, don't allow those objects to be changed:
    • Don't provide methods that modify the mutable objects.
    • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

Saturday, 9 September 2017

Lambdas

Lambda basics:-

whay lambdas?
-> helps functional programming

Advantages:-

  • concise code
  • parallel processing

Drawback of oops:
All code blocks should be associated with class and objects.

Lambda Expression examples

(int a) -> a * 2; // Calculate the double of a
a -> a * 2; // or simply without type
(a, b) -> a + b; // Sum of 2 parameters
If the lambda is more than one expression we can use { } and return

(x, y) -> {
int sum = x + y;
int avg = sum / 2;
return avg;
}
A lambda expression cannot stand alone in Java, it need to be associated to a functional interface.

interface MyMath {
    int getDoubleOf(int a);
}

MyMath d = a -> a * 2; // associated to the interface
d.getDoubleOf(4); // is 8

HATEOAS

HATEOAS:- Hypertext As The Engine Of Application State
.
The way of sending the response with links which directs the next possible operations.
{
"name": "Vikram",
"id": 1
"link" [
{
"href":"http://localhost:8080/myApp/1",
"rel":"self"
}
]
}

Richardson maturity Modal:-


Friday, 8 September 2017

Spring Framework Complete

understand the complete flow and add spring security,cloud,log,database change and add an article in dzone. sept 8-10

worked on Spring application pet Clinic accepted as the Reference project for all spring features since 2007.
Github link:- PETCLINIC GITHUB
in Spring site:- link

Its Developed by spring developers and opensource contributors.
Spring-Petclinic is spread accross different repos in github each with different approach to solve the same common requirement.

List of features it explains:-
Spring MVC
  With XMl
  With Annotations+xml
  With JavaConfig
  With JSP UI
  With Thymeleaf UI


Spring Rest - HATEOAS

  With JDBC
  With jpa - EntityManager
  Spring Jpa

For both H2 Database and Mysql.

Spring Rest + Angular JS

Spring boot

Spring Microservices
  Eureka for node register and discovery


Docker 

Thursday, 7 September 2017

Revision Day 7: Multi-threading- synchronized : Class level vs object level

Class level vs object level

Class Level:-

private void lock() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
synchronized (Foo.class) {
System.out.println("in Class  " + Thread.currentThread().getName());
Thread.sleep(4000);
System.out.println("in Class " + Thread.currentThread().getName() + " end");
}

}
main(){
Foo b1 = new Foo();
Thread t1 = new Thread(b1);
Foo b2 = new Foo();
Thread t3 = new Thread(b2);
t1.setName("t1");
t3.setName("t3");
t1.start();
t3.start();
}

Output:-
t1
t3
in Class  t1
in Class t1 end2
in Class  t3
in Class t3 end2

in case of Class level blocking,only one thread can run the class-synchronized method at a time,Also to note an instance synchronized method can also run simultaneously

As seen above eventhough t1 and t3 entered the block at the same time,only after completion of t1, t3 can start accessing the method.

Object Level:-

public void Lock() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
synchronized (this) {
System.out.println("in obj " + Thread.currentThread().getName());
Thread.sleep(4000);
System.out.println("in obj " + Thread.currentThread().getName() + " end");
}
}

main(){
Foo b1 = new Foo();
Thread t1 = new Thread(b1);
Foo b2 = new Foo();
Thread t3 = new Thread(b2);
t1.setName("t1");
t3.setName("t3");
t1.start();
t3.start();
}

Output:-
t1
t3
in obj t1
in obj t3
in obj t3 end
in obj t1 end

In case of instance synchronized method,both t1 and t3 can start accessing the method at the same time because both are acting on 2 different objects.

Tuesday, 5 September 2017

Notes on Threads and Multi-threading Application

MultiThread Project takes the directory as input,and gives the PDF and TXT files with their size,name and path.

Github path:- MultiThread_Application

Notes:-

Concurrency 
· BlockingQueue
- interface - extremely useful in producer-consumer problems
o ArrayBlockingQueue,- implementation of BlockingQueue interface
o PriorityBlockingQueue - Same as BlockingQueue but its contents should be comparable either
 naturl ordering or by                 customizing
o DelayQueue - Same as BlockingQueue but its contents can be accessed only after certain time say 5 sec.
o SynchronousQueue - A special type of BlockingQueue with the size of zero.you can insert into it only 
when the consumer is waiting
o LinkedBlockingQueue - linkedlist approach of ArrayBlockingQueue -- implementation of BlockingQueue
 interface
· BlockingDeQueue - Same as BlockingQueue but with Dequque functions
o LinkedBlockingDeque - Same as BlockingQueue but with linkedlist + Dequque functions
· Java Thread Pools
o Executor - Its an interface with execute function.
o Executors Class - its like a factory class that supports all ExecutorService inteface functions.
o ExecutorService - Interface extends Executor class, Executor framework.
o ScheduledExecutorService - creates threads upon specified time.
· ConcurrentMap
o ConcurrentHashMap - HashMap with all methods synchronized
· ConcurrentNavigableMap
o ConcurrentSkipListMap
· ConcurrentSkipListSet
· ConcurrentLinkedQueue
· CopyOnWriteArrays - thread-safe variant of ArrayList - The collection internally copies its contents over 
to a new array upon any modification, so readers accessing the contents of the array incur no synchronization 
costs
· Callable and Future - callable is alternative to Runnable where object has to be returned,and Future is
 going to hold the returned object.
· Condition Class - Condition factors out the Object monitor methods (wait, notify and notifyAll) into distinct 
objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary
 Lock implementations.
· CyclicBarrier - allows multiple threads to wait for each other (barrier) before proceeding.
· CountDownLatch - just a counter need to decrease and call await method till then all threads after the 
current thread are held.
· ReentrantLock Class - alternate to using Synchronized block.
eg:-
private Lock lock = new ReentrantLock();
lock.lock();
try {
increment();
} finally {
lock.unlock();
}

· ReentrantReadWriteLock - ReentrantLock with additional readlock and writeLock functionalities.
· Semaphore - counter is greater than zero, then access is allowed. If it is zero, then access is denied.
use sem.acquire();,sem.release(); to get the permit and release
· Exchanger - an CLass which helps to exchange the classes between threads.

· java.util.concurrent.atomic - increment the integer in synchronized way.

 ExecutorService service = Executors.newFixedThreadPool(4);//total 4 threads availabe in the pool
        new Thread(new FileOperations()).start();        for (int i = 0; i < 2; i++) {
            service.submit(new PDFreader());//2 threads given to PDFReader
        }
        for (int i = 0; i < 2; i++) {
            service.submit(new TxtReader());//2 threads given to TxtReader

        }

Revision Day 5: Spring BOOT


Revision Day 5: Gradle and CSS

CSS Selectors:-


The element Selector

{
    text-align: center;
    color: red;
}

The id Selector

#para1 {
    text-align: center;
    color: red;
}

The class Selector

.center {
    text-align: center;
    color: red;
}

Grouping selectors:-
h1, h2, p {
    text-align: center;
    color: red;
}

GRADLE:-


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'gs-accessing-data-rest'
    version = '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("com.h2database:h2")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Revision Day 4: Spring Java Config

completed the spring Java config example with lot of difficulty.
used plain hibernate initially later changed to EntityManager and was successfully able to run the code.

Thursday, 31 August 2017

Wednesday, 30 August 2017

Revision Day 2: SPRING MVC

Completed the simple mvc application.
Changed it from spring jdbctemplate to hibernate for Dao implementation.


Web.xml:-

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

       <display-name>Archetype Created Web Application</display-name>

       <welcome-file-list>
             <welcome-file>home.jsp</welcome-file>
       </welcome-file-list>

       <servlet>
             <servlet-name>spring-mvc</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
             <servlet-name>spring-mvc</servlet-name>
             <url-pattern>/</url-pattern>
       </servlet-mapping>

<!-- <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
       </context-param>

       <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener> -->
</web-app>

Wasted lot of time in understanding how this “spring-mvc-servlet.xml” getting loaded i.e. how spring is getting initiated when I am not specifying which file to fetch nor the file path??

After so much of investigation I came to know that
<servlet-name>spring-mvc</servlet-name>
             <url-pattern>/</url-pattern>

Here server will take servlet-name as the standard file name and appends -servlet at the end like this:-
Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-mvc-servlet.xml

When you want to put your Servlet file in your custom location or with custom name, rather than the default naming convention [servletname]-servlet.xml and path under Web-INF/ ,then you can use ContextLoaderListener.

I ran simple java main class to test the application functionality by placing the spring-mvc-servlet.xml in resources folder.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import jbr.springmvc.model.Login;
import jbr.springmvc.model.User;
import jbr.springmvc.service.UserService;

public class App {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-mvc-servlet.xml");

    UserService obj = (UserService) context.getBean("userService");
    Login login = new Login();
    login.setUsername("admin");
    login.setPassword("admin123");
    User user = obj.validateUser(login);
    System.out.println(user.toString());
  }
}

<build>
             <finalName>SpringMvcUser</finalName>
             <plugins>
                    <plugin>
                           <groupId>org.mortbay.jetty</groupId>
                           <artifactId>jetty-maven-plugin</artifactId>
                           <version>8.1.16.v20140903</version>
                           <configuration>
                                 <scanIntervalSeconds>10</scanIntervalSeconds>
                           </configuration>
                    </plugin>
             </plugins>
       </build>

Use mvn jetty:run

SPRING:-

Spring framework provides flexibility to configure beans in multiple ways such as XML, Annotations, and JavaConfig.



What is dependency injection and its types?

A technique where the instance is not directly passed but its specified how to create the instance, this helps in loose coupling and testing.

Types:-
  1.              Setter injection
  2.              Constructor injection
We prefer setter injection over constructor injection because it helps in resolving cyclic dependency.

Scenario:- If A is dependent on B and vice-versa, then we can neither create instance of A nor B because A requires instance of B passed in the constructor and B requires instance of A be passed in the constructor, This can be resolved by setter injection where the instance A is created first then we can set the B’s instance.

Tuesday, 29 August 2017

Revision Day 1


  • Revised all the algorithms That i worked on in the past couple of months while working on Princeton's algorithms course.
  • Read about Hoare’s Partition Scheme and Lomuto partition scheme on quicksort,Dijkistra's algorithm etc.
  • I am not going to spend more time on this again.
  • updated my algorithms summary article.so that i can just go through it for quick reference.

Installing Docker and Minikube

  install docker-   sudo apt install docker.io   set user to docker group:- sudo gpasswd -a   {user} docker   imp commands:- ...