小编AnM*_*nMi的帖子

千分尺永远不会更新

我正在尝试构建一个用于千分尺的自定义量规,它应该监视自定义 ThreadPoolTask​​Executor。问题在于,仪表值保持不变,并且自应用程序启动以来从未更新。但是,执行器统计信息(可以在调试器中看到)显示不断变化的值。

这是我的仪表配置类。

@Component
public class ThreadPoolMetricProbe implements MeterBinder {

@Inject
private Executor taskExecutor;

@Override
public void bindTo(MeterRegistry meterRegistry) {
    ThreadPoolTaskExecutor exec = (ThreadPoolTaskExecutor) taskExecutor;

    long completedTaskCount = exec.getThreadPoolExecutor().getCompletedTaskCount();
    int largestPoolSize = exec.getThreadPoolExecutor().getLargestPoolSize();
    int maximumPoolSize = exec.getThreadPoolExecutor().getMaximumPoolSize();
    int poolSize = exec.getThreadPoolExecutor().getPoolSize();
    long taskCount = exec.getThreadPoolExecutor().getTaskCount();
    int activeCount = exec.getThreadPoolExecutor().getActiveCount();

    String threadNamePrefix = exec.getThreadNamePrefix();

    Gauge.builder("executorCompletedTaskCount", completedTaskCount, Double::new)
            .description(String.format("Returns the approximate total number of tasks that have completed execution of %s executor", threadNamePrefix.toLowerCase()))
            .register(meterRegistry);

    Gauge.builder("executorLargestThreadsNumber", largestPoolSize, Double::new)
            .description(String.format(" Returns the largest number …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-boot-actuator spring-micrometer

13
推荐指数
1
解决办法
9632
查看次数

CompletableFuture:运行期货列表、等待结果和处理异常的正确方法

我有一个遗留代码,它有十几个数据库调用来填充报告,我试图减少使用CompletableFuture.

我怀疑我是否正确地做事并且没有过度使用这项技术。

我的代码现在看起来像这样:

  1. 在每个方法中使用许多数据库调用启动文档部分的异步填充

    CompletableFuture section1Future = CompletableFuture.supplyAsync(() -> populateSection1(arguments));
    CompletableFuture section2Future = CompletableFuture.supplyAsync(() -> populateSection2(arguments));
        ...
    CompletableFuture section1oFuture = CompletableFuture.supplyAsync(() -> populateSection10(arguments));
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后我按特定顺序安排期货arrayList并加入所有期货以确保我的代码只有在所有期货都完成后才能进一步运行。

    List<CompletableFuture> futures = Arrays.asList(
                section1Future,
                section2Future, ...
                section10Future);
    
    List<Object> futureResults = futures.stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());
    
    Run Code Online (Sandbox Code Playgroud)
  3. 然后我用它的碎片填充 PDF 文档本身

    Optional.ofNullable((PdfPTable) futureResults.get(0)).ifPresent(el -> populatePdfElement(document, el));
    Optional.ofNullable((PdfPTable) futureResults.get(1)).ifPresent(el -> populatePdfElement(document, el));
        ...
    Optional.ofNullable((PdfPTable) futureResults.get(10)).ifPresent(el -> populatePdfElement(document, el));
    
    Run Code Online (Sandbox Code Playgroud)

    退回文件

我的担忧是:

1) 以这种方式创建和实例化许多 Completable Futures 是否可以?将它们按要求的顺序排序arrayList,加入它们以确保它们全部完成,然后通过将它们转换为特定对象来获得结果?

2) 可以不指定执行器服务而运行,而是依赖 commonForkJoinPool吗?但是,此代码在 Web 容器中运行,所以可能为了使用 JTA,我需要通过 JNDI 使用容器提供的线程池执行程序? …

java design-patterns asynchronous java-8 completable-future

5
推荐指数
1
解决办法
7777
查看次数