Spring集成 - 文件入站通道适配器

时间:2017-02-28 03:47:11

标签: spring-integration

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

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

由于我在这里使用了入站通道适配器,我没有使用Gateway作为入口点。这有什么不对吗?

@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");
   }
  }

1 个答案:

答案 0 :(得分:0)

我想日志中有一些有趣的东西。看起来你的变压器会抛出错误。这就是为什么你无法进入下一个组件的原因。

此外,您可以为DEBUG类别启用org.springframework.integration,并调查日志邮件的传输方式。

相关问题