Spring Batch - Process Multiple Files Parallel

Photo Credit Pixabay

Today, We will discuss how we can Process Multiple Files Concurrently using Spring Batch.

Prerequisite Basic knowledge of Spring & Spring Batch Framework is Required.

Background
Spring Batch is a lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems. Spring Batch builds upon the characteristics of the Spring Framework that people have come to expect (productivity, POJO-based development approach, and general ease of use), while making it easy for developers to access and leverage more advance enterprise services when necessary.

Scaling and Parallel Processing Spring Batch Offers Multiple options for Scaling and Parallel Processing. At very high level these are separated in below categories.

  1. Multi-threaded Step
  2. Parallel Steps
  3. Remote Chunking
  4. Partitioning

For Processing Multiple Files we will be using Partitioning.

Partitioning

The Job is executing on the left hand side as a sequence of Steps, and one of the Steps is labelled as a Master. The Slaves in this picture are all identical instances of a Step, which could in fact take the place of the Master resulting in the same outcome for the Job. The Slaves are typically going to be remote services, but could also be local threads of execution. The messages sent by the Master to the Slaves in this pattern do not need to be durable, or have guaranteed delivery: Spring Batch meta-data in the JobRepository will ensure that each Slave is executed once and only once for each Job execution.
If required, we can pass data from the master to the slave. The meta data (i.e. the JobRepository), makes sure that every slave is executed only once in a single execution of the Job.

Read More

Share Comments

Angular Material Tabs with Router

In this article, I will show you how to Use Angular Material Tab Component with Angular Routing. The Article is based on Angular 6.
We will Create small application using angular cli and will add needed component step by step.

Step 1: Create Angular 6 Project

Run the command in angular cli
1
ng new angular-material-tab-router

Step 2: Add Angular material to project

Run the command in angular cli
1
npm install --save @angular/material @angular/cdk @angular/animations

Step 3: Add Angular PreBuild Theme to project.

We will be using indigo-pink Theme.

styless.css
1
@import "~@angular/material/prebuilt-themes/indigo-pink.css";

Read More

Share Comments

Java 8 Custom Collector

In this Article , we’ll Discuss How we can Create our own Custom Collector in java 8 and above.
For this article, We will implement Summary Statistics For BigDecimal number.

Java 8 Summary Statistics

Java 8 Provides Summary Statistics for Long,Int & Double. These Summary classes will help you to get count, min, max, sum, and average values.

  • LongSummaryStatistics
  • IntSummaryStatistics
  • DoubleSummaryStatistics
IntSummaryStatistics Example

In below example we are trying to calculate count, min, max, sum, and average of 1 to 100 Integer.

Calculate Sum,Min,Max,Count & Average
1
2
3
4
5

IntSummaryStatistics summaryStatistics = IntStream.range(1, 101)
.summaryStatistics();
System.out.println(summaryStatistics);

Read More

Share Comments

Sort Map by Value using Custom Comparator

In this Article , we’ll Discuss How we can Sort map by Value using Comparator in java 8.

Name class as key of HashMap
1
2
3
4
5
public class Name {
private String firstName;
private String lastName;
//builder
}
Name class as key of HashMap
1
2
3
4
5
6
  
public class Age {
private Integer year;
private Integer month;
//builder
}

We want to sort Map by Age.Year.

Sort map by value using Comparator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
	
Map<Name, Age> map = new HashMap<>();

Name name0 = Name.builder().firstName("Zendor").lastName("Sonawane").build();
Name name1 = Name.builder().firstName("Niraj").lastName("Sonawane").build();
Name name2 = Name.builder().firstName("Pratik").lastName("Sonawane").build();
Name name3 = Name.builder().firstName("Jeetesh").lastName("Sonawane").build();
Name name4 = Name.builder().firstName("Rahul").lastName("Sonawane").build();
Name name5 = Name.builder().firstName("Amit").lastName("Sonawane").build();

