GH-8732 Don't remove JDBC message if other groups (#8733)

* GH-8732 Don't remove JDBC message if other groups

Fixes https://github.com/spring-projects/spring-integration/issues/8732

When same message is added into different groups,
its record in the `INT_MESSAGE` must remain until the last group is removed

* Improve `JdbcMessageStore.DELETE_MESSAGES_FROM_GROUP` SQL
to ignore those messages for removal which has other group records in the `INT_GROUP_TO_MESSAGE` table

**Cherry-pick to `6.1.x`, `6.0.x` & `5.5.x`**

* * Fix `JdbcMessageStore.removeMessage()` and `removeMessagesFromGroup()`
to remove from `INT_MESSAGE` only if there is no other records in `INT_GROUP_TO_MESSAGE`

* * Improve `MessageStore.removeMessage()` Javadoc
mentioning `MessageGroupStore` specifics
This commit is contained in:
Artem Bilan
2023-09-14 12:58:07 -04:00
committed by GitHub
parent 73ed3eeebd
commit df67c59f8e
3 changed files with 56 additions and 12 deletions

View File

@@ -172,6 +172,9 @@ public class JdbcMessageStore extends AbstractMessageGroupStore
DELETE_MESSAGE("""
DELETE from %PREFIX%MESSAGE
where MESSAGE_ID=? and REGION=?
and MESSAGE_ID not in (
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE
where MESSAGE_ID=? and REGION = ?)
"""),
CREATE_MESSAGE("""
@@ -199,7 +202,12 @@ public class JdbcMessageStore extends AbstractMessageGroupStore
DELETE_MESSAGES_FROM_GROUP("""
DELETE from %PREFIX%MESSAGE
where MESSAGE_ID in (SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE where GROUP_KEY = ? and REGION = ?)
where MESSAGE_ID in (
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE where GROUP_KEY = ? and REGION = ?
and MESSAGE_ID not in (
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE
where GROUP_KEY != ? and REGION = ?)
)
and REGION = ?
"""),
@@ -384,7 +392,8 @@ public class JdbcMessageStore extends AbstractMessageGroupStore
if (message == null) {
return null;
}
int updated = this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGE), getKey(id), this.region);
String key = getKey(id);
int updated = this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGE), key, this.region, key, this.region);
if (updated != 0) {
return message;
}
@@ -575,15 +584,18 @@ public class JdbcMessageStore extends AbstractMessageGroupStore
(ps, messageToRemove) -> {
ps.setString(1, groupKey); // NOSONAR - magic number
ps.setString(2, getKey(messageToRemove.getHeaders().getId())); // NOSONAR - magic number
ps.setString(3, JdbcMessageStore.this.region); // NOSONAR - magic number
ps.setString(3, this.region); // NOSONAR - magic number
});
this.jdbcTemplate.batchUpdate(getQuery(Query.DELETE_MESSAGE),
messages,
getRemoveBatchSize(),
(ps, messageToRemove) -> {
ps.setString(1, getKey(messageToRemove.getHeaders().getId())); // NOSONAR - magic number
ps.setString(2, JdbcMessageStore.this.region); // NOSONAR - magic number
String key = getKey(messageToRemove.getHeaders().getId());
ps.setString(1, key); // NOSONAR - magic number
ps.setString(2, this.region); // NOSONAR - magic number
ps.setString(3, key); // NOSONAR - magic number
ps.setString(4, this.region); // NOSONAR - magic number
});
updateMessageGroup(groupKey);
@@ -593,7 +605,8 @@ public class JdbcMessageStore extends AbstractMessageGroupStore
public void removeMessageGroup(Object groupId) {
String groupKey = getKey(groupId);
this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGES_FROM_GROUP), groupKey, this.region, this.region);
this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGES_FROM_GROUP),
groupKey, this.region, groupKey, this.region, this.region);
if (logger.isDebugEnabled()) {
logger.debug("Removing relationships for the group with group key=" + groupKey);