Skip to content

Commit 23f3482

Browse files
committed
Improve Kryo Codec for registrations
1 parent f095f94 commit 23f3482

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 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.
@@ -44,6 +44,7 @@ public abstract class AbstractKryoCodec implements Codec {
4444
protected AbstractKryoCodec() {
4545
KryoFactory factory = () -> {
4646
Kryo kryo = new Kryo();
47+
kryo.setRegistrationRequired(true);
4748
// configure Kryo instance, customize settings
4849
configureKryoInstance(kryo);
4950
return kryo;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.codec.kryo;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.List;
2122

2223
import org.springframework.util.Assert;
@@ -43,6 +44,13 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar {
4344

4445
private int initialValue = DEFAULT_INITIAL_ID;
4546

47+
/**
48+
* @param classes the vararg of classes to validateRegistration
49+
*/
50+
public KryoClassListRegistrar(Class<?>... classes) {
51+
this(Arrays.asList(classes));
52+
}
53+
4654
/**
4755
* @param classes the list of classes to validateRegistration
4856
*/
@@ -56,8 +64,7 @@ public KryoClassListRegistrar(List<Class<?>> classes) {
5664
* @param initialValue the initial value
5765
*/
5866
public void setInitialValue(int initialValue) {
59-
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE,
60-
"'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
67+
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE, "'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
6168
this.initialValue = initialValue;
6269
}
6370

spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 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.
@@ -39,8 +39,9 @@ public class CompositeCodecTests {
3939

4040
@Before
4141
public void setup() {
42-
Map<Class<?>, Codec> codecs = new HashMap<Class<?>, Codec>();
43-
this.codec = new CompositeCodec(codecs, new PojoCodec());
42+
Map<Class<?>, Codec> codecs = new HashMap<>();
43+
this.codec = new CompositeCodec(codecs, new PojoCodec(
44+
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
4445
}
4546

4647
@Test

spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 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.
@@ -66,7 +66,7 @@ public void testSerializationWithStreams() throws IOException {
6666

6767
@Test
6868
public void testPojoSerialization() throws IOException {
69-
PojoCodec codec = new PojoCodec();
69+
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class));
7070
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("foo", 123);
7171
ByteArrayOutputStream bos = new ByteArrayOutputStream();
7272
codec.encode(foo, bos);
@@ -101,12 +101,12 @@ public void testPrimitiveSerialization() throws IOException {
101101

102102
@Test
103103
public void testMapSerialization() throws IOException {
104-
PojoCodec codec = new PojoCodec();
104+
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(HashMap.class));
105105
Map<String, Integer> map = new HashMap<>();
106106
map.put("one", 1);
107107
map.put("two", 2);
108108
ByteArrayOutputStream bos = new ByteArrayOutputStream();
109-
codec.encode(map, bos);
109+
codec.encode(map, bos);4
110110
Map<?, ?> m2 = (Map<?, ?>) codec.decode(bos.toByteArray(), HashMap.class);
111111
assertThat(m2.size()).isEqualTo(2);
112112
assertThat(m2.get("one")).isEqualTo(1);
@@ -115,7 +115,7 @@ public void testMapSerialization() throws IOException {
115115

116116
@Test
117117
public void testComplexObjectSerialization() throws IOException {
118-
PojoCodec codec = new PojoCodec();
118+
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(Foo.class));
119119
Foo foo = new Foo();
120120
foo.put("one", 1);
121121
foo.put("two", 2);

0 commit comments

Comments
 (0)