Improve Kryo Codec for registrations
# Conflicts: # spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoCodec.java # spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java # spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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.
|
||||
@@ -46,6 +46,7 @@ public abstract class AbstractKryoCodec implements Codec {
|
||||
KryoFactory factory = new KryoFactory() {
|
||||
public Kryo create() {
|
||||
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-2016 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-2016 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.
|
||||
@@ -63,7 +63,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);
|
||||
@@ -98,12 +98,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<String, Integer>();
|
||||
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);
|
||||
assertEquals(2, m2.size());
|
||||
assertEquals(1, m2.get("one"));
|
||||
@@ -112,7 +112,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