Skip to content

Commit c83da55

Browse files
committed
Improve MessageKryoRegistrar for registrations
(cherry picked from commit 5ec71d4) # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/CompositeKryoRegistrar.java # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/CompositeKryoRegistrar.java # spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/MessageCodec.java # spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/MessageKryoRegistrar.java # spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/RegistrationIds.java
1 parent 9ada785 commit c83da55

File tree

4 files changed

+117
-9
lines changed

4 files changed

+117
-9
lines changed

spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/CompositeKryoRegistrar.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2020 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.
@@ -22,6 +22,7 @@
2222
import org.springframework.util.Assert;
2323
import org.springframework.util.CollectionUtils;
2424

25+
import com.esotericsoftware.kryo.Kryo;
2526
import com.esotericsoftware.kryo.Registration;
2627

2728
/**
@@ -43,7 +44,14 @@ public CompositeKryoRegistrar(List<KryoRegistrar> delegates) {
4344
}
4445

4546
@Override
46-
public List<Registration> getRegistrations() {
47+
public void registerTypes(Kryo kryo) {
48+
for (KryoRegistrar registrar : this.delegates) {
49+
registrar.registerTypes(kryo);
50+
}
51+
}
52+
53+
@Override
54+
public final List<Registration> getRegistrations() {
4755
List<Registration> registrations = new ArrayList<Registration>();
4856
for (KryoRegistrar registrar : this.delegates) {
4957
registrations.addAll(registrar.getRegistrations());

spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/MessageCodec.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2020 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.
@@ -18,7 +18,9 @@
1818

1919
/**
2020
* {@link PojoCodec} configured to encode/decode {@code Message<?>}s.
21+
*
2122
* @author Gary Russell
23+
*
2224
* @since 4.2
2325
*
2426
*/

spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/MessageKryoRegistrar.java

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2020 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.
@@ -17,41 +17,127 @@
1717
package org.springframework.integration.codec.kryo;
1818

1919
import java.util.Arrays;
20+
import java.util.HashMap;
2021
import java.util.List;
22+
import java.util.UUID;
2123

24+
import org.springframework.integration.message.AdviceMessage;
25+
import org.springframework.integration.support.MutableMessage;
2226
import org.springframework.integration.support.MutableMessageHeaders;
2327
import org.springframework.messaging.MessageHeaders;
28+
import org.springframework.messaging.support.ErrorMessage;
29+
import org.springframework.messaging.support.GenericMessage;
2430

31+
import com.esotericsoftware.kryo.Kryo;
2532
import com.esotericsoftware.kryo.Registration;
2633

