SpringBoot9AM 21122020
SpringBoot9AM 21122020
StopWatch (C):
This is a class given by Spring F/w , package: org.springframework.util
--Method--
start() : It will start time counting
start(taskName)
stop(): It will stop time counting
getTotalTimeMillis(): long
getTotalTimeSeconds():int
prettyPrint(): String
------------------------------------------
------------------------------Example#1---------------------------
package in.nareshit.raghu.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Component
public class TimeTestRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//1. Create StopWatch Object
StopWatch watch = new StopWatch();
}
---------------------------Example#2---------------------------
package in.nareshit.raghu.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Component
public class TimeTestRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//1. Create StopWatch Object
//StopWatch watch = new StopWatch();
StopWatch watch = new StopWatch("Time Test For Loops"); //Watch#ID
watch.start("Loop#1"); //Watch#taskName
for (int i = 0; i < 999999999; i++) {
Math.pow(i+1, 909856);
Math.pow(i+1, 909856);
}
watch.stop();
watch.start("Loop#2");
for (int i = 0; i < 889999999; i++) {
Math.pow(i+1, 999956);
}
watch.stop();
watch.start("Loop#3");
for (int i = 0; i < 666699999; i++) {
Math.pow(i+1, 998856);
Math.pow(i+1, 998856);
}
watch.stop();
//4. Stop watch
//watch.stop();
}
========================================================================
*) Note:
*) For below code
watch.stop(); //watch stopped
watch.stop(); //exception
IllegalStateException: Can't stop StopWatch: it's not running
--------------------------------------------------------------
*) TimeUnit (Enum # Java 5)
It supports time factors and conversions
---------------------------_Example#3_----------------------
package in.nareshit.raghu.runner;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class TimeFactorRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//3 days-> hours
System.out.println(TimeUnit.DAYS.toHours(3));
//3 days-> mins
System.out.println(TimeUnit.DAYS.toMinutes(3));
When we start any Spring Boot application one logo is printed at console
called as Banner.
=> This Banner setup and printing is done when we run starer class.
=> We can even customize our code ie OFF Banner, modify data..etc
package in.nareshit.raghu;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot2StopWatchApplication {
SpringApplication sa = new
SpringApplication(SpringBoot2StopWatchApplication.class);
sa.setBannerMode(Banner.Mode.OFF);
sa.run(args);
}
=> Default Banner.Mode.CONSOLE, banner is printed at console.
=> To provide our own banner file, you need to create one txt file under
scr/main/resources folder. Bcoz Spring Boot has provided
internal key:
spring.banner.location=classpath:banner.txt
Copy Banner data and paste in banner.txt file (press ctlt+s and ctlr+F11)
----------------------------------------------------------------
Q) What are Command Line Arguments and VM/System Args?
Syntax: -Dkey=val
Read : System.getProperty(key):val
-------------------------------------------------------------------
*)Note:
We can give setup data to Spring Boot application (Properties/Arguments data)
using below order:
b) VM Arguments
-Dkey=val
c) YAML Arguments
prefix:
variable:<space>value
d) Properties Arguments
prefix.variable=value
VM Arguments
-Dmy.app.export.data=VMA
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class DataInputTestRunner implements CommandLineRunner {
@Value("${my.app.export.data}")
private String exportData;
@Override
public void run(String... args) throws Exception {
System.out.println(exportData);
}
}
----------------------------------
*) also create : application.yml and application.properties
with above key and check order.
================================================================
enum Result {
PASS, FAIL, ABSENT
}
enum Gender {
MALE, FEMALE, OTHER
}
show(String s) { }
show(Result r) { }
Enums Basics
https://www.youtube.com/c/NareshIT/search?query=enum