Skip to content

Instantly share code, notes, and snippets.

@joshlong
Last active January 6, 2018 01:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlong/0d1082c0b4c84cad65982fbe89db71ea to your computer and use it in GitHub Desktop.
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.
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)
}
@joshlong
Copy link
Author

joshlong commented Jan 2, 2018

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.

@juan-medina
Copy link

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