Improve Kryo Codec for registrations
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2019 the original author or authors.
|
||||
* Copyright 2015-2020 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.
|
||||
@@ -44,6 +44,7 @@ public abstract class AbstractKryoCodec implements Codec {
|
||||
protected AbstractKryoCodec() {
|
||||
KryoFactory factory = () -> {
|
||||
Kryo kryo = new Kryo();
|
||||
kryo.setRegistrationRequired(true);
|
||||
// configure Kryo instance, customize settings
|
||||
configureKryoInstance(kryo);
|
||||
return kryo;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.codec.kryo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
@@ -39,6 +40,13 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar {
|
||||
|
||||
private int initialValue = 50;
|
||||
|
||||
/**
|
||||
* @param classes the vararg of classes to validateRegistration
|
||||
*/
|
||||
public KryoClassListRegistrar(Class<?>... classes) {
|
||||
this(Arrays.asList(classes));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param classes the list of classes to validateRegistration
|
||||
*/
|
||||
@@ -52,8 +60,7 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar {
|
||||
* @param initialValue the initial value
|
||||
*/
|
||||
public void setInitialValue(int initialValue) {
|
||||
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE,
|
||||
"'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
|
||||
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE, "'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
|
||||
this.initialValue = initialValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2019 the original author or authors.
|
||||
* Copyright 2015-2020 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.
|
||||
@@ -39,8 +39,9 @@ public class CompositeCodecTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Map<Class<?>, Codec> codecs = new HashMap<Class<?>, Codec>();
|
||||
this.codec = new CompositeCodec(codecs, new PojoCodec());
|
||||
Map<Class<?>, Codec> codecs = new HashMap<>();
|
||||
this.codec = new CompositeCodec(codecs, new PojoCodec(
|
||||
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2019 the original author or authors.
|
||||
* Copyright 2015-2020 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.
|
||||
@@ -66,7 +66,7 @@ public class KryoCodecTests {
|
||||
|
||||
@Test
|
||||
public void testPojoSerialization() throws IOException {
|
||||
PojoCodec codec = new PojoCodec();
|
||||
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class));
|
||||
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("foo", 123);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
codec.encode(foo, bos);
|
||||
@@ -101,12 +101,12 @@ public class KryoCodecTests {
|
||||
|
||||
@Test
|
||||
public void testMapSerialization() throws IOException {
|
||||
PojoCodec codec = new PojoCodec();
|
||||
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(HashMap.class));
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
map.put("one", 1);
|
||||
map.put("two", 2);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
codec.encode(map, bos);
|
||||
codec.encode(map, bos);4
|
||||
Map<?, ?> m2 = (Map<?, ?>) codec.decode(bos.toByteArray(), HashMap.class);
|
||||
assertThat(m2.size()).isEqualTo(2);
|
||||
assertThat(m2.get("one")).isEqualTo(1);
|
||||
@@ -115,7 +115,7 @@ public class KryoCodecTests {
|
||||
|
||||
@Test
|
||||
public void testComplexObjectSerialization() throws IOException {
|
||||
PojoCodec codec = new PojoCodec();
|
||||
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(Foo.class));
|
||||
Foo foo = new Foo();
|
||||
foo.put("one", 1);
|
||||
foo.put("two", 2);
|
||||
|
||||
Reference in New Issue
Block a user