Skip to content

Commit af9e69c

Browse files
artembilangaryrussell
authored andcommitted
Make MessageHistory JSON-serializable
* Add `org.springframework.integration.history` to trusted default packaged of the `JacksonJsonUtils` * Add `@JsonCreator` to `MessageHistory` `private` ctor to let it to be created automatically by Jackson * Add `equals()` and `hashCode()` into `MessageHistory` for the proper `Message` comparison * Add `MessageHistory` into headers for testing with Redis JSON (de)serialization **Cherry-pick to `5.4.x` & `5.3.x`**
1 parent 9c718c3 commit af9e69c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistory.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -23,6 +23,7 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.ListIterator;
26+
import java.util.Objects;
2627
import java.util.Properties;
2728
import java.util.stream.Collectors;
2829

@@ -42,6 +43,8 @@
4243
import org.springframework.messaging.support.GenericMessage;
4344
import org.springframework.util.Assert;
4445

46+
import com.fasterxml.jackson.annotation.JsonCreator;
47+
4548
/**
4649
* @author Mark Fisher
4750
* @author Artem Bilan
@@ -133,7 +136,7 @@ else if (message instanceof AdviceMessage) {
133136
return message;
134137
}
135138

136-
139+
@JsonCreator
137140
private MessageHistory(List<Properties> components) {
138141
Assert.notEmpty(components, "component list must not be empty");
139142
this.components = components;
@@ -205,6 +208,21 @@ public int lastIndexOf(Object o) {
205208
return this.components.lastIndexOf(o);
206209
}
207210

211+
@Override public boolean equals(Object o) {
212+
if (this == o) {
213+
return true;
214+
}
215+
if (!(o instanceof MessageHistory)) {
216+
return false;
217+
}
218+
MessageHistory that = (MessageHistory) o;
219+
return this.components.equals(that.components);
220+
}
221+
222+
@Override public int hashCode() {
223+
return Objects.hash(this.components);
224+
}
225+
208226
@Override
209227
public String toString() {
210228
return this.components

spring-integration-core/src/main/java/org/springframework/integration/support/json/JacksonJsonUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -158,7 +158,8 @@ private static final class AllowlistTypeIdResolver implements TypeIdResolver {
158158
"org.springframework.messaging.support",
159159
"org.springframework.integration.support",
160160
"org.springframework.integration.message",
161-
"org.springframework.integration.store"
161+
"org.springframework.integration.store",
162+
"org.springframework.integration.history"
162163
);
163164

164165
private final TypeIdResolver delegate;

spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2007-2019 the original author or authors.
2+
* Copyright 2007-2021 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.
@@ -40,6 +40,7 @@
4040
import org.springframework.data.redis.core.StringRedisTemplate;
4141
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
4242
import org.springframework.integration.channel.DirectChannel;
43+
import org.springframework.integration.channel.NullChannel;
4344
import org.springframework.integration.channel.QueueChannel;
4445
import org.springframework.integration.history.MessageHistory;
4546
import org.springframework.integration.message.AdviceMessage;
@@ -143,7 +144,7 @@ public void testRemoveMessageGroup() {
143144
store.removeMessageGroup(this.groupId);
144145
MessageGroup messageGroupA = store.getMessageGroup(this.groupId);
145146
assertThat(messageGroupA).isNotSameAs(messageGroup);
146-
// assertEquals(0, messageGroupA.getMarked().size());
147+
// assertEquals(0, messageGroupA.getMarked().size());
147148
assertThat(messageGroupA.getMessages().size()).isEqualTo(0);
148149
assertThat(messageGroupA.size()).isEqualTo(0);
149150

@@ -438,6 +439,9 @@ public void testJsonSerialization() {
438439
store.setValueSerializer(serializer);
439440

440441
Message<?> genericMessage = new GenericMessage<>(new Date());
442+
NullChannel testComponent = new NullChannel();
443+
testComponent.setBeanName("testChannel");
444+
genericMessage = MessageHistory.write(genericMessage, testComponent);
441445
Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
442446
Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
443447
ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
@@ -448,6 +452,7 @@ public void testJsonSerialization() {
448452
assertThat(messageGroup.size()).isEqualTo(4);
449453
List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
450454
assertThat(messages.get(0)).isEqualTo(genericMessage);
455+
assertThat(messages.get(0).getHeaders()).containsKeys(MessageHistory.HEADER_NAME);
451456
assertThat(messages.get(1)).isEqualTo(mutableMessage);
452457
assertThat(messages.get(2)).isEqualTo(adviceMessage);
453458
Message<?> errorMessageResult = messages.get(3);

0 commit comments

Comments
 (0)