|
17 | 17 | package org.springframework.integration.jdbc.channel; |
18 | 18 |
|
19 | 19 | import java.sql.DriverManager; |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.time.Duration; |
20 | 22 | import java.util.ArrayList; |
21 | 23 | import java.util.List; |
22 | 24 | import java.util.concurrent.CountDownLatch; |
|
62 | 64 | * @author Artem Bilan |
63 | 65 | * @author Igor Lovich |
64 | 66 | * @author Adama Sorho |
| 67 | + * @author Johannes Edmeier |
65 | 68 | * |
66 | 69 | * @since 6.0 |
67 | 70 | */ |
@@ -102,15 +105,14 @@ CREATE FUNCTION INT_CHANNEL_MESSAGE_NOTIFY_FCT() |
102 | 105 |
|
103 | 106 | private String groupId; |
104 | 107 |
|
| 108 | + private ConnectionSupplier connectionSupplier; |
| 109 | + |
105 | 110 | @BeforeEach |
106 | 111 | void setUp(TestInfo testInfo) { |
107 | 112 | // Not initiated as a bean to allow for registrations prior and post the life cycle |
108 | | - this.postgresChannelMessageTableSubscriber = |
109 | | - new PostgresChannelMessageTableSubscriber(() -> |
110 | | - DriverManager.getConnection(POSTGRES_CONTAINER.getJdbcUrl(), |
111 | | - POSTGRES_CONTAINER.getUsername(), |
112 | | - POSTGRES_CONTAINER.getPassword()) |
113 | | - .unwrap(PgConnection.class)); |
| 113 | + this.connectionSupplier = new ConnectionSupplier(); |
| 114 | + this.postgresChannelMessageTableSubscriber = new PostgresChannelMessageTableSubscriber(connectionSupplier); |
| 115 | + this.postgresChannelMessageTableSubscriber.setNotificationTimeout(Duration.ofSeconds(5)); |
114 | 116 |
|
115 | 117 |
|
116 | 118 | this.taskExecutor = new ThreadPoolTaskExecutor(); |
@@ -263,6 +265,26 @@ void testRetryOnErrorDuringDispatch(boolean transactionsEnabled) throws Interrup |
263 | 265 | assertThat(payloads).containsExactly("1"); |
264 | 266 | } |
265 | 267 |
|
| 268 | + @Test |
| 269 | + public void testRenewConnection() throws Exception { |
| 270 | + CountDownLatch latch = new CountDownLatch(2); |
| 271 | + List<Object> payloads = new ArrayList<>(); |
| 272 | + CountDownLatch connectionLatch = new CountDownLatch(2); |
| 273 | + connectionSupplier.onGetConnection = connectionLatch::countDown; |
| 274 | + postgresChannelMessageTableSubscriber.start(); |
| 275 | + postgresSubscribableChannel.subscribe(message -> { |
| 276 | + payloads.add(message.getPayload()); |
| 277 | + latch.countDown(); |
| 278 | + }); |
| 279 | + |
| 280 | + assertThat(connectionLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 281 | + |
| 282 | + messageStore.addMessageToGroup(groupId, new GenericMessage<>("1")); |
| 283 | + messageStore.addMessageToGroup(groupId, new GenericMessage<>("2")); |
| 284 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 285 | + assertThat(payloads).containsExactlyInAnyOrder("1", "2"); |
| 286 | + } |
| 287 | + |
266 | 288 | @Configuration |
267 | 289 | @EnableIntegration |
268 | 290 | public static class Config { |
@@ -302,4 +324,21 @@ public JdbcChannelMessageStore jdbcChannelMessageStore(DataSource dataSource) { |
302 | 324 |
|
303 | 325 | } |
304 | 326 |
|
| 327 | + private static class ConnectionSupplier implements PgConnectionSupplier { |
| 328 | + |
| 329 | + Runnable onGetConnection; |
| 330 | + |
| 331 | + @Override |
| 332 | + public PgConnection get() throws SQLException { |
| 333 | + var conn = DriverManager.getConnection(POSTGRES_CONTAINER.getJdbcUrl(), |
| 334 | + POSTGRES_CONTAINER.getUsername(), |
| 335 | + POSTGRES_CONTAINER.getPassword()) |
| 336 | + .unwrap(PgConnection.class); |
| 337 | + if (this.onGetConnection != null) { |
| 338 | + this.onGetConnection.run(); |
| 339 | + } |
| 340 | + return conn; |
| 341 | + } |
| 342 | + |
| 343 | + } |
305 | 344 | } |
0 commit comments