简体   繁体   中英

MD5 as idempotentKey in Apache Camel

The default behavior is that the absolute file path is used to uniquely identify files - I would like customize this to use the md5 checksum of the file.

Is it possible to simply implement a custom idempotentKey (that doesn't use the 'File Language')?

Camel File2 component supports pluggable expressions. Create bean implementing Expression and pass it to route with idempotentKey=#myExpressionBean .


I don't think, it is good idea to compute md5 hash of file content for every poll, but it is possible.

Expression

@Component
class FileContentMD5Expression implements Expression {
    @Override
    public <T> T evaluate(Exchange exchange, Class<T> type) {
        if (type != String.class){
            throw new IllegalArgumentException("This is String only expression");
        }
        try (FileInputStream fis = new FileInputStream(((File)exchange.getIn(GenericFileMessage.class).getGenericFile().getFile()))) {
            return type.cast(DigestUtils.md5Hex(fis));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Consumer

from("file://somewhere?idempotent=true&idempotentKey=#fileContentMD5Expression")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM