I have some App with WebFlux and i want to use BlockHound, but i need to have a possible turn on and off it through parameter in application.properties or through spring profiling or somthing else. Also I want to override action, when the lock operation is caught so that not throw error but log warning. And firstly, i did through parameter in application.properties:
@SpringBootApplication @Slf4j public class GazPayApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(GazPayApplication.class, args); BlockHoundSwitch blockHoundSwitch = (BlockHoundSwitch)context.getBean("BlockHoundSwitchBean"); if (blockHoundSwitch.isBlockHoundEnabled()) { BlockHound.install(builder -> builder.blockingMethodCallback(it -> log.warn("find block operation: {}", it.toString()))); } }
And my BlockHoundSwitch:
@Component("BlockHoundSwitchBean") @Getter public class BlockHoundSwitch { @Value("${blockhound.enabled}") private boolean blockHoundEnabled; }
It works for me but in my opinion this solution quite difficult and a little unpredictable. Next i tried resolve this task through profiling:
@Profile("blockhound_enabled") @Slf4j @Component() public class BlockHoundSwitch { public BlockHoundSwitch() { BlockHound.install(builder -> builder.blockingMethodCallback(it -> log.warn("find block operation: {}", it.toString()))); } }
And it works too. Well, I have a few questions:
- Which way is better, why and maybe there is another solution?
- I need to localize and log, where block operation happened. How can I get class name and method, where it`s happened?
Advertisement
Answer
I resolve it. Maybe someone it will be useful. I did it through profiling and my code bellow:
@Profile("blockhound_on") @Slf4j @Component() @Getter public class BlockHoundSwitch { public BlockHoundSwitch() { BlockHound.install(builder -> builder.blockingMethodCallback(it -> { List<StackTraceElement> itemList = Arrays.stream(new Exception(it.toString()).getStackTrace()) .filter(i -> i.toString().contains("application.package")) .collect(Collectors.toList()); log.warn("find block operation: n{}", itemList); })); } }
where application.package – main package of my project, which i`m finding in stacktrace.