Age age0 = Age.builder().year(30).month(5).build();
Age age1 = Age.builder().year(66).month(3).build();
Age age2 = Age.builder().year(17).month(6).build();
Age age3 = Age.builder().year(3).month(5).build();
Age age4 = Age.builder().year(50).month(5).build();
Age age5 = Age.builder().year(80).month(12).build();

map.put(name0,age0);
map.put(name1,age1);
map.put(name2,age2);
map.put(name3,age3);
map.put(name4,age4);
map.put(name5,age5);

Comparator<Age> byAge = (Age obj1,Age obj2) -> obj1.getYear().compareTo(obj2.getYear());

LinkedHashMap<Name, Age> sortedMap = map.entrySet().stream()
.sorted(Map.Entry.<Name,Age>comparingByValue(byAge))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(e1,e2)->e1,LinkedHashMap::new));

Source Code Github Link

Share Comments

Sort Map by key using Custom Comparator

In this Article , we’ll Discuss How we can Sort map by Custom key or Comparator in java 8.

We want to sort below Map by FirstName. Name object is used as key for Map.

Name class as key of HashMap
1
2
3
4
5
public class Name {
private String firstName;
private String lastName;
//builder
}
Sort map by Key using Comparator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	
Map<Name, Integer> map = new HashMap<>();
Name name0 = Name.builder().firstName("Zendor").lastName("Sonawane").build();
Name name1 = Name.builder().firstName("Niraj").lastName("Sonawane").build();
Name name2 = Name.builder().firstName("Pratik").lastName("Sonawane").build();
Name name3 = Name.builder().firstName("Jeetesh").lastName("Sonawane").build();
Name name4 = Name.builder().firstName("Rahul").lastName("Sonawane").build();
Name name5 = Name.builder().firstName("Amit").lastName("Sonawane").build();
map.put(name0,55);
map.put(name1,1);
map.put(name2,2);
map.put(name3,3);
map.put(name4,4);
map.put(name5,5);

This is how we can do that

Sort map by Key using Comparator
1
2
3
4
5
6

Comparator<Name> byName = (Name o1, Name o2)-> o1.getFirstName().compareTo(o2.getFirstName());

LinkedHashMap<Name, Integer> sortedMap = map.entrySet().stream()
.sorted(Map.Entry.<Name,Integer>comparingByKey(byName))
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(e1,e2)->e1,LinkedHashMap::new));

Source Code Github Link

Share Comments

Java Stream - Sort map by value

In this Article , we’ll Discuss How we can Sort map by Value in java 8.

We want to sort below Map by Value

Sort map by Value
1
2
3
4
5
6
7
Map<String, Integer> map = new HashMap<>();
map.put("Niraj", 6);
map.put("Rahul", 43);
map.put("Ram", 44);
map.put("Sham", 33);
map.put("Pratik", 5);
map.put("Ashok", 5);

Map Sorting using comparingByValue in Ascending order

Sort map by Value Ascending order
1
2
3
4
Map<String, Integer> sortedMapByValueAscending 
= map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1,LinkedHashMap::new));

Map Sorting using comparingByValue in Descending order. For Descending order you need to use reversed()

Sort map by Value Descending order
1
2
3
4
Map<String, Integer> sortedMapByValueDescending
= map.entrySet().stream()
.sorted(Map.Entry.<String,Integer>comparingByValue().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1 ,LinkedHashMap::new));

Source Code Github Link

Share Comments

Java Stream - Sort map by key

Photo by Anton Lecock on Unsplash




In this Article , we’ll Discuss How we can Sort map by key in java 8.

We want to sort below Map by key
Sort map by Key
1
2
3
4
5
6
7
Map<String, Integer> map = new HashMap<>();
map.put("Niraj", 6);
map.put("Rahul", 43);
map.put("Ram", 44);
map.put("Sham", 33);
map.put("Pratik", 5);
map.put("Ashok", 5);

Map Sorting using comparingByKey in Ascending order

