Skip to content

Commit 145edb2

Browse files
raphwartembilan
authored andcommitted
GH-3872: Docs for PostgresSubscribableChannel
Fixes #3872 * Add documentation for `PostgresSubscribableChannel` and related components * Add links, example and what's new section. * Docs clean up
1 parent 14d8597 commit 145edb2

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/reference/asciidoc/jdbc.adoc

+53
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ Instead, if possible, consider using either JMS- or AMQP-backed channels instead
480480
For further reference, see the following resource:
481481
482482
* https://mikehadlow.blogspot.com/2012/04/database-as-queue-anti-pattern.html[The Database As Queue Anti-Pattern].
483+
484+
If you are still planning to use your database as a queue, consider using PostgreSQL and its notification mechanism which is described <<postgresql-push,in a subsequent section>>.
483485
====
484486

485487
===== Concurrent Polling
@@ -575,6 +577,57 @@ The message data for a persistent channel is keyed in the store on the channel n
575577
Consequently, if the channel names are not globally unique, the channels can pick up data that is not intended for them.
576578
To avoid this danger, you can use the message store `region` to keep data separate for different physical channels that have the same logical name.
577579

580+
581+
[[postgresql-push]]
582+
==== PostgreSQL: Receiving Push Notifications
583+
584+
PostgreSQL offers a listen and notification framework for receiving push notifications upon database table manipulations.
585+
Spring Integration leverages this mechanism (starting with version 6.0) to allow for receiving push notifications when new messages are added to a `JdbcChannelMessageStore`.
586+
When using this feature, a database trigger must be defined, which can be found as part of the comments of the `schema-postgresql.sql` file which is included in the JDBC module of Spring Integration.
587+
588+
Push notifications are received via the `PostgresChannelMessageTableSubscriber` class which allows its subscribers to receive a callback upon the arrival of new messages for any given `region` and `groupId`.
589+
These notifications are received even if a message was appended on a different JVM, but to the same database.
590+
The `PostgresSubscribableChannel` implementation uses a `PostgresChannelMessageTableSubscriber.Subscription` contract to pull messages from the store as a reaction for notification from the mentioned `PostgresChannelMessageTableSubscriber` notifications.
591+
592+
For example, push notifications for `some group` can be received as follows:
593+
594+
====
595+
[source,java]
596+
----
597+
@Bean
598+
public JdbcChannelMessageStore messageStore(DataSource dataSource) {
599+
JdbcChannelMessageStore messageStore = new JdbcChannelMessageStore(dataSource);
600+
messageStore.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider());
601+
return messageStore;
602+
}
603+
604+
@Bean
605+
public PostgresChannelMessageTableSubscriber subscriber(
606+
@Value("${spring.datasource.url}") String url,
607+
@Value("${spring.datasource.username}") String username,
608+
@Value("${spring.datasource.password}") String password) {
609+
return new PostgresChannelMessageTableSubscriber(() ->
610+
DriverManager.getConnection(url, username, password).unwrap(PgConnection.class));
611+
}
612+
613+
@Bean
614+
public PostgresSubscribableChannel channel(
615+
PostgresChannelMessageTableSubscriber subscriber,
616+
JdbcChannelMessageStore messageStore) {
617+
return new PostgresSubscribableChannel(messageStore, "some group", subscriber);
618+
}
619+
----
620+
====
621+
622+
[IMPORTANT]
623+
====
624+
Any active `PostgresChannelMessageTableSubscriber` occupies an exclusive JDBC `Connection` for the duration of its active life cycle.
625+
It is therefore important that this connection does not originate from a pooling `DataSource`.
626+
Such connection pools do normally expect that issued connections are closed within a predefined timeout window.
627+
628+
For this need of an exclusive connection, it is also recommended that a JVM only runs a single `PostgresChannelMessageTableSubscriber` which can be used to register any number of subscriptions.
629+
====
630+
578631
[[stored-procedures]]
579632
=== Stored Procedures
580633

src/reference/asciidoc/whats-new.adoc

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,34 @@ In general the project has been moved to Java 17 baseline and migrated from Java
1717
[[x6.0-new-components]]
1818
=== New Components
1919

20+
[[x6.0-mqtt]]
21+
==== MQTT ClientManager
22+
2023
A new MQTT `ClientManager` has been added to support a reusable MQTT connection across different channel adapters.
2124
See <<./mqtt.adoc#mqtt-shared-client,Shared MQTT Client Support>> for more information.
2225

2326
[[x6.0-graphql]]
24-
=== GraphQL Support
27+
==== GraphQL Support
2528

2629
The GraphQL support has been added.
2730
See <<./graphql.adoc#graphql,GraphQL Support>> for more information.
2831

2932
[[x6.0-smb]]
30-
=== SMB Support
33+
==== SMB Support
3134

3235
SMB support has been added from the Spring Integration Extensions project.
3336
The Java DSL (see `org.springframework.integration.smb.dsl.Smb` factory) also has been added to this module.
3437
An `SmbStreamingMessageSource` and `SmbOutboundGateway` implementation are introduced.
3538
See <<./smb.adoc#smb,SMB Support>> for more information.
3639

40+
[[x6.0-jdbc]]
41+
==== PostgreSQL Push Notification
42+
43+
A `PostgresSubscribableChannel` allows to receive push notifications via `PostgresChannelMessageTableSubscriber` upon new messages add to the `JdbcChannelMessageStore`.
44+
45+
See <<./jdbc.adoc#postgresql-push,PostgreSQL: Receiving Push Notifications>> for more information.
46+
47+
3748
[[x6.0-general]]
3849
=== General Changes
3950

0 commit comments

Comments
 (0)