2734
/**
2835
* Registers common MessageHeader types and Serializers.
36+
*
2937
* @author David Turanski
3038
* @author Gary Russell
39+
* @author Artem Bilan
40+
*
3141
* @since 4.2
3242
*/
3343
public class MessageKryoRegistrar extends AbstractKryoRegistrar {
3444

35-
private volatile int messageHeadersRegistrationId = RegistrationIds.DEFAULT_MESSAGEHEADERS_ID;
45+
private int genericMessageRegistrationId = RegistrationIds.DEFAULT_GENERIC_MESSAGE_ID;
46+
47+
private int errorMessageRegistrationId = RegistrationIds.DEFAULT_ERROR_MESSAGE_ID;
48+
49+
private int adviceMessageRegistrationId = RegistrationIds.DEFAULT_ADVICE_MESSAGE_ID;
50+
51+
private int mutableMessageRegistrationId = RegistrationIds.DEFAULT_MUTABLE_MESSAGE_ID;
3652

37-
private volatile int mutableMessageHeadersRegistrationId = RegistrationIds.DEFAULT_MUTABLE_MESSAGEHEADERS_ID;
53+
private int messageHeadersRegistrationId = RegistrationIds.DEFAULT_MESSAGEHEADERS_ID;
54+
55+
private int mutableMessageHeadersRegistrationId = RegistrationIds.DEFAULT_MUTABLE_MESSAGEHEADERS_ID;
56+
57+
private int hashMapRegistrationId = RegistrationIds.DEFAULT_HASH_MAP_ID;
58+
59+
private int uuidRegistrationId = RegistrationIds.DEFAULT_UUID_ID;
3860

3961
/**
40-
* Set the registration id for {@code MessageHeaders}.
62+
* Set the registration id for {@link MessageHeaders}.
4163
* @param messageHeadersRegistrationId the id, default 41.
4264
*/
4365
public void setMessageHeadersRegistrationId(int messageHeadersRegistrationId) {
4466
this.messageHeadersRegistrationId = messageHeadersRegistrationId;
4567
}
4668

4769
/**
48-
* Set the registration id for {@code MutableMessageHeaders}.
70+
* Set the registration id for {@link MutableMessageHeaders}.
4971
* @param mutableMessageHeadersRegistrationId the id, default 42.
5072
*/
5173
public void setMutableMessageHeadersRegistrationId(int mutableMessageHeadersRegistrationId) {
5274
this.mutableMessageHeadersRegistrationId = mutableMessageHeadersRegistrationId;
5375
}
5476

77+
/**
78+
* Set the registration id for {@link GenericMessage}.
79+
* @param genericMessageRegistrationId the id, default 43.
80+
* @since 4.3.23
81+
*/
82+
public void setGenericMessageRegistrationId(int genericMessageRegistrationId) {
83+
this.genericMessageRegistrationId = genericMessageRegistrationId;
84+
}
85+
86+
/**
87+
* Set the registration id for {@link ErrorMessage}.
88+
* @param errorMessageRegistrationId the id, default 44.
89+
* @since 4.3.23
90+
*/
91+
public void setErrorMessageRegistrationId(int errorMessageRegistrationId) {
92+
this.errorMessageRegistrationId = errorMessageRegistrationId;
93+
}
94+
95+
/**
96+
* Set the registration id for {@link AdviceMessage}.
97+
* @param adviceMessageRegistrationId the id, default 45.
98+
* @since 4.3.23
99+
*/
100+
public void setAdviceMessageRegistrationId(int adviceMessageRegistrationId) {
101+
this.adviceMessageRegistrationId = adviceMessageRegistrationId;
102+
}
103+
104+
/**
105+
* Set the registration id for {@link MutableMessage}.
106+
* @param mutableMessageRegistrationId the id, default 46.
107+
* @since 4.3.23
108+
*/
109+
public void setMutableMessageRegistrationId(int mutableMessageRegistrationId) {
110+
this.mutableMessageRegistrationId = mutableMessageRegistrationId;
111+
}
112+
113+
/**
114+
* Set the registration id for {@link HashMap}.
115+
* @param hashMapRegistrationId the id, default 47.
116+
* @since 4.3.23
117+
*/
118+
public void setHashMapRegistrationId(int hashMapRegistrationId) {
119+
this.hashMapRegistrationId = hashMapRegistrationId;
120+
}
121+
122+
/**
123+
* Set the registration id for {@link UUID}.
124+
* @param uuidRegistrationId the id, default 48.
125+
* @since 4.3.23
126+
*/
127+
public void setUuidRegistrationId(int uuidRegistrationId) {
128+
this.uuidRegistrationId = uuidRegistrationId;
129+
}
130+
131+
@Override
132+
public void registerTypes(Kryo kryo) {
133+
super.registerTypes(kryo);
134+
kryo.register(GenericMessage.class, this.genericMessageRegistrationId);
135+
kryo.register(ErrorMessage.class, this.errorMessageRegistrationId);
136+
kryo.register(AdviceMessage.class, this.adviceMessageRegistrationId);
137+
kryo.register(MutableMessage.class, this.mutableMessageRegistrationId);
138+
kryo.register(HashMap.class, this.hashMapRegistrationId);
139+
kryo.register(UUID.class, this.uuidRegistrationId);
140+
}
55141

56142
@Override
57143
public List<Registration> getRegistrations() {

spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/RegistrationIds.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2020 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.
@@ -31,6 +31,18 @@ public final class RegistrationIds {
3131

3232
public static final int DEFAULT_MUTABLE_MESSAGEHEADERS_ID = 42;
3333

34+
public static final int DEFAULT_GENERIC_MESSAGE_ID = 43;
35+
36+
public static final int DEFAULT_ERROR_MESSAGE_ID = 44;
37+
38+
public static final int DEFAULT_ADVICE_MESSAGE_ID = 45;
39+
40+
public static final int DEFAULT_MUTABLE_MESSAGE_ID = 46;
41+
42+
public static final int DEFAULT_HASH_MAP_ID = 47;
43+
44+
public static final int DEFAULT_UUID_ID = 48;
45+
3446
private RegistrationIds() { }
3547

3648
}

0 commit comments

Comments
 (0)