Skip to content

Commit

Permalink
add SerializerFactory when use hessian
Browse files Browse the repository at this point in the history
  • Loading branch information
caochengxiang committed Mar 11, 2024
1 parent 7d795da commit 8e52c2b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions jcommon/rpc-codes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>rpc-codes</artifactId>
<version>1.6.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xiaomi.data.push.uds.codes;

import com.alibaba.com.caucho.hessian.io.SerializerFactory;

/**
* @author zhangping17
* @date 2024/03/09
*/
public class DefaultHessian2FactoryInitializer{

private static SerializerFactory SERIALIZER_FACTORY;

public static SerializerFactory getSerializerFactory() {
if (SERIALIZER_FACTORY != null) {
return SERIALIZER_FACTORY;
}
synchronized (DefaultHessian2FactoryInitializer.class) {
if (SERIALIZER_FACTORY == null) {
SERIALIZER_FACTORY = new SerializerFactory();
}
}
return SERIALIZER_FACTORY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class HessianCodes implements ICodes {
public <T> T decode(byte[] data, Type type) {
ByteArrayInputStream is = new ByteArrayInputStream(data);
Hessian2Input hi = new Hessian2Input(is);
hi.setSerializerFactory(DefaultHessian2FactoryInitializer.getSerializerFactory());
try {
return (T) hi.readObject();
} catch (IOException e) {
Expand All @@ -53,6 +54,7 @@ public <T> T decode(byte[] data, Type type) {
public <T> byte[] encode(T t) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.setSerializerFactory(DefaultHessian2FactoryInitializer.getSerializerFactory());
try {
ho.writeObject(t);
ho.flush();
Expand Down

0 comments on commit 8e52c2b

Please sign in to comment.