【问题标题】:Spring Integration DSL Error Handler Thread IDSpring 集成 DSL 错误处理程序线程 ID
【发布时间】:2021-09-08 16:05:07
【问题描述】:

目前,由于在没有任何处理线程之前不让系统关闭,我正在跟踪正在处理的活动线程

例如

package com.example.demo.flow;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.dsl.*;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.file.dsl.Files;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.Executors;

/**
 * Created by on 03/01/2020.
 */
@Component
@Slf4j
public class TestFlow {

    @Bean
    public StandardIntegrationFlow errorChannelHandler() {

        return IntegrationFlows.from("testChannel")
                .handle(o -> {

                    log.info("Handling error....{}", o);
                }).get();
    }

    @Bean
    public IntegrationFlow testFile() {


        IntegrationFlowBuilder testChannel = IntegrationFlows.from(Files.inboundAdapter(new File("d:/input-files/")),
                e -> e.poller(Pollers.fixedDelay(5000L).maxMessagesPerPoll(5)
                        .errorChannel("testChannel")))
                .channel(MessageChannels.executor(Executors.newFixedThreadPool(5)))
                .transform(o -> {

                    throw new RuntimeException("Failing on purpose");

                }).handle(o -> {
                });

        return testChannel.get();


    }


}

我为集成流启用了多个文件,但在错误处理程序中线程不同 我怎么知道它来自哪个线程?

有没有我能找到的,因为这很关键

【问题讨论】:

    标签: java error-handling spring-integration spring-integration-dsl


    【解决方案1】:

    根据您当前的配置,testChannelDrectChannel,因此无论您发送给它什么,都将在您发送的线程上进行处理。 因此Thread.currentThread() 足以让您确定它。

    对于更通用的解决方案,请考虑将 MessagePublishingErrorHandler 作为 bean 并使用 ChannelUtils.MESSAGE_PUBLISHING_ERROR_HANDLER_BEAN_NAME 覆盖默认值。这个MessagePublishingErrorHandler 可以提供自定义ErrorMessageStrategy。在那里,当您创建 ErrorMessage 时,您可以添加具有相同 Thread.currentThread() 信息的自定义标头,以进行该错误通道处理,即使它是在单独的线程中完成的。

    您也可以使用该信息引发异常!

    【讨论】:

    • Thread.currentThread() 仅在有单个线程时工作,但假设我在错误通道中一次收到两个文件,现在线程 ID 不同例如线程 1 和线程 2 启动实际的线程但在错误通道中它是线程 3 我不知道的东西
    • 好吧,您可能有点困惑,轮询器是在其自己的预定线程上完成的,然后您将工作转移到ExecutorChannel。但是您对Thread.currentThread() 的结论仍然不清楚。您可以在执行程序通道之前(或之后)使用enrichHeaders() 将当前线程信息添加到标头中。无论如何,那个都会被错误处理程序处理。
    • 我试过它不会传播当前线程
    • 请在您的问题中显示您是如何尝试的?
    • 我想我的实现一定是错误的。你是对的, Thread.currentThread() 看起来确实一样。我会尝试更多并回来。感谢您的及时回复
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2019-02-22
    • 2013-03-30
    相关资源
    最近更新 更多