Skip to content

Commit 19ab19a

Browse files
author
Dan Blaisdell
committed
hessian serializer that can be used instead of the default Java serializer
1 parent ade7b6b commit 19ab19a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
<groupId>de.flapdoodle.embed</groupId>
137137
<artifactId>de.flapdoodle.embed.mongo</artifactId>
138138
</dependency>
139+
<dependency>
140+
<groupId>com.caucho</groupId>
141+
<artifactId>hessian</artifactId>
142+
<version>4.0.38</version>
143+
</dependency>
139144
</dependencies>
140145

141146
<build>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.arhs.spring.cache.mongo.serializer;
2+
3+
import com.caucho.hessian.io.Hessian2Input;
4+
import com.caucho.hessian.io.Hessian2Output;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
11+
/**
12+
* Adapts HessianSerializer so it can be used in Redis. User: msellers Date:
13+
* 9/3/14 Time: 10:17 AM
14+
*/
15+
public class HessianSerializer implements Serializer {
16+
17+
private static final Logger m_logger = LoggerFactory.getLogger(HessianSerializer.class);
18+
19+
@Override
20+
public byte[] serialize(Object obj) {
21+
try {
22+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
23+
Hessian2Output out = new Hessian2Output(bos);
24+
out.startMessage();
25+
out.writeObject(obj);
26+
out.completeMessage();
27+
out.close();
28+
byte[] array = bos.toByteArray();
29+
30+
return array;
31+
} catch (Exception e) {
32+
m_logger.error("Error Serializing a value to be put into the Redis cache", e);
33+
throw new RuntimeException(e.getMessage());
34+
}
35+
}
36+
37+
@Override
38+
public Object deserialize(byte[] bytes) {
39+
try {
40+
Object toReturn = null;
41+
42+
if (bytes != null) {
43+
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
44+
Hessian2Input in = new Hessian2Input(bin);
45+
in.startMessage();
46+
47+
toReturn = in.readObject();
48+
}
49+
50+
return toReturn;
51+
} catch (Exception e) {
52+
m_logger.error("Error De-Serializing a value from the Redis cache", e);
53+
throw new RuntimeException(e.getMessage());
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)