Sudarsana Kasireddy
Sudarsana Kasireddy

Reputation: 960

how to add file name to Message header when I poll a file using file:inbound-channel-adapter?

Can someone pls let me know, how to add file name to Message header when i poll a file using file:inbound-channel-adapter?

I have tried to poll files and drop in a channel as follows.

<int-file:inbound-channel-adapter
    directory="C://inbound" channel="filesChannel" prevent-duplicates="false"
    filename-pattern="*.*">
    <int:poller id="poll" fixed-delay="30000"
        max-messages-per-poll="100" />
</int-file:inbound-channel-adapter>

then I have a service activator

<int:service-activator ref="fileService" input-channel="filesChannel" />



@ServiceActivator
    public void service(Message<File> message) throws IOException {

log.info(message.getHeaders());

        log.info(message.getPayload());     
        Path source = Paths.get(message.getPayload().getAbsolutePath());
        Path destination = Paths.get("C:\\inbound\\t\\"+message.getPayload().getName());

    //  Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
    }

I'm able to add headers with the following configuration. But some how i'm getting same file 10 times per poll.

<int:channel id="filesChannel" />
<int:channel id="filesAddedHeaderChannel" />

<int-file:inbound-channel-adapter
    directory="C://inbound" channel="filesChannel" prevent-duplicates="false"
    filename-pattern="*.*">
    <int:poller id="poll" fixed-delay="30000"
        max-messages-per-poll="10" />
</int-file:inbound-channel-adapter>

<int:header-enricher input-channel="filesChannel" output-channel="filesAddedHeaderChannel">
    <int:header name="file-name"  expression="payload.name"/>
</int:header-enricher>

<int:bridge input-channel="filesChannel" output-channel="filesAddedHeaderChannel"/>

<int:service-activator ref="fileService" input-channel="filesAddedHeaderChannel" />

--log file

2015-03-08T19:24:18,663 [task-scheduler-1]    [INFO ] FileService(25) [] - {file-name=tml_internalAps.csv, id=1b9bcd2d-d01c-539b-8eee-c12ba0fcc2b0, timestamp=1425857058650}
2015-03-08T19:24:18,667 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:tml_internalAps.csv
2015-03-08T19:24:18,668 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,671 [task-scheduler-1]    [INFO ] FileService(25) [] - {id=a56980e9-f111-5dbc-6fe8-889c748f04b6, timestamp=1425857058670}
2015-03-08T19:24:18,672 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:null
2015-03-08T19:24:18,672 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,674 [task-scheduler-1]    [INFO ] FileService(25) [] - {file-name=tml_internalAps.csv, id=b53afaa9-9eeb-9795-451e-6898f56a0db7, timestamp=1425857058674}
2015-03-08T19:24:18,675 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:tml_internalAps.csv
2015-03-08T19:24:18,675 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,677 [task-scheduler-1]    [INFO ] FileService(25) [] - {id=7bc88312-2f1b-c4bf-65ed-fe3053b95be7, timestamp=1425857058676}
2015-03-08T19:24:18,677 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:null
2015-03-08T19:24:18,678 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,680 [task-scheduler-1]    [INFO ] FileService(25) [] - {file-name=tml_internalAps.csv, id=c96bc18b-ff52-a96e-7f56-6d72e7e28a4c, timestamp=1425857058679}
2015-03-08T19:24:18,681 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:tml_internalAps.csv
2015-03-08T19:24:18,681 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,683 [task-scheduler-1]    [INFO ] FileService(25) [] - {id=e11e5fe5-f7ab-9721-0f36-11d96f1d4887, timestamp=1425857058683}
2015-03-08T19:24:18,684 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:null
2015-03-08T19:24:18,685 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,686 [task-scheduler-1]    [INFO ] FileService(25) [] - {file-name=tml_internalAps.csv, id=b2a0f757-e4a6-55b8-728a-e8bad8ba25a3, timestamp=1425857058686}
2015-03-08T19:24:18,687 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:tml_internalAps.csv
2015-03-08T19:24:18,687 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,689 [task-scheduler-1]    [INFO ] FileService(25) [] - {id=93c60c2b-ed88-5b79-d163-b4cf923c2e73, timestamp=1425857058688}
2015-03-08T19:24:18,690 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:null
2015-03-08T19:24:18,690 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,692 [task-scheduler-1]    [INFO ] FileService(25) [] - {file-name=tml_internalAps.csv, id=9022ad48-26cb-1e1c-2383-370e556c85a1, timestamp=1425857058691}
2015-03-08T19:24:18,692 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:tml_internalAps.csv
2015-03-08T19:24:18,693 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv
2015-03-08T19:24:18,694 [task-scheduler-1]    [INFO ] FileService(25) [] - {id=37ee06bc-f093-be7c-a292-743626fa3d9f, timestamp=1425857058694}
2015-03-08T19:24:18,695 [task-scheduler-1]    [INFO ] FileService(27) [] - file name via header:null
2015-03-08T19:24:18,696 [task-scheduler-1]    [INFO ] FileService(29) [] - C:\inbound\tml_internalAps.csv

Upvotes: 1

Views: 1481

Answers (1)

Gary Russell
Gary Russell

Reputation: 174664

prevent-duplicates="false" requires you to delete or move the file before the poller thread completes. Leave it as the default (true) if you wish to, err... prevent duplicates.

If you wish duplicate prevention across system restarts you also need to use a persistent file list filter.

But, generally, it's better to remove or rename processed files.

Upvotes: 1

Related Questions