Last active
January 6, 2018 01:42
-
-
Save joshlong/0d1082c0b4c84cad65982fbe89db71ea to your computer and use it in GitHub Desktop.
Dynamic registration of Spring Integration adapters using functional bean definition with the Spring Framework 5.0 Kotlin DSL.
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
package com.example.feed | |
import com.rometools.rome.feed.synd.SyndEntry | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.boot.builder.SpringApplicationBuilder | |
import org.springframework.context.support.beans | |
import org.springframework.core.io.UrlResource | |
import org.springframework.integration.dsl.IntegrationFlows | |
import org.springframework.integration.feed.dsl.Feed | |
import org.springframework.integration.handler.GenericHandler | |
// add org.springframework.boot:spring-boot-starter-integration and | |
// org.springframework.integration:spring-integration-feed to your CLASSPATH | |
// for this to work. | |
@SpringBootApplication | |
class FeedApplication | |
fun main(args: Array<String>) { | |
fun processSyndEntry(syndEntry: SyndEntry) { | |
// todo | |
} | |
SpringApplicationBuilder() | |
.initializers( | |
beans { | |
val feeds = mapOf("atom" to listOf("https://spring.io/blog.atom"), | |
"rss" to listOf("https://cloudfoundry.org/feed/")) | |
feeds.entries.forEach { e -> | |
e.value.forEach { url -> | |
val urlAsKey = url.filter { !it.isLetterOrDigit() } | |
bean(name = "feedFlorFor${urlAsKey}") { | |
IntegrationFlows | |
.from(Feed.inboundAdapter(UrlResource(url), urlAsKey), { it.poller({ it.fixedDelay(1000) }) }) | |
.handle(GenericHandler<SyndEntry> { syndEntry, headers -> | |
processSyndEntry(syndEntry) | |
}) | |
.get() | |
} | |
} | |
} | |
} | |
) | |
.sources(FeedApplication::class.java) | |
.run(*args) | |
} |
I've done an Small PR that will make even more beautiful that code! Just a couple of Kotlin extensions functions!
spring-projects/spring-boot#11527
It's my first PR ! so excited!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a lot going on here. This uses Spring Boot 2.x which in turn uses Spring Framework 5.0 which in turn supports functional (programmatic) bean definition. There's also a nice Kotlin DSL in Spring Framework 5.0, so this example uses the Kotlin language. Finally, this example dynamically registers Spring Integration inbound adapters with functional bean definitions.