Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Gosling committed Jan 6, 2015
1 parent b937abb commit e5b5a4c
Show file tree
Hide file tree
Showing 13 changed files with 793 additions and 144 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.class
.gradle/
build/
.idea/
*.iml

Expand Down
83 changes: 47 additions & 36 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@ apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'

ext.projectIds = ['group': 'com.creative.commons', 'version': '0.1']
group = projectIds.group
version = projectIds.version

jar {
baseName = 'Jvm-serializer'
version = '1.0'
manifest {
attributes 'Implementation-Title': 'Jvm serializer', 'Implementation-Version': version
}

configurations{
serializer{
description='One jvm seriazlier benchmark platform.'
visible=false
configurations {
serializer {
description = 'One jvm seriazlier benchmark platform.'
visible = false
}
}
configurations.serializer {
resolutionStrategy {
failOnVersionConflict()
force 'junit:junit:4.11'
cacheDynamicVersionsFor(0, 'seconds')
}
failOnVersionConflict()
force('junit:junit:4.11')
cacheDynamicVersionsFor(0, 'seconds')
}
}

configurations.compile.resolutionStrategy {
cacheChangingModulesFor(0, 'seconds')
}
}

ext {
serializerGroup='com.creative.commons'
serializerGroup=version
linkHomepage = 'https://github.com/vongosling/jvm-serializer'
linkCi = 'https://github.com/vongosling/jvm-serializer/issues'
linkIssue = 'https://github.com/vongosling/jvm-serializer/issues'
Expand All @@ -40,44 +34,61 @@ ext {
}

compileJava.options*.compilerArgs = [
"-Xlint:serial", "-Xlint:varargs", "-Xlint:cast", "-Xlint:classfile",
"-Xlint:dep-ann", "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally",
"-Xlint:overrides", "-Xlint:path", "-Xlint:processing", "-Xlint:static",
"-Xlint:try", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation",
"-Xlint:unchecked", "-Xlint:-options", "-Werror"
"-Xlint:serial", "-Xlint:varargs", "-Xlint:cast", "-Xlint:classfile",
"-Xlint:dep-ann", "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally",
"-Xlint:overrides", "-Xlint:path", "-Xlint:processing", "-Xlint:static",
"-Xlint:try", "-Xlint:fallthrough", "-Xlint:rawtypes", "-Xlint:deprecation",
"-Xlint:unchecked", "-Xlint:-options", "-Werror"
]

compileTestJava.options*.compilerArgs = [
"-Xlint:serial", "-Xlint:varargs", "-Xlint:cast", "-Xlint:classfile",
"-Xlint:dep-ann", "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally",
"-Xlint:overrides", "-Xlint:path", "-Xlint:processing", "-Xlint:static",
"-Xlint:try", "-Xlint:-fallthrough", "-Xlint:-rawtypes", "-Xlint:-deprecation",
"-Xlint:-unchecked", "-Xlint:-options"
"-Xlint:serial", "-Xlint:varargs", "-Xlint:cast", "-Xlint:classfile",
"-Xlint:dep-ann", "-Xlint:divzero", "-Xlint:empty", "-Xlint:finally",
"-Xlint:overrides", "-Xlint:path", "-Xlint:processing", "-Xlint:static",
"-Xlint:try", "-Xlint:-fallthrough", "-Xlint:-rawtypes", "-Xlint:-deprecation",
"-Xlint:-unchecked", "-Xlint:-options"
]

compileJava {
sourceCompatibility=1.7
targetCompatibility=1.7
sourceCompatibility = 1.7
targetCompatibility = 1.7
}

compileTestJava {
sourceCompatibility=1.7
targetCompatibility=1.7
sourceCompatibility = 1.7
targetCompatibility = 1.7
}

jar {
baseName = 'Jvm-serializer'
version = '1.0'
manifest {
attributes 'Implementation-Title': 'Jvm serializer', 'Implementation-Version': version
}
}

repositories {
mavenCentral()
}

dependencies {
compile group: 'com.caucho', name: 'hessian', version : '4.0.38'
compile group: 'com.alibaba', name: 'fastjson', version : '1.2.3'
compile group: 'com.esotericsoftware', name: 'kryo-shaded', version : '3.0.0'
compile group: 'com.caucho', name: 'hessian', version: '4.0.38'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.3'
compile group: 'com.esotericsoftware', name: 'kryo-shaded', version: '3.0.0'
//compile group: 'com.esotericsoftware.kryo', name: 'kryo', version: '2.24.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
compile('com.google.guava:guava:18.0')
//testCompile 'org.codehaus.groovy:groovy:2.3.9'
}

test {
systemProperties 'property': 'value'
//systemProperties 'property': 'value'
testLogging {
showStandardStreams = true
exceptionFormat 'full'
events 'started', 'passed', 'skipped', 'failed'
}
minHeapSize = '128m'
maxHeapSize = '256m'
jvmArgs '-XX:MaxPermSize=128m'
}
18 changes: 13 additions & 5 deletions src/main/java/com/creative/commons/utils/HessianCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Locale;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
import com.caucho.hessian.io.*;

/**
* @author von gosling
*/
public abstract class HessianCodec {

private static final SerializerFactory serializerFactory = new SerializerFactory();
private static final SerializerFactory serializerFactory = SerializerFactory.createDefault();

static {
ExtSerializerFactory extFactory = new ExtSerializerFactory();
extFactory.addSerializer(Locale.class, LocaleSerializer.create());
extFactory.addSerializer(BigDecimal.class, new StringValueSerializer());
extFactory.addDeserializer(BigDecimal.class,new BigDecimalDeserializer());
serializerFactory.addFactory(extFactory);
}

public static Serializable decode(byte[] array) throws IOException {
Object obj = null;
Object obj;
ByteArrayInputStream bais = new ByteArrayInputStream(array);
Hessian2Input hi = new Hessian2Input(bais);
hi.setSerializerFactory(serializerFactory);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/creative/commons/utils/JsonCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
*/
public abstract class JsonCodec {
private static SerializeConfig config = new SerializeConfig();
private static SerializerFeature[] sfeatures = { SerializerFeature.UseISO8601DateFormat };
private static Feature[] features = { Feature.AllowISO8601DateFormat };
private static SerializerFeature[] sfeatures = {SerializerFeature.WriteEnumUsingToString,SerializerFeature.UseISO8601DateFormat, SerializerFeature.DisableCircularReferenceDetect};
private static Feature[] features = {Feature.AllowISO8601DateFormat, Feature.DisableCircularReferenceDetect};

static {
config.put(java.util.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
config.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
Expand All @@ -24,7 +25,7 @@ public static byte[] encode(Object content) {
return JSONObject.toJSONString(content, config, sfeatures).getBytes();
}

public static Object decode(byte[] content) {
return JSON.parse(content, features);
public static <T> T decode(byte[] content, Class<T> clazz) {
return JSON.parseObject(content, clazz, features);
}
}
66 changes: 59 additions & 7 deletions src/main/java/com/creative/commons/utils/KryoCodec.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,71 @@
package com.creative.commons.utils;

import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.*;
import com.google.common.collect.Lists;
import org.objenesis.strategy.StdInstantiatorStrategy;
import sun.util.calendar.ZoneInfo;

/**
* @author von gosling
*/
public abstract class KryoCodec {
private static final List<Class<?>> classList = new ArrayList<Class<?>>();
private static final List<Serializer<?>> serializerList = new ArrayList<Serializer<?>>();
private static final List<Integer> idList = new ArrayList<Integer>();
private static final List<Class<?>> classList = Lists.newArrayList();
private static final List<Serializer<?>> serializerList = Lists.newArrayList();
private static final List<Integer> idList = Lists.newArrayList();
private static final ThreadLocal<Kryo> kryos = new ThreadLocal<Kryo>() {
protected Kryo initialValue() {
Kryo kryo = new Kryo();

kryo.register(byte[].class);
kryo.register(char[].class);
kryo.register(short[].class);
kryo.register(int[].class);
kryo.register(long[].class);
kryo.register(float[].class);
kryo.register(double[].class);
kryo.register(boolean[].class);
kryo.register(String[].class);
kryo.register(Object[].class);
kryo.register(KryoSerializable.class);
kryo.register(BigInteger.class);
kryo.register(BigDecimal.class);
kryo.register(Class.class);
kryo.register(Date.class);
//kryo.register(Enum.class);
kryo.register(EnumSet.class);
kryo.register(Currency.class);
kryo.register(StringBuffer.class);
kryo.register(StringBuilder.class);
kryo.register(Collections.EMPTY_LIST.getClass());
kryo.register(Collections.EMPTY_MAP.getClass());
kryo.register(Collections.EMPTY_SET.getClass());
kryo.register(Collections.singletonList(null).getClass());
kryo.register(Collections.singletonMap(null, null).getClass());
kryo.register(Collections.singleton(null).getClass());
kryo.register(TreeSet.class);
kryo.register(Collection.class);
kryo.register(TreeMap.class);
kryo.register(Map.class);
kryo.register(TimeZone.class);
kryo.register(Calendar.class);
kryo.register(Locale.class);

kryo.register(BitSet.class);
kryo.register(HashMap.class);
kryo.register(Timestamp.class);
kryo.register(ZoneInfo.class);
kryo.register(ArrayList.class);

int size = idList.size();
for (int i = 0; i < size; i++) {
kryo.register(classList
Expand All @@ -28,6 +76,7 @@ protected Kryo initialValue() {
}
kryo.setRegistrationRequired(true);
kryo.setReferences(false);
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
return kryo;
}
};
Expand All @@ -39,6 +88,10 @@ public static synchronized void registerClass(Class<?> className, Serializer<?>
idList.add(id);
}

public static synchronized void register(Class<?> className){
getKryo().register(className);
}

public static Kryo getKryo() {
return kryos.get();
}
Expand All @@ -49,8 +102,7 @@ public static Object decode(byte[] bytes) throws Exception {
}

public static byte[] encode(Object object) throws Exception {
Output output = new Output(256);
getKryo().register(object.getClass());
Output output = new Output(1024 * 1024);
getKryo().writeClassAndObject(output, object);
return output.toBytes();
}
Expand Down
Loading

0 comments on commit e5b5a4c

Please sign in to comment.