使用Spring集成从Unix读取而不复制到本地时发现循环引用
我想从Unix位置读取.txt文件,而不使用Spring Integration将其复制到本地。这应该在连续模式下完成,即当出现新文件时,应该检测并读取它 代码:使用Spring集成从Unix读取而不复制到本地时发现循环引用,spring,spring-data,spring-integration,spring-cloud,cloud-foundry,Spring,Spring Data,Spring Integration,Spring Cloud,Cloud Foundry,我想从Unix位置读取.txt文件,而不使用Spring Integration将其复制到本地。这应该在连续模式下完成,即当出现新文件时,应该检测并读取它 代码: @SpringBootApplication public class SftpJavaApplication { public static void main(String[] args) { new SpringApplicationBuilder(SftpJavaApplic
@SpringBootApplication
public class SftpJavaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SftpJavaApplication.class)
.web(false)
.run(args);
}
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost("ip");
factory.setPort(port);
factory.setUser("user");
factory.setPassword("pwd");
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
@Transformer(inputChannel = "stream",outputChannel="data")
public org.springframework.integration.transformer.Transformer transformer () {
return new org.springframework.integration.transformer.StreamTransformer("UTF-8");
}
@Bean
@InboundChannelAdapter(value = "stream", poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<InputStream> ftpMessageSource() {
SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template(), null);
messageSource.setRemoteDirectory("/test1/test2/test3");
messageSource.setFilter(new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(),
"streaming"));
return messageSource;
}
@Bean
public SftpRemoteFileTemplate template() {
return new SftpRemoteFileTemplate(sftpSessionFactory());
}
@Bean
@ServiceActivator(inputChannel = "data" )
public MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println(">>>>>>>>>>>>>"+message.getPayload()); //this prints the data in the file
}
};
}
compile("org.springframework.cloud:spring-cloud-spring-service-connector:1.2.1.RELEASE")
compile("org.springframework.cloud:spring-cloud-cloudfoundry-connector:1.2.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-integration")
compile group: 'com.jcraft', name: 'jsch', version: '0.1.44-1'
compile group: 'org.springframework.integration', name: 'spring-integration-sftp', version: '4.3.1.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-file', version: '4.3.1.RELEASE'
compile('org.kie.modules:org-apache-commons-lang3:6.3.0.Final')
compile("com.h2database:h2:1.4.192")
Caused by: org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file
at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:104)
at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:50)
at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.list(CachingSessionFactory.java:218)
at org.springframework.integration.file.remote.RemoteFileTemplate$6.doInSession(RemoteFileTemplate.java:417)
at org.springframework.integration.file.remote.RemoteFileTemplate$6.doInSession(RemoteFileTemplate.java:413)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:435)
... 24 more
Caused by: 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1750)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1767)
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1205)
at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:92)
... 29 more
编译组:'org.springframework.integration',名称:'springintegrationcore',版本:'4.3.1.RELEASE'
堆栈跟踪:
@SpringBootApplication
public class SftpJavaApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SftpJavaApplication.class)
.web(false)
.run(args);
}
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost("ip");
factory.setPort(port);
factory.setUser("user");
factory.setPassword("pwd");
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
@Transformer(inputChannel = "stream",outputChannel="data")
public org.springframework.integration.transformer.Transformer transformer () {
return new org.springframework.integration.transformer.StreamTransformer("UTF-8");
}
@Bean
@InboundChannelAdapter(value = "stream", poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<InputStream> ftpMessageSource() {
SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template(), null);
messageSource.setRemoteDirectory("/test1/test2/test3");
messageSource.setFilter(new SftpPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(),
"streaming"));
return messageSource;
}
@Bean
public SftpRemoteFileTemplate template() {
return new SftpRemoteFileTemplate(sftpSessionFactory());
}
@Bean
@ServiceActivator(inputChannel = "data" )
public MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println(">>>>>>>>>>>>>"+message.getPayload()); //this prints the data in the file
}
};
}
compile("org.springframework.cloud:spring-cloud-spring-service-connector:1.2.1.RELEASE")
compile("org.springframework.cloud:spring-cloud-cloudfoundry-connector:1.2.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-integration")
compile group: 'com.jcraft', name: 'jsch', version: '0.1.44-1'
compile group: 'org.springframework.integration', name: 'spring-integration-sftp', version: '4.3.1.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-file', version: '4.3.1.RELEASE'
compile('org.kie.modules:org-apache-commons-lang3:6.3.0.Final')
compile("com.h2database:h2:1.4.192")
Caused by: org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file
at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:104)
at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:50)
at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.list(CachingSessionFactory.java:218)
at org.springframework.integration.file.remote.RemoteFileTemplate$6.doInSession(RemoteFileTemplate.java:417)
at org.springframework.integration.file.remote.RemoteFileTemplate$6.doInSession(RemoteFileTemplate.java:413)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:435)
... 24 more
Caused by: 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2297)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1750)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:1767)
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1205)
at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:92)
... 29 more
从Spring集成
4.3
开始,远程文件支持(FTP/SFTP)提供streaming
适配器:
<int-ftp:inbound-streaming-channel-adapter id="ftpInbound"
channel="ftpChannel"
session-factory="sessionFactory"
filename-pattern="*.txt"
filename-regex=".*\.txt"
filter="filter"
remote-file-separator="/"
comparator="comparator"
remote-directory-expression="'foo/bar'">
<int:poller fixed-rate="1000" />
</int-ftp:inbound-streaming-channel-adapter>
但是!我们不能“在连续模式下”这样做,因为FTP不是事件驱动协议。所以,我们仍然应该定期轮询远程目录
如果您真的知道如何让它监听远程目录中的一些事件,我们将非常高兴在Spring集成中有这样一个现成的组件
编辑
升级Spring集成的依赖项时,请确保所有模块都在同一版本中,因为版本控制不好
spring集成文件
也应该是4.3.1.RELEASE
哦,你根本不需要它。它是spring集成sftp
的可传递依赖项。
您也不需要spring集成核心,因为它是所有这些核心的可传递依赖项
EDIT2
我只想知道过滤器中使用的“流”一词的意义,它本身并没有阻止我读取文件。但出于知识目的,我想知道
Streaming
适配器的目的是让目标应用程序不创建本地文件副本。只需直接在内存中从远程源读取数据。所以,这就是您仍然可以读取文件的方式,但这是一个远程文件。不是吗
关于PCF上的错误
如果能和我们分享这件事的线索那就太好了。
请从另一端尝试对DefaultSftpSessionFactory
使用allowUnknownKeys=true
。请阅读。Unix/Local是什么意思?您的意思是从远程服务器读取文件吗?是的。我想从远程服务器(Unix位置)读取文件。有人提到Sftp在这方面有帮助。我尝试过,但后来它在本地复制了文件,我不得不一个接一个地读取该文件。但我必须避免这种情况,并在任何新文件可用时读取特定目录中的所有可用文件,方法是避免XML。你能发布你的配置
文件吗我使用java配置,因为我们被要求避免使用xml。上面的代码就是我使用的代码。不要在注释中提供澄清和附加信息。编辑您的问题以使其更清楚。非常感谢。我正在使用通过链接提供的Sftp进行java配置。但是对于bean MessageSource,例外情况是:类型org.springframework.integration.file.remote.AbstractRemoteFileStreamingMessageSource无法解析。它间接引用自必需的.class文件。我正在gradleBean定义中使用spring integration core 4.3.1版本:@Bean@InboundChannelAdapter(channel=“stream”)public MessageSource ftpMessageSource(){SftpStreamingMessageSource=new SftpStreamingMessageSource(template(),null);messageSource.setRemoteDirectory(“sftpSource/”);messageSource.setFilter(新的sftpersistentAcceptonFileListFilter(新的SimpleMetadataStore(),“流”);return messageSource;}仅限核心?AbstractRemoteFileStreamingMessageSource
是spring集成文件的一部分。您必须将所有模块升级到同一版本。或者最好使用spring集成bom
在同一位置管理所有版本。请不要在注释中提供代码。这是完全不可读的。只需编辑您的问题并在此处进行评论。StackTrace也会很有用。好的。我已经添加了代码。请提供您的意见,因为我仍然面临同样的问题