How to receive details of file on ftp server using spring-integration? - ftp

I've been searching for answer but I didn't find any. How can I receive information about, for example date of creation file stored on ftp server using spring-integration? Is any chance to do this using inbound-channel-adapter or something like that?
Thanks for advance.
#EDIT:
Configuration:
<int:splitter id="splitter" input-channel="toSplitter" output-channel="ftpChannel"/>
<int-ftp:outbound-gateway id="gatewayLS"
session-factory="ftpClientFactory"
request-channel="inbound"
command="ls"
expression="payload"
reply-channel="toSplitter"/>
<int:channel id="toSplitter">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<bean id="ftpClientFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="${host}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<property name="clientMode" value="0"/>
<property name="fileType" value="2"/>
<property name="bufferSize" value="10000000"/>
</bean>
<int:channel id="ftpChannel"/>
#EDIT:
Problem is solved thanks to Gary.

Use the ls command of the outbound gateway.
As stated there: "The message payload resulting from an ls operation is a list of file names, or a list of FileInfo objects. These objects provide information such as modified time, permissions etc."
Also, see the FTP Sample App.

Related

How to set proxy in HttpComponentsMessageSender?

I am working behind a firewall,where i am trying to access a soap web service.
i went over through the internet,but couldn't find anything for setting proxy in .
I am calling the webservice using spring-integration.
Spring-integration.xml
<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<property name="connectionTimeout" value="10000"/>
<property name="readTimeout" value="10000"/>
</bean>
<bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
</property>
</bean>
<int:gateway id="requestGateway" service-interface="main.java.com.as.poller.RequestGateway"
default-request-channel="requestchannel" default-reply-timeout="20000">
<int:method name="pushNotification" />
</int:gateway>
<int-ws:outbound-gateway id="pointbalance"
uri="url"
marshaller="marshaller" unmarshaller="marshaller"
request-channel="pointbalancechannel" message-sender="messageSender"
message-factory="soapMessageFactory">
<int-ws:request-handler-advice-chain>
<int:retry-advice max-attempts="${retry_limit}">
</int:retry-advice>
<bean class="main.java.com.as.poller.RetryAdvice" />
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
I could find only examples using CommonsHttpMessageSender.But this is deprecated.Can anyone please help me on setting the proxy for HttpComponentsMessageSender for my xml configuration...
I did it using org.springframework.ws.transport.http.CommonsHttpMessageSender from a superb article https://onebyteatatime.wordpress.com/2009/04/08/spring-web-service-call-using-proxy-per-connection/ . But still i dont see any thing regarding HttpComponentsMessageSender.

DefaultMessageListenerContainer recoveryInterval with specific retry count

We are using Spring Integration in our project and we have a requirement where If IBM MQ goes down then we will have to auto connect to IBM MQ when it is up. We have done this implementation using recoveryInterval option of org.springframework.jms.listener.DefaultMessageListenerContainer class. We have given recovery interval as 6 seconds so every 6 seconds system try to recover the MQ connection but now we have a requirement where we will have to do the autorecover twice only and after that if still MQ is down then stop the inbound adapter.
Is there any way in Spring Integration to mention the auto recovery retry count so that system will try to recover only for that retry count?
Below is my existing configuration.
<bean id="inQ" class="com.ibm.mq.jms.MQQueue">
<constructor-arg value="${mq.inbound.queue}" />
</bean>
<int:channel id="inbound" />
<int-jms:message-driven-channel-adapter
id="jmsIn" channel="inbound" container="messageListenerContainer"
acknowledge="transacted" auto-startup="false">
</int-jms:message-driven-channel-adapter>
<int:service-activator id="mainService"
input-channel="inbound" ref="messageListener" method="onMessage">
<int:request-handler-advice-chain>
<ref bean="retryWithBackoffAdviceSession" />
</int:request-handler-advice-chain>
</int:service-activator>
<bean id="messageListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="mqConnectionFactory" />
<property name="destination" ref="inQ" />
<property name="sessionTransacted" value="true" />
<property name="maxConcurrentConsumers" value="${maxConcurrentConsumers}" />
<property name="concurrentConsumers" value="${concurrentConsumers}" />
<property name="receiveTimeout" value="${receiveTimeout}" />
<property name="recoveryInterval" value="60000" />
<property name="autoStartup" value="${autoStartup}" />
</bean>
Thanks
Sach
As an alternative to the recoveryInterval, you can now specify a Backoff instead (see the docs).
It doesn't provide a mechanism to stop the container but an appropriate backoff can effectively do what you want.
You would then need to programmatically stop/start to kick it off again.

How to receive list of directories or file from ftp server with int-ftp:outbound-gateway?

I defined ftp adapter to connect to ftp server, but I see ftp server log and don't see request sent to ftp server. my adapter code is :
<bean id="ftpClientFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="127.0.0.1"/>
<property name="port" value="21"/>
<property name="username" value="banks_reader"/>
<property name="password" value="123456"/>
<property name="clientMode" value="2"/>
<property name="fileType" value="2"/>
</bean>
<int:channel id="inbound1">
<int:queue/>
</int:channel>
<int:channel id="outbound"/>
<int-ftp:outbound-gateway id="gateway1"
session-factory="ftpClientFactory"
request-channel="inbound1"
reply-channel="outbound"
reply-timeout="777"
auto-create-local-directory="false"
auto-startup="true"
filename-pattern="*"
remote-file-separator="/"
command="ls"
command-options="-1 -f"
expression="payload"
order="1"
mput-regex=".*">
<int:poller fixed-delay="1000"/>
</int-ftp:outbound-gateway>
which shows:
DEBUG PollingConsumer:208 - Received no Message during the poll, returning 'false' in application log
when change usename and password for test (fake pass and usename) Does not change anything and throw exception
Your config looks good, but you have missed an approach a bit.
<int-ftp:outbound-gateway> is an event-driven request/reply component and it can't do anything with FTP until there is no message in the inbound1.
Even if it is <queue>, the <poller> initiates its work only if there is no that Received no Message during the poll.
Since you use expression="payload", your requestMessage must contain a payload with remote dir from your FTP user home.
So, just send such a message to the inbound1 and let us know how it is!
UPDATE
To perform the LS command on the <int-ftp:outbound-gateway> periodically with the same dir as a payload you have to configure something like this:
<inbound-channel-adapter channel="inbound1" expresssion="'/'">
<poller fixed-delay="10000"/>
</inbound-channel-adapter>
Having that (and your <gateway>) case there is no need to have that inbound1 channel as a <queue>

how to trigger many ftp inbound-channel-adapter from one poller to connect ftp server?

First, thank you for your attention
i defined two ftp inbound-channel-adapter in my spring integration project,the adapters with diferent configuration but have share session factory to connect ftp server, each adapters have one poller to trigger,i want to see the bellow code is correct?
is efficiency?
<bean id="ftpClientFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="127.0.0.1"/>
<property name="port" value="21"/>
<property name="username" value="banks_reader"/>
<property name="password" value="123456"/>
</bean>
<bean id="myFilter" class="com.ali.util.FtpFilter"/>
<bean id="onceFilter" class="com.ali.util.OnceFilter"/>
<int-ftp:inbound-channel-adapter id="ftpInbound1"
channel="inboundFileChannel"
**session-factory="ftpClientFactory"**
charset="UTF-8"
auto-create-local-directory="true"
delete-remote-files="true"
remote-directory="/directoryA"
remote-file-separator="/"
temporary-file-suffix=".writing"
local-filter="myFilter"
filter="onceFilter"
local-directory="file:output">
**<int:poller fixed-rate="5000"/>**
</int-ftp:inbound-channel-adapter>
<int-ftp:inbound-channel-adapter id="ftpInbound2"
channel="inboundFileChannel"
**session-factory="ftpClientFactory"**
charset="UTF-8"
auto-create-local-directory="true"
delete-remote-files="true"
remote-directory="/directoryB"
remote-file-separator="/"
temporary-file-suffix=".writing"
local-filter="myFilter"
filter="onceFilter"
local-directory="file:output">
**<int:poller fixed-rate="5000"/>**
</int-ftp:inbound-channel-adapter>
Is there another way?
That is the correct technique using channel adapters.
Or, you could use an outbound gateway instead, and fetch (ls + get or mget) files from both directories in turn.

Downloading files from FTP Server where directory changed dynamically

I am using Spring Integration to download/upload files from FTP server.
How can I change remote-directory="/directory Name" dynamically in Spring FTP:Inbound-Channel.
My client will create a folder daily basically in "MM-dd-yy" format and copy all files there.
In "FTP:Inbound-channel" I did not find any way to configure this pattern. I basically have
to hardcord the directory or file names in configuration.
What I want is to set the path programatically. Because some times I need to download all files
from a direcotory or download only a specific file.
I found "remote-directory-expression="'directory'+'/'+ new java.text.SimpleDateFormat('dd-MM-yyyy').format(new java.util.Date())" can be set in FTP:Outbound-channel
is there any such attribute in FTP:InBound-channel
My configuration is like this:
<bean id="ftpClientFactory"
class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="${host}" />
<property name="port" value="${availableServerPort}" />
<property name="username" value="${userid}" />
<property name="password" value="${password}" />
</bean>
<int-ftp:inbound-channel-adapter id="ftpInbound"
cache-sessions="false" channel="ftpChannel" session-factory="ftpClientFactory"
filename-pattern="*.txt" auto-create-local-directory="true"
delete-remote-files="false" remote-directory="/filedirectory"
local-directory="${local_directory}">
<int:poller fixed-rate="1000" />
</int-ftp:inbound-channel-adapter>
<int:channel id="ftpChannel">
<int:queue />
</int:channel>
I did not find a way to do all the above items.
Please let me know how can I achieve this.
You can't do it with the inbound adapter, but the <ftp:outbound-gateway/> can be used to achieve what you need; described here.
You can either use ls to list the files, followed a <splitter/> and another gateway using get; or you can use the mget command with a file name pattern in the expression.
The FTP sample has an example of using the gateway

Resources