You are on page 1of 3

Date : 30/04/2020

Working with Runner - PART#2


----------------------------------------------------------------------
Duration : 1:15Min 1:30 min
Start timing : 6:10 PM
Payment and Queries :+91- 6302 968 665 (Srikanth)

javabyraghu@gmail.com
https://www.facebook.com/groups/thejavatemple/
============================================================================
Q) How many runners can we define in one Spring Boot Project?

A) Multiple Runners.
For Example: JdbcRunner, BatchExRunner, MongoDbSetupRunner,..etc

**** In case of multiple runners, execution order is given as


Class (Method) Naming Sorting Order(A to Z name rule)

--Custom Execution Order/Programmer define order for multiple Runners---


*) Programmer can define his own exeuction order for multiple runners.
by using code like :
@Order(intVal)

*) By default Order number is given as 2147483647 (Integer.MAX_VALUE)

*****) If two Ruuners are having same order number


then again method naming sorting order (A to Z)

Example (Runners - Order Number)


R#1 - 10
R#2 - 6
R#3 - 6
R#4 - 3
R#5 - (NONE) 2147483647 (Default)

=>Execution order is R#4 -> R#2 -> R#3 -> R#1 -> R#5

========================= Example Code ===============================


#1. Create Spring Starter project
> File > new > Spring Starter Project > Enter Name and groupId/package Name
Name : SpringBootRunnersTwo
GroupId : in.nit
Package : in.nit

#2 Create multiple runner classes under src/main/java


(inside package in.nit.runner)
-------------------
package in.nit.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//ctrl+shift+O (imports)
@Component
@Order(5)
public class JdbcTestRunner implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println("from JDBC Test Runner-5");
}
}
------------
package in.nit.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(8)
public class EmailTestRunner implements CommandLineRunner{

@Override
public void run(String... args) throws Exception {
System.out.println("from Email Test Runner-8");
}
}
---------------
package in.nit.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(6)
public class DevAppTestRunner implements CommandLineRunner{

@Override
public void run(String... args) throws Exception {
System.out.println("from DevAppTest Runner-6");
}
}
------------------
package in.nit.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(6)
public class BatchExRunner implements CommandLineRunner{

@Override
public void run(String... args) throws Exception {
System.out.println("from Batch Runner-6");

}
}

****** Output ********


from JDBC Test Runner-5
from Batch Runner-6
from DevAppTest Runner-6
from Email Test Runner-8

****************************************************************************
----------------------- Order in Legacy Style ----------------------------
****************************************************************************
We can also use one interface : Ordered
It is having one abstract method : getOrder():int
We can implement our runner using this interface and override this
method and provide one int value.

*** if both @Order and Ordered(I) provided then priority is taken


as Ordered(I) only.

-----------Example code--------------------
package in.nit.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//ctrl+shift+O (imports)
@Component
//@Order(10)
public class JdbcTestRunner implements CommandLineRunner,Ordered{

@Override
public void run(String... args) throws Exception {
System.out.println("from JDBC Test Runner-5");
}

@Override
public int getOrder() {
return 4;
}

Number Systems:
.... -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 ...

---Task for You --------------


Runner Order
R#1 -567
R#2 902
R#3 178
R#4 (none)
R#5 450
R#6 -836
-- Execution Order is : ____________________

You might also like