当我使用file:inbound-channel-adapter轮询文件时,如何将文件名添加到Message标头?

时间:2015-03-08 21:59:42

标签: spring-integration

有人请告诉我,当我使用file:inbound-channel-adapter查询文件时,如何将文件名添加到Message标头?

我已尝试轮询文件并按如下方式放入频道。

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

然后我有一个服务激活器

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

我可以使用以下配置添加标头。但有些人如何在每次投票中获得10次同样的文件。

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

- 日志文件

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

1 个答案:

答案 0 :(得分:1)

prevent-duplicates="false"要求您在轮询线程完成之前删除或移动文件。如果您愿意,请将其保留为默认值(true),错误...防止重复。

如果您希望跨系统重新启动进行重复防护,则还需要使用持久文件列表过滤器。

但是,通常情况下,最好删除或重命名已处理的文件。