zoukankan      html  css  js  c++  java
  • Spring boot 集成hessian

    - 反序列化

    import com.caucho.hessian.HessianException;
    import com.caucho.hessian.io.AbstractDeserializer;
    import com.caucho.hessian.io.AbstractHessianInput;
    import com.caucho.hessian.io.IOExceptionWrapper;
    
    import java.io.IOException;
    import java.time.LocalDateTime;
    import java.time.ZoneOffset;
    
    /**
     * @author zenglw
     * @date 2018/6/7
     */
    public class LocalDateTimeDeserializer extends AbstractDeserializer {
    
        @Override
        public Class getType()
        {
            return LocalDateTime.class;
        }
    
    
        @Override
        public Object readObject(AbstractHessianInput in,
                                 Object []fields)
                throws IOException
        {
            String []fieldNames = (String []) fields;
    
            int ref = in.addRef(null);
    
            long initValue = Long.MIN_VALUE;
    
            for (int i = 0; i < fieldNames.length; i++) {
                String key = fieldNames[i];
    
                if (key.equals("value")) {
                    initValue = in.readUTCDate();
                } else {
                    in.readObject();
                }
            }
            Object value = create(initValue);
            in.setRef(ref, value);
            return value;
        }
    
        private Object create(long initValue)
                throws IOException
        {
            if (initValue == Long.MIN_VALUE) {
                throw new IOException(LocalDateTime.class + " expects name.");
            }
            try {
                return LocalDateTime.ofEpochSecond(new Long(initValue)/1000,Integer.valueOf(String.valueOf(initValue%1000))*1000,ZoneOffset.of("+8"));
            } catch (Exception e) {
                throw new IOExceptionWrapper(e);
            }
        }
    }

    - 序列化

    import com.caucho.hessian.io.AbstractHessianOutput;
    import com.caucho.hessian.io.AbstractSerializer;
    
    import java.io.IOException;
    import java.time.LocalDateTime;
    import java.time.ZoneOffset;
    
    /**
     * @author zenglw
     * @date 2018/6/7
     */
    public class LocalDateTimeSerializer extends AbstractSerializer {
    
        @Override
        public void writeObject(Object obj, AbstractHessianOutput out)
                throws IOException
        {
            if (obj == null) {
                out.writeNull();
            } else {
                Class cl = obj.getClass();
    
                if (out.addRef(obj)) {
                    return;
                }
                // ref 返回-2 便是开始写Map
                int ref = out.writeObjectBegin(cl.getName());
    
                if (ref < -1) {
                    out.writeString("value");
                    Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    out.writeUTCDate(milliSecond);
                    out.writeMapEnd();
                } else {
                    if (ref == -1) {
                        out.writeInt(1);
                        out.writeString("value");
                        out.writeObjectBegin(cl.getName());
                    }
    
                    Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
                    out.writeUTCDate(milliSecond);
                }
            }
        }
    }

    - hessian的序列化工厂

    import com.caucho.hessian.io.ExtSerializerFactory;
    import com.caucho.hessian.io.SerializerFactory;
    import com.klxx.ta.common.util.hessian.LocalDateTimeDeserializer;
    import com.klxx.ta.common.util.hessian.LocalDateTimeSerializer;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author zenglw
     * @date 2018/6/7
     */
    @Configuration
    public class HessianConfig {
    
        @Bean
        public SerializerFactory serializerFactory() {
            // DO 自定义hessian反序列化
            // step 1. 定义外部序列化工厂
            ExtSerializerFactory extSerializerFactory = new ExtSerializerFactory();
            extSerializerFactory.addSerializer(java.time.LocalDateTime.class,new LocalDateTimeSerializer());
            extSerializerFactory.addDeserializer(java.time.LocalDateTime.class,new LocalDateTimeDeserializer());
            // step 2. 序列化工厂
            SerializerFactory serializerFactory = new SerializerFactory();
            serializerFactory.addFactory(extSerializerFactory);
            return serializerFactory;
        }
    }

    - hessian服务端暴露服务

    import com.caucho.hessian.io.SerializerFactory;
    import com.klxx.ta.foundation.api.OrganizationInfoApi;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.remoting.caucho.HessianServiceExporter;
    
    
    /**
     * Hessian注册对外提供服务的service
     * @author zenglw
     * @date 2018/6/4
     */
    @Configuration
    public class HessianExportConfig {
    
        @Autowired
        private OrganizationInfoApi organizationInfoApi;
    
        @Bean(name = "organizationInfoApi")
        public HessianServiceExporter accountService(SerializerFactory serializerFactory) throws Exception {
            HessianServiceExporter exporter = new HessianServiceExporter();
            exporter.setSerializerFactory(serializerFactory);
            exporter.setService(organizationInfoApi);
            exporter.setServiceInterface(OrganizationInfoApi.class);
            return exporter;
        }
    
    }

    - hessian客户端的服务代理配置

    import com.caucho.hessian.io.SerializerFactory;
    import com.klxx.ta.foundation.api.OrganizationInfoApi;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.remoting.caucho.HessianProxyFactoryBean;
    
    /**
     * @author zenglw
     * @date 2018/6/7
     */
    @Configuration
    public class HessianProxyConfig {
    
        @Value("${hessian.external.service.url.foundation}")
        private String foundationUrl;
    
        @Bean(name = "accountServiceApi")
        public HessianProxyFactoryBean accountServiceApi(SerializerFactory serializerFactory) {
            HessianProxyFactoryBean hessianProxyFactoryBean = new HessianProxyFactoryBean();
            hessianProxyFactoryBean.setSerializerFactory(serializerFactory);
            hessianProxyFactoryBean.setServiceUrl(foundationUrl + "/organizationInfoApi");
            hessianProxyFactoryBean.setServiceInterface(OrganizationInfoApi.class);
            return hessianProxyFactoryBean;
        }
    }
  • 相关阅读:
    C# 批量图片合并工具(附源代码)
    C# 封装
    SQL语句基础
    c# My计算器源码
    炸酱面
    烧茄子
    Linux Desktop Entry 文件深入解析
    硬盘安装ubuntu
    使用C语言进行面向对象的开发--GObject入门[2]
    GObject对象系统 (1)
  • 原文地址:https://www.cnblogs.com/jimboi/p/9241695.html
Copyright © 2011-2022 走看看