Skip to content

Commit df67c59

Browse files
authored
GH-8732 Don't remove JDBC message if other groups (#8733)
* 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`** * * 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
1 parent 73ed3ee commit df67c59

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

spring-integration-core/src/main/java/org/springframework/integration/store/MessageStore.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,11 +62,12 @@ public interface MessageStore {
6262
<T> Message<T> addMessage(Message<T> message);
6363

6464
/**
65-
* Remove the Message with the given id from the MessageStore, if present, and return it. If no Message with that id
66-
* is present in the store, this will return <i>null</i>.
67-
*
68-
* @param id THe message identifier.
69-
* @return The message.
65+
* Remove the Message with the given id from the MessageStore, if present, and return it.
66+
* If no Message with that id is present in the store, this will return {@code null}.
67+
* If this method is implemented on a {@link MessageGroupStore},
68+
* the message is removed from the store only if no groups holding this message.
69+
* @param id the message identifier.
70+
* @return the message (if any).
7071
*/
7172
Message<?> removeMessage(UUID id);
7273

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ SELECT COUNT(MESSAGE_ID)
172172
DELETE_MESSAGE("""
173173
DELETE from %PREFIX%MESSAGE
174174
where MESSAGE_ID=? and REGION=?
175+
and MESSAGE_ID not in (
176+
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE
177+
where MESSAGE_ID=? and REGION = ?)
175178
"""),
176179

177180
CREATE_MESSAGE("""
@@ -199,7 +202,12 @@ SELECT COUNT(GROUP_KEY)
199202

200203
DELETE_MESSAGES_FROM_GROUP("""
201204
DELETE from %PREFIX%MESSAGE
202-
where MESSAGE_ID in (SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE where GROUP_KEY = ? and REGION = ?)
205+
where MESSAGE_ID in (
206+
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE where GROUP_KEY = ? and REGION = ?
207+
and MESSAGE_ID not in (
208+
SELECT MESSAGE_ID from %PREFIX%GROUP_TO_MESSAGE
209+
where GROUP_KEY != ? and REGION = ?)
210+
)
203211
and REGION = ?
204212
"""),
205213

@@ -384,7 +392,8 @@ public Message<?> removeMessage(UUID id) {
384392
if (message == null) {
385393
return null;
386394
}
387-
int updated = this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGE), getKey(id), this.region);
395+
String key = getKey(id);
396+
int updated = this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGE), key, this.region, key, this.region);
388397
if (updated != 0) {
389398
return message;
390399
}
@@ -575,15 +584,18 @@ public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messa
575584
(ps, messageToRemove) -> {
576585
ps.setString(1, groupKey); // NOSONAR - magic number
577586
ps.setString(2, getKey(messageToRemove.getHeaders().getId())); // NOSONAR - magic number
578-
ps.setString(3, JdbcMessageStore.this.region); // NOSONAR - magic number
587+
ps.setString(3, this.region); // NOSONAR - magic number
579588
});
580589

581590
this.jdbcTemplate.batchUpdate(getQuery(Query.DELETE_MESSAGE),
582591
messages,
583592
getRemoveBatchSize(),
584593
(ps, messageToRemove) -> {
585-
ps.setString(1, getKey(messageToRemove.getHeaders().getId())); // NOSONAR - magic number
586-
ps.setString(2, JdbcMessageStore.this.region); // NOSONAR - magic number
594+
String key = getKey(messageToRemove.getHeaders().getId());
595+
ps.setString(1, key); // NOSONAR - magic number
596+
ps.setString(2, this.region); // NOSONAR - magic number
597+
ps.setString(3, key); // NOSONAR - magic number
598+
ps.setString(4, this.region); // NOSONAR - magic number
587599
});
588600

589601
updateMessageGroup(groupKey);
@@ -593,7 +605,8 @@ public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messa
593605
public void removeMessageGroup(Object groupId) {
594606
String groupKey = getKey(groupId);
595607

596-
this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGES_FROM_GROUP), groupKey, this.region, this.region);
608+
this.jdbcTemplate.update(getQuery(Query.DELETE_MESSAGES_FROM_GROUP),
609+
groupKey, this.region, groupKey, this.region, this.region);
597610

598611
if (logger.isDebugEnabled()) {
599612
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,36 @@ 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+
516+
@Test
517+
public void removeMessagesFromGroupDontRemoveSameMessageInOtherGroup() {
518+
GenericMessage<String> testMessage = new GenericMessage<>("test data");
519+
520+
messageStore.addMessageToGroup("1", testMessage);
521+
messageStore.addMessageToGroup("2", testMessage);
522+
523+
messageStore.removeMessagesFromGroup("1", testMessage);
524+
525+
assertThat(messageStore.getMessageCount()).isEqualTo(1);
526+
assertThat(messageStore.messageGroupSize("1")).isEqualTo(0);
527+
assertThat(messageStore.messageGroupSize("2")).isEqualTo(1);
528+
}
529+
500530
@Configuration
501531
public static class Config {
502532

0 commit comments

Comments
 (0)