From 68c842bdbd627a5bd18f53997bbfea32aa684f6e Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 18 Nov 2021 10:05:50 -0500 Subject: [PATCH] GH-248: Use keySet() for HazelcastMS to list keys Fixes https://github.com/spring-projects/spring-integration-extensions/issues/248 The `HazelcastMessageStore.doListKeys()` uses `this.map.values()` by mistake. * Fix `HazelcastMessageStore.doListKeys()` to use a `keySet()` for a proper filtering and result inferring * Add unit test to cover an iteration functionality for message groups in the store --- .../hazelcast/store/HazelcastMessageStore.java | 10 ++++------ .../store/HazelcastMessageStoreTests.java | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/store/HazelcastMessageStore.java b/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/store/HazelcastMessageStore.java index 408407e..2ff361d 100644 --- a/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/store/HazelcastMessageStore.java +++ b/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/store/HazelcastMessageStore.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2019 the original author or authors. + * Copyright 2017-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +23,8 @@ import org.springframework.util.Assert; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.map.IMap; -import com.hazelcast.query.Predicate; -import com.hazelcast.query.impl.predicates.SqlPredicate; +import com.hazelcast.query.Predicates; +import com.hazelcast.query.QueryConstants; /** * The Hazelcast {@link IMap}-based {@link AbstractKeyValueMessageStore} implementation. @@ -77,9 +77,7 @@ public class HazelcastMessageStore extends AbstractKeyValueMessageStore { protected Collection doListKeys(String keyPattern) { Assert.hasText(keyPattern, "'keyPattern' must not be empty"); keyPattern = keyPattern.replaceAll("\\*", "%"); - @SuppressWarnings("unchecked") - Predicate sqlPredicate = new SqlPredicate("__key like " + keyPattern); - return this.map.values(sqlPredicate); + return this.map.keySet(Predicates.like(QueryConstants.KEY_ATTRIBUTE_NAME.value(), keyPattern)); } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/store/HazelcastMessageStoreTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/store/HazelcastMessageStoreTests.java index 88fb325..7cc308d 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/store/HazelcastMessageStoreTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/store/HazelcastMessageStoreTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2020 the original author or authors. + * Copyright 2017-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -132,4 +132,18 @@ public class HazelcastMessageStoreTests { assertThat(size).isEqualTo(2); } + @Test + public void messageStoreIterator() { + Message message1 = MessageBuilder.withPayload("test").build(); + Message message2 = MessageBuilder.withPayload("test").build(); + store.addMessageToGroup("test", message1); + store.addMessageToGroup("test", message2); + int groupCount = 0; + for (MessageGroup messageGroup : store) { + assertThat(messageGroup.size()).isEqualTo(2); + groupCount++; + } + assertThat(groupCount).isEqualTo(1); + } + }