Skip to content

Commit 251d02f

Browse files
committed
avoid repeating connection details
1 parent 2895c9b commit 251d02f

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

src/main/kotlin/com/emilburzo/stirimm/stirimmwebapp/service/NewsChangeListener.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package com.emilburzo.stirimm.stirimmwebapp.service
22

33
import com.emilburzo.stirimm.stirimmwebapp.persistence.NewsRepository
4+
import com.zaxxer.hikari.HikariDataSource
45
import org.postgresql.PGConnection
56
import org.slf4j.LoggerFactory
6-
import org.springframework.beans.factory.annotation.Value
77
import org.springframework.boot.context.event.ApplicationReadyEvent
88
import org.springframework.context.event.EventListener
99
import org.springframework.scheduling.annotation.Scheduled
1010
import org.springframework.stereotype.Component
1111
import java.sql.DriverManager
1212
import java.util.*
13+
import javax.sql.DataSource
1314

1415
@Component
1516
class NewsChangeListener(
1617
private val newsService: NewsService,
1718
private val newsRepository: NewsRepository,
18-
@Value("\${spring.datasource.url}") private val dbUrl: String,
19-
@Value("\${spring.datasource.username}") private val dbUser: String,
20-
@Value("\${spring.datasource.password}") private val dbPassword: String,
19+
private val dataSource: DataSource,
2120
) {
2221

2322
private val logger = LoggerFactory.getLogger(javaClass)
@@ -31,8 +30,9 @@ class NewsChangeListener(
3130
newsService.refresh()
3231
lastKnownMaxId = newsRepository.findMaxId()
3332

34-
if (dbUrl.startsWith("jdbc:postgresql")) {
35-
Thread(::listenLoop, "pg-notify-listener").apply {
33+
val hikari = dataSource as? HikariDataSource
34+
if (hikari != null && hikari.jdbcUrl.startsWith("jdbc:postgresql")) {
35+
Thread({ listenLoop(hikari) }, "pg-notify-listener").apply {
3636
isDaemon = true
3737
start()
3838
}
@@ -41,21 +41,20 @@ class NewsChangeListener(
4141
}
4242
}
4343

44-
private fun listenLoop() {
44+
private fun listenLoop(hikari: HikariDataSource) {
4545
while (true) {
4646
try {
4747
val props = Properties().apply {
48-
setProperty("user", dbUser)
49-
setProperty("password", dbPassword)
48+
setProperty("user", hikari.username)
49+
setProperty("password", hikari.password ?: "")
5050
setProperty("tcpKeepAlive", "true")
5151
}
52-
DriverManager.getConnection(dbUrl, props).use { conn ->
53-
conn.unwrap(PGConnection::class.java)
52+
DriverManager.getConnection(hikari.jdbcUrl, props).use { conn ->
53+
val pgConn = conn.unwrap(PGConnection::class.java)
5454
conn.createStatement().execute("LISTEN news_changed")
5555
logger.info("Listening for news_changed notifications")
5656

5757
while (true) {
58-
val pgConn = conn.unwrap(PGConnection::class.java)
5958
val notifications = pgConn.getNotifications(30_000)
6059
if (notifications != null && notifications.isNotEmpty()) {
6160
logger.info("Received news_changed notification, refreshing cache")

0 commit comments

Comments
 (0)