简体   繁体   English

Spring Batch Java配置-写入没有本地文件的远程sftp xml文件

[英]Spring Batch Java configuration - Writing to remote sftp xml file without local file

I have a requirement to write xml file to a sftp server in a Spring Batch application. 我需要将xml文件写入Spring Batch应用程序中的sftp服务器。 Currently below code writes xml file to local file system using StaxEventItemWriter. 当前,以下代码使用StaxEventItemWriter将xml文件写入本地文件系统。 I need to write directly to remote server instead of writing it to local and then moving to the sftp server. 我需要直接写入远程服务器,而不是将其写入本地,然后再移动到sftp服务器。 Referred this link ( Writing to a remote file using Spring Integrations Sftp Streaming java configuration ) but not sure how to write using StaxEventItemWriter/setup Resource object with remote file 引用了此链接( 使用Spring Integrations Sftp Streaming java配置写入远程文件 ),但不确定如何使用StaxEventItemWriter / setup资源对象和远程文件进行写入

public void write(List<? extends UserDTO> items) throws Exception {
    for(UserDTO item : items) {
        StaxEventItemWriter<UserDTO> staxWriter = getStaxEventItemWriter(item);
        staxWriter.write(Arrays.asList(item));
    }
}

private StaxEventItemWriter<UserDTO> getStaxEventItemWriter(UserDTO user) {

    String key = user.getDomain();      
    StaxEventItemWriter<UserDTO> writer = writers.get(key);
    if (writer == null) {enter code here
        writer = new StaxEventItemWriter<>();
        try {
            UrlResource resource = new UrlResource("file:"+outputDir+"/"+key+"_"+fileName+".xml");
            writer.setResource(resource);
            writer.setRootTagName("customerSet");
            Jaxb2Marshaller UserMarshaller = new Jaxb2Marshaller();
            UserMarshaller.setClassesToBeBound(UserDTO.class);
            writer.setMarshaller(UserMarshaller);
            writer.setOverwriteOutput(Boolean.TRUE);
            writer.open(executionContext);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        writers.put(key, writer);

    }
    return writer;
}

您可能可以尝试使用基于Spring Integration的SftpResource (类似于您共享的链接中的解决方案),并在StaxEventItemWriter使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM