Reputation: 813
I have a integration case that get xml payload from FTP then use http outbound channel to sent the payload to webservice, ftp inbound-channel-adapter has a mandatory attribute named local-directory, remote ftp files will be downloaded here, however when I restart, seems all files in local-directory will be handled again, may I know how to avoid this? One possible way is to delete local file in ftp inbound-channel-adapter, how to do it, can you advise?
thanks
My Spring integration configurations
<ftp:inbound-channel-adapter
channel="requestChannel"
session-factory="ftpClientSessionFactory"
remote-directory="/outbound"
local-directory="/temp"
auto-create-local-directory="true"
delete-remote-files="false"
filename-pattern="*.xml"
temporary-file-suffix=".writing">
<int:poller fixed-delay="5000" max-messages-per-poll="10"/>
</ftp:inbound-channel-adapter>
<int:chain id="inboundChain" input-channel="requestChannel" output-channel="replyChannel">
<int:transformer ref="xmlToJsonTransformer" />
<int:transformer ref="jsonToMapTransformer" />
<int:header-enricher>
<int:header name="Content-Type" value="application/json" overwrite="true"/>
</int:header-enricher>
<http:outbound-gateway expected-response-type="java.lang.String"
url="http://localhost:8080/postService/postupdate"
http-method="POST"
extract-request-payload="true"
request-factory="requestFactory">
</http:outbound-gateway>
</int:chain>
Upvotes: 0
Views: 1819
Reputation: 373
Create a bean of ExpressionEvaluatingRequestHandlerAdvice and set setOnSuccessExpression as payload.delete
@Bean
public ExpressionEvaluatingRequestHandlerAdvice deleteLocalFileAdvice() {
ExpressionEvaluatingRequestHandlerAdvice handlerAdvice =
new ExpressionEvaluatingRequestHandlerAdvice();
SpelExpressionParser spelParser = new SpelExpressionParser();
handlerAdvice.setOnSuccessExpression(
spelParser.parseExpression("payload.delete"));
return handlerAdvice;
}
and provide the bean in adviceChain of MessageHandler
@Bean
@ServiceActivator(inputChannel = "ftpChannel", adviceChain =
"deleteLocalFileAdvice")
public MessageHandler ftpMessageHandler() {
//process indbound file
}
and provide the channel to your @InboundChannelAdapter
@Bean
@InboundChannelAdapter(channel = "ftpChannel", poller = Poller(fixedDelay
= "3000"))
public MessageSource<File> ftpMessageSource() {
// MessageSource config..
}
Upvotes: 0
Reputation: 813
Thanks Gray's advise, here is my corrected configuration
<int:chain id="inboundChain" input-channel="requestChannel" output-channel="replyChannel">
<int:header-enricher>
<int:header name="file_originalFile" expression="payload"/>
<int:header name="file-name" expression="payload.name"/>
<int:header name="file-failed-path" value="/project/ftp/failed/"/>
<int:header name="Content-Type" value="application/json" overwrite="true"/>
</int:header-enricher>
<int:transformer ref="xmlToJsonTransformer" />
<int:transformer ref="jsonToMapTransformer" />
<http:outbound-gateway expected-response-type="java.lang.String"
url="http://localhost:8080/postService/postupdate"
http-method="POST"
extract-request-payload="true"
request-factory="requestFactory">
<http:request-handler-advice-chain>
<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="onSuccessExpression" value="headers['file_originalFile'].delete()" />
<property name="onFailureExpression"
value="headers['file_originalFile'].renameTo(new java.io.File(headers['file-failed-path']+headers['file-name']))"/>
</bean>
</http:request-handler-advice-chain>
</http:outbound-gateway>
Upvotes: 1
Reputation: 174554
Add an ExpressionEvaluatingRequestHandlerAdvice
to the outbound gateway to remove the file. See the Expression Evaluating Advice Demo in the retry-and-more sample for an example - it removes or renames the file, depending on success or failure.
Upvotes: 1