|
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 | this.taskExecutor = new ThreadPoolTaskExecutor();
|
116 | 118 | this.taskExecutor.setCorePoolSize(10);
|
@@ -261,6 +263,26 @@ void testRetryOnErrorDuringDispatch(boolean transactionsEnabled) throws Interrup
|
261 | 263 | assertThat(payloads).containsExactly("1");
|
262 | 264 | }
|
263 | 265 |
|
| 266 | + @Test |
| 267 | + public void testRenewConnection() throws Exception { |
| 268 | + CountDownLatch latch = new CountDownLatch(2); |
| 269 | + List<Object> payloads = new ArrayList<>(); |
| 270 | + CountDownLatch connectionLatch = new CountDownLatch(2); |
| 271 | + connectionSupplier.onGetConnection = connectionLatch::countDown; |
| 272 | + postgresChannelMessageTableSubscriber.start(); |
| 273 | + postgresSubscribableChannel.subscribe(message -> { |
| 274 | + payloads.add(message.getPayload()); |
| 275 | + latch.countDown(); |
| 276 | + }); |
| 277 | + |
| 278 | + assertThat(connectionLatch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 279 | + |
| 280 | + messageStore.addMessageToGroup(groupId, new GenericMessage<>("1")); |
| 281 | + messageStore.addMessageToGroup(groupId, new GenericMessage<>("2")); |
| 282 | + assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 283 | + assertThat(payloads).containsExactlyInAnyOrder("1", "2"); |
| 284 | + } |
| 285 | + |
264 | 286 | @Configuration
|
265 | 287 | @EnableIntegration
|
266 | 288 | public static class Config {
|
@@ -300,4 +322,21 @@ public JdbcChannelMessageStore jdbcChannelMessageStore(DataSource dataSource) {
|
300 | 322 |
|
301 | 323 | }
|
302 | 324 |
|
| 325 | + private static class ConnectionSupplier implements PgConnectionSupplier { |
| 326 | + |
| 327 | + Runnable onGetConnection; |
| 328 | + |
| 329 | + @Override |
| 330 | + public PgConnection get() throws SQLException { |
| 331 | + var conn = DriverManager.getConnection(POSTGRES_CONTAINER.getJdbcUrl(), |
| 332 | + POSTGRES_CONTAINER.getUsername(), |
| 333 | + POSTGRES_CONTAINER.getPassword()) |
| 334 | + .unwrap(PgConnection.class); |
| 335 | + if (this.onGetConnection != null) { |
| 336 | + this.onGetConnection.run(); |
| 337 | + } |
| 338 | + return conn; |
| 339 | + } |
| 340 | + |
| 341 | + } |
303 | 342 | }
|
0 commit comments