首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打开和关闭BlockHound检查

如何打开和关闭BlockHound检查
EN

Stack Overflow用户
提问于 2022-03-14 08:25:56
回答 1查看 124关注 0票数 0

我在WebFlux中有一些应用程序,我想使用BlockHound,但我需要通过application.properties中的参数化或春季分析或其他一些方法来打开和关闭它。另外,当锁定操作被捕获时,我希望重写操作,这样就不会抛出错误,而会引发日志警告。首先,我在application.properties中使用了参数:

代码语言:javascript
复制
@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())));
    }
}

我的BlockHoundSwitch:

代码语言:javascript
复制
@Component("BlockHoundSwitchBean")
@Getter
public class BlockHoundSwitch {
    @Value("${blockhound.enabled}")
    private boolean blockHoundEnabled;
}

它对我有效,但在我看来,这个解决方案相当困难,有点不可预测。接下来,我尝试通过分析来解决此任务:

代码语言:javascript
复制
@Profile("blockhound_enabled")
@Slf4j
@Component()
public class BlockHoundSwitch {

    public BlockHoundSwitch() {
        BlockHound.install(builder ->
                builder.blockingMethodCallback(it ->
                        log.warn("find block operation: {}", it.toString())));
    }
}

而且效果也很好。嗯,我有几个问题:

  1. 哪种方式更好,为什么,也许还有另一种解决方案?
  2. ,我需要定位和记录,在哪里发生块操作。我怎样才能得到类名和方法,在哪里发生的?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-23 08:33:10

我来解决它。也许有人能帮上忙。我是通过分析和我的代码实现的:

代码语言:javascript
复制
@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 -我的项目的主要包,我在堆栈中找到它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71464826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档