Skip to content

Commit

Permalink
change defalut serializer to kryo
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Dec 12, 2017
1 parent 58096a6 commit 937f007
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@
<!--如果使用其他序列化组件,和fst冲突,请使用exclusions排除 fst 依赖-->
</dependency>

<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.1</version>
</dependency>

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -20,7 +20,6 @@

public class FastjsonSerializer implements ISerializer {


@Override
public byte[] serialize(Object obj) {
throw new JbootException("not finished");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -22,8 +22,9 @@
public class JbootSerializerConfig {
public static final String FST2 = "fst2";
public static final String FASTJSON = "fastjson";

public String type = FST2;
public static final String KRYO = "kryo";

public String type = KRYO;

public String getType() {
return type;
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/io/jboot/core/serializer/KryoSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) 2015-2017, Michael Yang 杨福海 ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.jboot.core.serializer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferInput;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;

import java.io.ByteArrayOutputStream;

/**
* @author Michael Yang 杨福海 ([email protected]
* @version V1.0
* @Title: Kryo 序列化
* @Description: 性能和 fst一样
* @Package io.jboot.core.serializer
*/
public class KryoSerializer implements ISerializer {


private KryoFactory kryoFactory = new KryoFactory() {
public Kryo create() {
return new Kryo();
}
};

private KryoPool kryoPool = new KryoPool.Builder(kryoFactory).
softReferences()
.build();

@Override
public byte[] serialize(Object obj) {
if (obj == null) return null;
Output output = null;
Kryo kryo = kryoPool.borrow();
try {
output = new Output(new ByteArrayOutputStream());
kryo.writeClassAndObject(output, obj);
return output.toBytes();
} finally {
if (output != null) {
output.close();
}
kryoPool.release(kryo);
}
}

@Override
public Object deserialize(byte[] bytes) {
if (bytes == null || bytes.length == 0) return null;
ByteBufferInput input = null;
Kryo kryo = kryoPool.borrow();
try {
input = new ByteBufferInput(bytes);
return kryo.readClassAndObject(input);
} finally {
if (input != null) {
input.close();
}
kryoPool.release(kryo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -75,10 +75,13 @@ private ISerializer buildSerializer(String serializerString) {


switch (serializerString) {
case JbootSerializerConfig.KRYO:
return new KryoSerializer();
case JbootSerializerConfig.FST2:
return new Fst2Serializer();
case JbootSerializerConfig.FASTJSON:
return new FastjsonSerializer();

default:
return JbootSpiLoader.load(ISerializer.class, serializerString);
}
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/RedisTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io.jboot.Jboot;
import io.jboot.component.redis.JbootRedis;
import org.junit.Test;


Expand All @@ -10,12 +12,13 @@ public void testRedis() {
//// Jboot.setBootArg("jboot.redis.password", "123456");
//
// JbootRedis redis = Jboot.me().getRedis();
// redis.set("mykey", "myvalue");
// redis.set("mykey", "");
//
// redis.lpush("list", 1,2,3,4,5);
// redis.lpush("list", 1,2,3,4,5,6);
//
// System.out.println(redis.get("mykey").toString());
// System.out.println(redis.lrange("list", 0, -1));
//
// System.out.println(redis.lrange("list", 0, -1).size());
//
// System.out.println(redis.blpop(10000, "list"));

Expand Down

0 comments on commit 937f007

Please sign in to comment.