Saturday, June 10, 2017

Spring Boot : Hands On Introduction


 Developing Web Application using SpringMVC and JPA (Non Spring Boot Way )
  1. Configure Maven Dependencies in pom.xml
  2. Configure Service/DAO layer beans using Java Config
    • Can configure DataSource, DataSourceInitializer, JPA Repo, Property Source etc
  3. Configure Spring MVC web layer beans
    •  Configure Thyleaf , ViewResolver, ResouceHandler, MessageSource etc
  4. Register AppConfig.class and WebMvcConfig.class  and with AbstractAnnotationConfigDispatcherServletInitializer
  5. Create a JPA Entity and Spring Data JPA Repository
  6. Create a SpringMVC Controller
  7. Create a thymeleaf view /WEB-INF/views/index.html to render list of BaseEntities

Developing Web Application using SpringBoot
  1. Create a Maven based SpringBoot Project
  2. Configure datasource/JPA properties in application.properties
  3. Straight away jump to creating Entity , EntityRepo & Controller
  4. Create Thyleaf view
  5. Create Spring Boot entry point.
 What Are The Advantages ?
Spring Boot lets the developer focus on the application’s development first, and removes the need to be overly concerned with every other aspect of its lifecycle, including deployment and management 
It is easy to create stand-alone, production-grade Spring based Applications that you can ‘just run’”. It supports “convention over configuration”.
  1. Easy Dependency Management
    • Pulls in dependency like springboot-starter-web dependency by default it will pull all the commonly used libraries while developing Spring MVC applications such as spring-webmvc, jackson-json, validation-api and tomcat. 
    • Similarly can see Spring-boot-starter-data-jpa dependency
  2. Auto Configuration
  3. Embedded Server
    • spring-boot-starter-web which pull the spring-boot-starter-tomcat automatically
Apart from the above following are noteable features
  1. Actuators
  2. Dev Tools


Bill Of Material : BOM
Spring  Boot calls its 'intelligent collection of dependencies' as BOM
Spring Developers have taken the time to match all the correct versions that work well with each other.
For Example : if you  are using 'spring-core 4.2.3' then it works well with 'logback-core 1.1.3'. If you keep including dependencies manually then you may have to spend a fair amount of time. Thanks to Spring boot (Maven) all of this is gone.

Note in traditional Spring boot app you use
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.2.4.RELEASE</version>
  </dependency>

but in Spring boot you use
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

Reason being... Spring boot starter parent at start or pom takes care of it
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.1.RELEASE</version>
 </parent>




You can change it as you want and the effective pom will change accordingly.





Friday, June 9, 2017

AWS Lambda

AWS Lambda is one of the most popular serverless computational architecture in use.
It is Event Driven.

It supports the following Function Runtime
  1. Python 2.7
  2. Node.js v0.10.36 v4.3.2
  3. Java 8
  4. C# Runtime.
There are certain addons that you can use for example C++.

 File Size Limitations
  1. Uncompressed code and dependencies must be < 250MB
  2. Compressed function < 50MB
  3. Total File Size in all of the Region < 75GB
How to come around these limitions
  1. Search for smaller 3rd party libraries 
  2. Can consider micro services architecture vs monolith
  3. Other runtime solutions like python vs java vs Javascript
 Resource Limitations
  1. Empheral File Storage < 512MB
  2. Maximum Execution Limitations of 300 seconds ( it includes wait time for DB query or an API return call )
  3. Limit of 100 Concurrent Lambda function
How to overcome these Limitations ?
  1. Suppose you have to write a file processing functionality using Lambda, consider downloading it in S3 (in 500MB chunks) and processing in lambda.
  2. Break as needed and chain lambdas
  3. If you want to call more than 100 Lambdas , try contacting AWS and see if they can increase the limit in your account.
 Others
  1. Memory 128 MB - 1.5 GB
  2. CPU doesn't have a configuration within lambda function , but it should scale proportionately with memory allocation to your function.... so if you double the memory in your function it should double the CPU.
  3.  Can configure function to access resources only within VPC , runs inside a VPC by default it doesn't have access to resources within your private VPC
Miscellaneous
  • They are given certain default configurations , based on role you provide



Saturday, June 3, 2017

Java Iterable Vs Iterator

In my experience, the most important consideration is whether the item in question should be able to be traversed more than once. This is because you can always rewind an Iterable by calling iterator() again, but there is no way to rewind an Iterator.

Iterator is single traversal.
 
In Java 5

  1. Iterable<SomeObj> objs = new Iterable>() {
  2. public Iterator<SomeObj> iterator() {
  3. return object.getChildren();
  4. }
  5. };


In Java 8, working with legacy Iterator-based interfaces is a little easier, thanks to method references. The Service.getPorts example above could be written as:
  1. Iterable<SomeObj> objs = object::getChildren;
And you can even inline it directly into the for loop:

  1. for (SomeObj objs : object::getChildren) {
  2. // ...
  3. }
A Collection is an Iterable... So you can write:

public static void main(String args[]) {
    List<String> list = new ArrayList<String>();
    list.add("a string");

    Iterable<String> iterable = list;
    for (String s : iterable) {
        System.out.println(s);
    }
}