From 8e52c2bacab6e932c6d392fab940afbd0523ca83 Mon Sep 17 00:00:00 2001 From: zhangping17 Date: Mon, 11 Mar 2024 10:02:18 +0800 Subject: [PATCH] add SerializerFactory when use hessian --- jcommon/rpc-codes/pom.xml | 1 + .../DefaultHessian2FactoryInitializer.java | 24 +++++++++++++++++++ .../data/push/uds/codes/HessianCodes.java | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/DefaultHessian2FactoryInitializer.java diff --git a/jcommon/rpc-codes/pom.xml b/jcommon/rpc-codes/pom.xml index 35ae38f7f..e84546c4d 100644 --- a/jcommon/rpc-codes/pom.xml +++ b/jcommon/rpc-codes/pom.xml @@ -10,6 +10,7 @@ 4.0.0 rpc-codes + 1.6.0-SNAPSHOT 8 diff --git a/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/DefaultHessian2FactoryInitializer.java b/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/DefaultHessian2FactoryInitializer.java new file mode 100644 index 000000000..e57e234b7 --- /dev/null +++ b/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/DefaultHessian2FactoryInitializer.java @@ -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; + } +} diff --git a/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/HessianCodes.java b/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/HessianCodes.java index 364037465..567584a9e 100644 --- a/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/HessianCodes.java +++ b/jcommon/rpc-codes/src/main/java/com/xiaomi/data/push/uds/codes/HessianCodes.java @@ -35,6 +35,7 @@ public class HessianCodes implements ICodes { public 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) { @@ -53,6 +54,7 @@ public T decode(byte[] data, Type type) { public byte[] encode(T t) { ByteArrayOutputStream os = new ByteArrayOutputStream(); Hessian2Output ho = new Hessian2Output(os); + ho.setSerializerFactory(DefaultHessian2FactoryInitializer.getSerializerFactory()); try { ho.writeObject(t); ho.flush();