Sort map by Key Ascending order
1
2
3
4
Map<String, Integer> sortedMapByValueAscending 
= map.entrySet()
.stream().sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1,LinkedHashMap::new));

Map Sorting using comparingByKey in Descending order. For Descending order you need to use reversed()

Sort map by Key Descending order
1
2
3
4
Map<String, Integer> sortedMapByValueDescending
= map.entrySet()
.stream().sorted(Map.Entry.<String,Integer>comparingByKey().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1 ,LinkedHashMap::new));

Source Code Github Link

Share Comments

Angular pipes

Angular Pipes are used to transforms texts.

A pipe takes in data as input and transforms it to a desired output

LowerCase Pipe

Transforms text to all lower case. It is used as follows.

{{value_expression | lowercase }}

UpperCase Pipe

Transforms text to all Upper case. It is used as follows.

{{ value_expression | uppercase  }} 

TitleCasePipe

Transforms text to title case. Capitalizes the first letter of each word, and transforms the rest of the word to lower case. Words are delimited by any whitespace character, such as a space, tab, or line-feed character.

{{ value_expression | titlecase }}

Date Pipe

Converts the Date to human-friendly date

CurrencyPipe

Transforms a number to a currency string, formatted according to locale rules that determine group sizing and separator, decimal-point character, and other locale-specific configurations.

Share Comments

Kotlin Smart Casts

Many times while working we need to check if an object is of certain type at runtime.

In java we have instanceof operator to check whether the object is an instance of the specified type.

instanceof Java Example
1
2
3
4
5
6
public class InstanceofExample {	
public static void main(String[] args) {
MyClass obj=new MyClass();
System.out.println(obj instanceof MyClass);//true
}
}

In Kotlin, You can check whether an object is of a certain type at runtime by using the is operator.

is operator Kotlin Example
1
2
3
4
5
6
7
8
9
if (obj is String) {
print(obj.length)
}
if (obj !is String) { // same as !(obj is String)
print("Not a String")
}
else {
print(obj.length)
}

Smart Casts

Kotlin Complier is quite smart and help us to avoid boilerplate code.

In many cases we do not need to use explicit cast operators , because the compiler tracks the is -checks and explicit casts for immutable values and inserts (safe) casts automatically when needed:

Smart Casts Kotlin Example
1
2
3
4
5
fun demo(x: Any) {
if (x is String) {
print(x.length) // x is automatically cast to String
}
}

The compiler is smart enough to know a cast to be safe if a negative check leads to a return:

Smart Casts Kotlin Example
1
2
if (x !is String) return
print(x.length) // x is automatically cast to String

Such smart casts work for when-expressions and while-loops as well:

Smart Casts Kotlin Example
1
2
3
4
5
when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}

Share Comments

Remove Optional Empty/Null values from list

In this Article , we’ll Discuss, How we can Convert Stream of Optional elements to a Stream of present value elements.

Java 8 has added Optional type to avoid null pointer exception.

lets say we have List<Optional<String>> and for further processing we want List<Strings>.
In This case we need to remove the null and empty elements from stream and convert it into a Stream of present value elements.

Convert Stream of Optional elements to a Stream of present value elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Input List 
List<Optional<String>> list = new ArrayList<>();
list.add(Optional.empty());
list.add(Optional.of("Str1"));
list.add(Optional.of("Str2"));
list.add(Optional.empty());
list.add(Optional.ofNullable(null));
//Using Filter
List<String> listWithoutNull = list.stream()
.filter(Optional::isPresent)
.map(obj ->obj.get())
.collect(Collectors.toList());

//Using removeIf (if that list supports removal )
list.removeIf(iteam->!iteam.isPresent());



Java 9

In java 9 We can easily convert Stream of optionals to a stream of present values.
Using newly addded Optional::stream API

java 9
1
2
3
List<String> listWithoutNull = list.stream()
.flatMap(Optional::stream)
.collect(Collectors.toList());
Share Comments