我有一个要求,我想使用ftp并将文件放在动态本地目录中。我还想添加一个年龄过滤器。那是不允许旧文件通过。这适用于文件。我添加了一个自定义过滤器,并使用以下代码检查了文件对象的创建日期:
int ageLimit = Integer.parseInt(props.getProperty("file.age"));
BasicFileAttributes view = null;
try
{
view = Files.getFileAttributeView(
Paths.get(f.getAbsolutePath()),
BasicFileAttributeView.class).readAttributes();
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileTime ft = view.creationTime();
if (((new Date()).getTime() - (((ft.to(TimeUnit.MILLISECONDS))/* / 10000L) - 11644473600000L*/))) > ageLimit
* (24 * 60 * 60 * 1000))// file creation
// date(converted
// to
// java.util.date)
// - current
// date >
// oldness in ms
{
logger.info("File is old:" + (f.getName()));
return false;
}
这适用于int-file适配器。但是当我添加ftp支持时,我在有效负载中获得的对象是一个FTPFile对象。这不会给出创建日期,但会给出最后修改日期。最后修改日期对我没用。因此,我必须配置ftp,然后将文件适配器链接到目录。
int-ftp:inbound-channel-adapter从ftp站点获取文件并将其放在本地目录中。
此文件由int-file:inbound-channel-adapter选取并放入最终目标。
这是拾取放置在ftp位置的文件的两步过程。
filter类适用于int-file:inbound-channel-adapter。这种链接正在发挥作用。
问题在于删除原始文件。 ftp站点文件(remote-directory)会自动删除。 int-file:inbound-channel-adapter选取的本地目录文件不会被删除。
这是我的配置。
<int:channel id="ftpChannel"/>
<int-ftp:outbound-channel-adapter id="ftpOutbound"
channel="ftpChannel"
session-factory="ftpClientFactory"
charset="UTF-8"
remote-file-separator="/"
auto-create-directory="true"
remote-directory="."
use-temporary-file-name="true"
/>
<int-ftp:inbound-channel-adapter id="ftpInbound"
channel="ftpChannel"
session-factory="ftpClientFactory"
charset="UTF-8"
local-directory="file:${paths.root}"
delete-remote-files="true"
temporary-file-suffix=".writing"
remote-directory="."
filename-pattern="*${file.char}*${file.char}*${file.char}*${file.char}*${file.char}*"
preserve-timestamp="true"
auto-startup="true">
<int:poller fixed-rate="1000"/>
</int-ftp:inbound-channel-adapter>
<bean id="ftpClientFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="${ftp.ip}"/>
<property name="port" value="${ftp.port}"/>
<property name="username" value="${ftp.username}"/>
<property name="password" value="${ftp.password}"/>
<property name="clientMode" value="0"/>
<property name="fileType" value="2"/>
<property name="bufferSize" value="100000"/>
</bean>
<int-file:outbound-channel-adapter channel="abc" id="filesOut"
directory-expression="@outPathBean.getPath()"
delete-source-files="true" filename-generator="fileNameGenerator" />
<int:header-enricher input-channel="ftpChannel" output-channel="ftpChannel">
<int:header name="file_originalFile" ref="getPath" method="getCurrentPath" />
</int:header-enricher>
<int-file:inbound-channel-adapter id="filesIn" directory="file:${paths.root}" channel="abc" filter="compositeFilter" >
<int:poller id="poller" fixed-delay="5000" />
</int-file:inbound-channel-adapter>
<int:channel id="abc"/>
还定义了customfilefilter和filenamegenerator bean。
我已经添加了header-richher来添加file_originalFile标头,因为我在某处读取了如果文件在我的情况下被使用两次/链接,则删除此标头。虽然bean方法被调用并且我在其中编写了以下代码,但文件不会从文件适配器源目录中删除。
public String getCurrentPath(Message<File> payload)
{
File f = payload.getPayload();
if (payload.getHeaders().get(FileHeaders.ORIGINAL_FILE)== null)
{
return f.getAbsolutePath();
}
else
{
payload.getHeaders().get(FileHeaders.ORIGINAL_FILE).toString();
}
return null;
}
我做错了什么?我已经尝试过使用header-richher来获取abc频道而不是运气!
希望现在问题很明显..感谢您的帮助
答案 0 :(得分:0)
关于:
无法将[org.springframework.integration.endpoint.EventDrivenConsumer]类型的值转换为属性“outputChannel”所需的类型[org.springframework.messaging.MessageChannel]:找不到匹配的编辑器或转换策略
由于您有<int-ftp:outbound-channel-adapter id="ftpOutbound" channel="ftpChannel"
您应该使用<int:transformer id="testTransformer" input-channel="ftpInbound"
的频道 bean名称,而不是该适配器的id
<int:transformerd="testTransformer" input-channel="ftpChannel"
答案 1 :(得分:0)
您的问题并不完全清楚。
也就是说,Spring Integration 3.0 introduced the local-filter
on the (s)ftp adapters应允许您在那里使用自定义过滤器。
要使用标题删除本地文件,您可以使用请求处理程序建议,有关详细信息,请参阅retry-and-more示例。
您的例外是因为您将出站适配器注入变换器的output-channel
;你应该注入ftpChannel
。