question about "KryoRedisSerializer implements RedisSerializer<Object>"

1,003 views
Skip to first unread message

Sungwon Jung

unread,
Apr 28, 2016, 10:58:23 AM4/28/16
to kryo-users
Hello. I have a one question.

[pom.xml]
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>3.0.3</version>
</dependency>

[KryoRedisSerializer.java]
public class KryoRedisSerializer implements RedisSerializer<Object> {

Kryo kryo = new Kryo();

public KryoRedisSerializer() {
kryo.setInstantiatorStrategy(new SerializingInstantiatorStrategy());
}

@Override
public byte[] serialize(Object object) {
if (object == null) {
return new byte[0];
}

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Output output = new Output(outputStream);
kryo.writeClassAndObject(output, object);
return output.toBytes();
} catch (IOException e) {
throw new SerializationException("Failed Serialization", e);
}
}
@Override
public Object deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}

try (Input input = new Input(bytes)) {
return kryo.readClassAndObject(input);
}
}

}

[conf]
@Bean
public RedisClusterTemplate<String, ?> redisClusterTemplate() {
RedisClusterTemplate<String, ?> redisTemplate = new RedisClusterTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());

redisTemplate.setKeySerializer(stringSerializer());
redisTemplate.setHashKeySerializer(stringSerializer());

redisTemplate.setHashValueSerializer(kryoRedisSerializer());
redisTemplate.setValueSerializer(kryoRedisSerializer());

redisTemplate.afterPropertiesSet();
return redisTemplate;
}

@Bean
public RedisSerializer<String> stringSerializer() {
return new StringRedisSerializer();
}

@Bean
public RedisSerializer<Object> kryoRedisSerializer() {
return new KryoRedisSerializer();
}

[test code]
@Test
public void test() {
String key = "11:12";

Mod mod = new Mod();
mod.setA(2);
redisClusterTemplate.opsForValue().set(key, mod);

System.out.println(redisClusterTemplate.opsForValue().get(key));
System.out.println("size : " + redisClusterTemplate.opsForValue().size(key));
}

public class Mod implements Serializable {

private static final long serialVersionUID = 2750816192200570448L;

private Integer a;

public Mod() {
}

public Integer getA() {
return a;
}

public void setA(Integer a) {
this.a = a;
}
}


1) this test code is works.
2) I've changed test code like this.
    @Test
public void test() {
String key = "11:12";

// Mod mod = new Mod();
// mod.setA(2);
// redisClusterTemplate.opsForValue().set(key, mod);

System.out.println(redisClusterTemplate.opsForValue().get(key));
System.out.println("size : " + redisClusterTemplate.opsForValue().size(key));
}

public class Mod implements Serializable {

private static final long serialVersionUID = 2750816192200570448L;

private Integer a;
private Integer b;

public Mod() {
}

public Integer getA() {
return a;
}

public void setA(Integer a) {
this.a = a;
}

public Integer getB() {
return b;
}

public void setB(Integer b) {
this.b = b;
}

}

3) I just added member variable 'b' and error occured
여기에 코드 com.esotericsoftware.kryo.KryoException: Buffer underflow.
Serialization trace:
b (us.band.ds.feed.core.commons.nbasearc.NbaseArcTest$Mod)

at com.esotericsoftware.kryo.io.Input.require(Input.java:199)
at com.esotericsoftware.kryo.io.Input.readVarInt(Input.java:373)
at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:808)
at com.esotericsoftware.kryo.Kryo.readObjectOrNull(Kryo.java:757)
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:132)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:551)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:790)
at us.band.ds.abuser.core.redis.KryoRedisSerializer.deserialize(KryoRedisSerializer.java:46)
at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:315)
at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:55)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88)
at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
at us.band.ds.feed.core.commons.nbasearc.NbaseArcTest.test(NbaseArcTest.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


same key and I just added member variable. and then opsForValue().get(key).
I don't know why error occured and how can I slove.
Please share your know-how.

Thank you.

Martin Grotzke

unread,
Apr 28, 2016, 3:55:45 PM4/28/16
to kryo-users

Please read https://github.com/EsotericSoftware/kryo#compatibility

Cheers,
Martin


--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users
---
You received this message because you are subscribed to the Google Groups "kryo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kryo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sungwon Jung

unread,
Apr 29, 2016, 12:25:28 AM4/29/16
to kryo-users
I got it!

Thank you.

2016년 4월 29일 금요일 오전 4시 55분 45초 UTC+9, Martin Grotzke 님의 말:

Raja K

unread,
Sep 1, 2017, 6:01:29 PM9/1/17
to kryo-users
Can any one of you explain the reason behind the compatibility as I couldn't get to see how the error is related to.

Thanks,
Raja

Martin Grotzke

unread,
Sep 2, 2017, 4:04:20 AM9/2/17
to kryo-users

FieldSerializer (during deserialization) reads data from the input according to the current fields of the class (determined via reflection). If the class was changed there's a mismatch between available/serialized data and consumed data.

Cheers,
Martin

Ratheesh K

unread,
Jan 14, 2018, 2:36:53 AM1/14/18
to kryo-users
How to set non-serialized Model object to RedisCache using kryoSerialzation.

I have a model object (Type T, i.e, response of some remote API).
Tried with Sungwon Jung's  KryoRedisSerializer   code its not working.

Mode.java
public class Model{
    private  String prop1;
    ......
}.

Error: org.objenesis.ObjenesisException: java.io.NotSerializableException: class Model object not serializable.

I couldn't implements Serializable interface on this model object since this is the response of some remote API also, the type of the response will change based on the request parameter. 
Kindly help me on this issue.
Reply all
Reply to author
Forward
0 new messages