Skip to content

Commit 94e1ff5

Browse files
committed
GH-8732 Don't remove JDBC message if other groups
Fixes #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`**
1 parent 7d4e7e9 commit 94e1ff5

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcMessageStore.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,12 @@ SELECT COUNT(GROUP_KEY)
199199

200200
DELETE_MESSAGES_FROM_GROUP("""
201201
DELETE from %PREFIX%MESSAGE
202-
where MESSAGE_ID in (SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE where GROUP_KEY = ? and REGION = ?)
202+
where MESSAGE_ID in (
203+
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE where GROUP_KEY = ? and REGION = ?
204+
and MESSAGE_ID not in (
205+
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE
206+
where GROUP_KEY != ? and REGION = ?)
207+
)
203208
and REGION = ?
204209
"""),
205210

@@ -593,7 +598,8 @@ public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messa
593598
public void removeMessageGroup(Object groupId) {
594599
String groupKey = getKey(groupId);
595600

596-
this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGES_FROM_GROUP), groupKey, this.region, this.region);
601+
this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGES_FROM_GROUP),
602+
groupKey, this.region, groupKey, this.region, this.region);
597603

598604
if (logger.isDebugEnabled()) {
599605
logger.debug("Removing relationships for the group with group key=" + groupKey);

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/mysql/MySqlJdbcMessageStoreTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,22 @@ public void testMessageGroupCondition() {
497497
assertThat(this.messageStore.getMessageGroup(groupId).getCondition()).isEqualTo("testCondition");
498498
}
499499

500+
@Test
501+
public void sameMessageInTwoGroupsNotRemovedByFirstGroup() {
502+
GenericMessage<String> testMessage = new GenericMessage<>("test data");
503+
504+
messageStore.addMessageToGroup("1", testMessage);
505+
messageStore.addMessageToGroup("2", testMessage);
506+
507+
messageStore.removeMessageGroup("1");
508+
509+
assertThat(messageStore.getMessageCount()).isEqualTo(1);
510+
511+
messageStore.removeMessageGroup("2");
512+
513+
assertThat(messageStore.getMessageCount()).isEqualTo(0);
514+
}
515+
500516
@Configuration
501517
public static class Config {
502518

0 commit comments

Comments
 (0)