Skip to content

Instantly share code, notes, and snippets.

View LokeshAggarwal2's full-sized avatar

LokeshAggarwal2

View GitHub Profile
public class Application {
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
Scanner sc= new Scanner(System.in);
System.out.print("Enter a car");
String str= sc.nextLine();
Car car = carFactory.getCar(str);
car.setColor();
car.getColor();
}
public class CarFactory {
public Car getCar(String carType){
if(carType == null) {
return null;
}
if(carType.equalsIgnoreCase("Hyundai")) {
return new Hyundai();
}
else if(carType.equalsIgnoreCase("Maruti")) {
public class Tata extends Car{
@Override
public void setColor() {
color = "Tata color is Grey";
}
}
public class Maruti extends Car{
@Override
public void setColor() {
color = "Maruti car color is Red";
}
}
public class Hyundai extends Car{
@Override
public void setColor() {
color = "Hyundai car color is black";
}
}
public abstract class Car {
protected String color;
public abstract void setColor();
public void getColor() {
System.out.println("car color:"+ color);
}
}
@LokeshAggarwal2
LokeshAggarwal2 / UserRegistrationResilience4j
Created August 3, 2020 15:37
UserRegistrationResilience4j
@Service
public class UserRegistrationResilience4j {
@Bulkhead(name = "bulkheadService1", fallbackMethod = "bulkHeadFallback")
public String registerSeller(SellerDto sellerDto) throws InterruptedException {
String response = restTemplate.postForObject("/addSeller", sellerDto, String.class);
return response;
}
public String bulkHeadFallback(SellerDto sellerDto, Throwable t) {
logger.error("Inside bulkHeadFallback, cause - {}", t.toString());
@LokeshAggarwal2
LokeshAggarwal2 / UserRegistrationController
Created August 3, 2020 15:35
UserRegistrationController
@RestController
public class UserRegistrationController {
@PostMapping("/register/seller")
public String registerAsSeller(@RequestBody SellerDto sellerDto) throws InterruptedException {
String registerSeller = null;
for (int i = 0; i < 100; i++) {
long start = System.currentTimeMillis();
new Thread(() -> {
try {
userRegistrationResilience4j.registerSeller(sellerDto);
resilience4j.bulkhead:
instances:
bulkheadService1:
maxWaitDuration: 1000ms
maxConcurrentCall: 2
resilience4j.thread-pool-bulkhead:
instances:
bulkheadService1:
maxThreadPoolSize: 1
@LokeshAggarwal2
LokeshAggarwal2 / UserRegistrationResilience4j
Created August 2, 2020 16:12
UserRegistrationResilience4j
@Service
public class UserRegistrationResilience4j {
@CircuitBreaker(name = "service1", fallbackMethod = "fallbackForRegisterSeller")
public String registerSeller(SellerDto sellerDto) {
String response = restTemplate.postForObject("/addSeller", sellerDto, String.class);
return response;
}
public String fallbackForRegisterSeller(SellerDto sellerDto, Throwable t) {