Skip to content

Instantly share code, notes, and snippets.

@tgrushka
Created July 14, 2022 21:33
Show Gist options
  • Save tgrushka/82b1071ec6d94cba3c281ee97ee78c37 to your computer and use it in GitHub Desktop.
Save tgrushka/82b1071ec6d94cba3c281ee97ee78c37 to your computer and use it in GitHub Desktop.
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)
}
}
}
@tgrushka
Copy link
Author

Polls a file (files) in the Spring resource directory for changes at given interval and emits file contents when changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment