- 反序列化
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);
}
}
}
61
1
import com.caucho.hessian.HessianException;
2
import com.caucho.hessian.io.AbstractDeserializer;
3
import com.caucho.hessian.io.AbstractHessianInput;
4
import com.caucho.hessian.io.IOExceptionWrapper;
5
6
import java.io.IOException;
7
import java.time.LocalDateTime;
8
import java.time.ZoneOffset;
9
10
/**
11
* @author zenglw
12
* @date 2018/6/7
13
*/
14
public class LocalDateTimeDeserializer extends AbstractDeserializer {
15
16
17
public Class getType()
18
{
19
return LocalDateTime.class;
20
}
21
22
23
24
public Object readObject(AbstractHessianInput in,
25
Object []fields)
26
throws IOException
27
{
28
String []fieldNames = (String []) fields;
29
30
int ref = in.addRef(null);
31
32
long initValue = Long.MIN_VALUE;
33
34
for (int i = 0; i < fieldNames.length; i++) {
35
String key = fieldNames[i];
36
37
if (key.equals("value")) {
38
initValue = in.readUTCDate();
39
} else {
40
in.readObject();
41
}
42
}
43
Object value = create(initValue);
44
in.setRef(ref, value);
45
return value;
46
}
47
48
private Object create(long initValue)
49
throws IOException
50
{
51
if (initValue == Long.MIN_VALUE) {
52
throw new IOException(LocalDateTime.class + " expects name.");
53
}
54
try {
55
return LocalDateTime.ofEpochSecond(new Long(initValue)/1000,Integer.valueOf(String.valueOf(initValue%1000))*1000,ZoneOffset.of("+8"));
56
} catch (Exception e) {
57
throw new IOExceptionWrapper(e);
58
}
59
}
60
}
61
- 序列化
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);
}
}
}
}
46
1
import com.caucho.hessian.io.AbstractHessianOutput;
2
import com.caucho.hessian.io.AbstractSerializer;
3
4
import java.io.IOException;
5
import java.time.LocalDateTime;
6
import java.time.ZoneOffset;
7
8
/**
9
* @author zenglw
10
* @date 2018/6/7
11
*/
12
public class LocalDateTimeSerializer extends AbstractSerializer {
13
14
15
public void writeObject(Object obj, AbstractHessianOutput out)
16
throws IOException
17
{
18
if (obj == null) {
19
out.writeNull();
20
} else {
21
Class cl = obj.getClass();
22
23
if (out.addRef(obj)) {
24
return;
25
}
26
// ref 返回-2 便是开始写Map
27
int ref = out.writeObjectBegin(cl.getName());
28
29
if (ref < -1) {
30
out.writeString("value");
31
Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
32
out.writeUTCDate(milliSecond);
33
out.writeMapEnd();
34
} else {
35
if (ref == -1) {
36
out.writeInt(1);
37
out.writeString("value");
38
out.writeObjectBegin(cl.getName());
39
}
40
41
Long milliSecond = ((LocalDateTime) obj).toInstant(ZoneOffset.of("+8")).toEpochMilli();
42
out.writeUTCDate(milliSecond);
43
}
44
}
45
}
46
}
- 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;
}
}
27
1
import com.caucho.hessian.io.ExtSerializerFactory;
2
import com.caucho.hessian.io.SerializerFactory;
3
import com.klxx.ta.common.util.hessian.LocalDateTimeDeserializer;
4
import com.klxx.ta.common.util.hessian.LocalDateTimeSerializer;
5
import org.springframework.context.annotation.Bean;
6
import org.springframework.context.annotation.Configuration;
7
8
/**
9
* @author zenglw
10
* @date 2018/6/7
11
*/
12
13
public class HessianConfig {
14
15
16
public SerializerFactory serializerFactory() {
17
// DO 自定义hessian反序列化
18
// step 1. 定义外部序列化工厂
19
ExtSerializerFactory extSerializerFactory = new ExtSerializerFactory();
20
extSerializerFactory.addSerializer(java.time.LocalDateTime.class,new LocalDateTimeSerializer());
21
extSerializerFactory.addDeserializer(java.time.LocalDateTime.class,new LocalDateTimeDeserializer());
22
// step 2. 序列化工厂
23
SerializerFactory serializerFactory = new SerializerFactory();
24
serializerFactory.addFactory(extSerializerFactory);
25
return serializerFactory;
26
}
27
}
- 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;
}
}
x
1
import com.caucho.hessian.io.SerializerFactory;
2
import com.klxx.ta.foundation.api.OrganizationInfoApi;
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6
import org.springframework.remoting.caucho.HessianServiceExporter;
7
8
9
/**
10
* Hessian注册对外提供服务的service
11
* @author zenglw
12
* @date 2018/6/4
13
*/
14
15
public class HessianExportConfig {
16
17
18
private OrganizationInfoApi organizationInfoApi;
19
20
name = "organizationInfoApi") (
21
public HessianServiceExporter accountService(SerializerFactory serializerFactory) throws Exception {
22
HessianServiceExporter exporter = new HessianServiceExporter();
23
exporter.setSerializerFactory(serializerFactory);
24
exporter.setService(organizationInfoApi);
25
exporter.setServiceInterface(OrganizationInfoApi.class);
26
return exporter;
27
}
28
29
}
- 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;
}
}
x
1
import com.caucho.hessian.io.SerializerFactory;
2
import com.klxx.ta.foundation.api.OrganizationInfoApi;
3
import org.springframework.beans.factory.annotation.Value;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
7
8
/**
9
* @author zenglw
10
* @date 2018/6/7
11
*/
12
13
public class HessianProxyConfig {
14
15
"${hessian.external.service.url.foundation}") (
16
private String foundationUrl;
17
18
name = "accountServiceApi") (
19
public HessianProxyFactoryBean accountServiceApi(SerializerFactory serializerFactory) {
20
HessianProxyFactoryBean hessianProxyFactoryBean = new HessianProxyFactoryBean();
21
hessianProxyFactoryBean.setSerializerFactory(serializerFactory);
22
hessianProxyFactoryBean.setServiceUrl(foundationUrl + "/organizationInfoApi");
23
hessianProxyFactoryBean.setServiceInterface(OrganizationInfoApi.class);
24
return hessianProxyFactoryBean;
25
}
26
}