【问题标题】:Spring Integration - File Inbound Channel AdapterSpring Integration - 文件入站通道适配器
【发布时间】:2023-03-12 11:54:01
【问题描述】:

我正在尝试带有文件的示例程序 - 入站通道适配器。我只想使用适配器读取文件并将其传递给转换器以转换为地图,最后将其传递给服务激活器以打印地图。

当我运行程序时,它从适配器到达转换器,但根本没有到达服务激活器。

因为我在这里使用了入站通道适配器,所以我没有使用网关作为入口点。这有什么问题吗?

@Configuration
public class SpringIntegrationAdapterConfig {

static Logger log = Logger.getLogger(SpringIntegrationAdapterConfig.class);

@Bean
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageResource(){
    FileReadingMessageSource source = new FileReadingMessageSource();
    source.setDirectory(new File("C:\\Rajashree\\work\\test"));
    source.setFilter(new SimplePatternFileListFilter("Sample.csv"));

    log.info("Reading file using File Adapter");

    return source;
}
}

@Component
public class FileService {

static Logger log = Logger.getLogger(FileService.class);

@Transformer(inputChannel = "fileInputChannel", outputChannel =  "mappingChannel")
public List<Map<String, String>> readFile(File file){
    log.info(file.getName());

    List<Map<String, String>> dataList = new ArrayList<>();
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader();

    try(CSVParser parser = new CSVParser(new FileReader(file), csvFormat)){
        parser.getRecords().stream().map(e ->    dataList.add(e.toMap())).collect(Collectors.toList());
        log.info(dataList);

    } catch (IOException e) {
        log.error("File read Error : " + e);
    }

    return dataList;
  }
 }


 @Component
 public class MappingTransformer {

     @Transformer(inputChannel = "mappingChannel", outputChannel =   "printChannel")
     public List<Map<String, String>> mapFields(List<Map<String, String>> dataList){
      System.out.println("File mapped :: " + dataList );
      return dataList;
     }
}

  @MessageEndpoint
  public class printService{

   @ServiceActivator(inputChannel="printChannel", outputChannel=  "outputChannel")
   public void print(List<Map<String, String>> dataList){
       System.out.println("Message Printed");
   }
  }

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    我猜日志中有一些有趣的东西。看起来你的变压器抛出错误。这就是你无法到达下一个组件的原因。

    此外,您可以为org.springframework.integration 类别打开DEBUG,并调查您的消息如何传播的日志。

    【讨论】:

    • 变压器工作正常。我使用普通方法加载文件并通过网关调用转换器。整个流程运行良好。但我想使用文件适配器加载文件,但它不工作。
    • 请用日志和堆栈状态确认您的“但是”。
    • 让我知道...谢谢
    猜你喜欢
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2016-11-17
    • 2017-10-08
    • 1970-01-01
    • 2012-09-24
    • 2017-11-10
    相关资源
    最近更新 更多