Created
July 14, 2022 21:33
-
-
Save tgrushka/82b1071ec6d94cba3c281ee97ee78c37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.context.annotation.Bean | |
import org.springframework.context.annotation.Configuration | |
import org.springframework.core.io.ClassPathResource | |
import org.springframework.integration.config.EnableIntegration | |
import org.springframework.integration.dsl.integrationFlow | |
import org.springframework.integration.file.FileHeaders | |
import org.springframework.integration.file.dsl.Files | |
import org.springframework.integration.file.filters.ChainFileListFilter | |
import org.springframework.integration.file.filters.FileListFilter | |
import org.springframework.integration.file.filters.SimplePatternFileListFilter | |
import org.springframework.integration.file.transformer.FileToStringTransformer | |
import java.io.File | |
class ChangedFileFilter : FileListFilter<File> { | |
private val seen = mutableMapOf<File, Long>() | |
override fun filterFiles(files: Array<out File>?): MutableList<File> { | |
println(seen) | |
return files?.filterNot { file -> | |
val isSeen = seen[file] == file.lastModified() | |
if (!isSeen) seen[file] = file.lastModified() | |
isSeen | |
}?.toMutableList() ?: mutableListOf() | |
} | |
} | |
@Configuration | |
@EnableIntegration | |
class ApplicationConfig { | |
@Bean | |
fun processFileFlow() = integrationFlow( | |
Files.inboundAdapter(ClassPathResource("manifest.json").file.parentFile) | |
.filter( | |
ChainFileListFilter<File>() | |
.addFilter(SimplePatternFileListFilter("*.json")) | |
.addFilter(ChangedFileFilter()), | |
), | |
{ poller { it.fixedRate(1000) } } | |
) { | |
transform(FileToStringTransformer()) | |
handle { msg -> | |
val filename = msg.headers[FileHeaders.FILENAME] as String | |
val content: String = msg.payload.toString() | |
println("FILE CONTENTS") | |
println(filename) | |
println(content) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Polls a file (files) in the Spring resource directory for changes at given interval and emits file contents when changed.