1
votes

I have a requirement where I want to use ftp and put the file in a dynamic local-directory. I also want to add a age filter. That is not allow the old files through. This works with file properly. I added a custom filter and checked the File objects creation date using this code:

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;
        }

This works fine for int-file adapter. But when I added ftp support, the object I got in the payload was an FTPFile object. This does not give the creationdate but gives the last modified date. The last modified date is not usefull for me. Hence I had to configure the ftp and then chain the file adapter to the directory.

  1. the int-ftp:inbound-channel-adapter picks up a file from the ftp site and puts it in a local directory.

  2. This file is picked up by the int-file:inbound-channel-adapter and put in the final destination.

This is a two step process to pickup a file put in the ftp location.

The filter class works on the int-file:inbound-channel-adapter. This chaining is working.

The problem is in deletion of the original files. The ftp site file(remote-directory) gets deleted automatically. The local-directory file which is picked up by the int-file:inbound-channel-adapter does not get deleted.

This is my configuration.

     <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"/>

The customfilefilter and filenamegenerator beans are also defined.

I have added the header-enricher to add the file_originalFile header as I read somewhere that this header is dropped if the file is used twice/ chained like in my situation. Though the beans method gets called and I have written the following code in it, the file does not get deleted from the file adapters source directory.

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;
}

What am I doing wrong?? I have tried using the header-enricher for abc channel as well with not luck!

Hope the problem is clear now.. Thanks for your help

2
Sorry, it isn't crear yet what's going on. Explain, please, you requirement and which issue you have.Artem Bilan

2 Answers

0
votes

Regarding to:

Cannot convert value of type [org.springframework.integration.endpoint.EventDrivenConsumer] to required type [org.springframework.messaging.MessageChannel] for property 'outputChannel': no matching editors or conversion strategy found

Since you have <int-ftp:outbound-channel-adapter id="ftpOutbound" channel="ftpChannel"

You should you use the channel bean name for the <int:transformer id="testTransformer" input-channel="ftpInbound", rather an id of that adapter <int:transformerd="testTransformer" input-channel="ftpChannel"

0
votes

It's not entirely clear what your question is.

That said, Spring Integration 3.0 introduced the local-filter on the (s)ftp adapters which should allow you to use your custom filter there.

To remove the local file using the header, you can use a request handler advice, see the retry-and-more sample for more information.

Your exception is because you are injecting the outbound adapter as the transformer's output-channel; you should inject ftpChannel instead.