Skip to content

Commit

Permalink
feature: support kryo codec (apache#1437)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx authored and xingfudeshi committed Sep 9, 2019
1 parent 1f9615f commit 031a99f
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 1 deletion.
16 changes: 16 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@
<artifactId>seata-codec-protobuf</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-codec-kryo</artifactId>
<version>${project.version}</version>
</dependency>

<!-- spring -->
<dependency>
Expand Down Expand Up @@ -410,6 +415,16 @@
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -512,6 +527,7 @@
<include>io.seata:seata-tm</include>
<include>io.seata:seata-codec-seata</include>
<include>io.seata:seata-codec-protobuf</include>
<include>io.seata:seata-codec-kryo</include>
</includes>
</artifactSet>
<transformers>
Expand Down
12 changes: 12 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<protobuf.version>3.7.1</protobuf.version>
<kryo.version>4.0.2</kryo.version>
<kryo-serializers.version>0.42</kryo-serializers.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -405,6 +407,16 @@
<artifactId>jcommander</artifactId>
<version>${jcommander.version}</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>${kryo.version}</version>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>${kryo-serializers.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
Expand Down
1 change: 1 addition & 0 deletions codec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<module>seata-codec-all</module>
<module>seata-codec-protobuf</module>
<module>seata-codec-seata</module>
<module>seata-codec-kryo</module>
</modules>

</project>
5 changes: 5 additions & 0 deletions codec/seata-codec-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@
<artifactId>seata-codec-protobuf</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-codec-kryo</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
34 changes: 34 additions & 0 deletions codec/seata-codec-kryo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.seata</groupId>
<artifactId>seata-codec</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>seata-codec-kryo</artifactId>
<packaging>jar</packaging>
<name>seata-codec-kryo ${project.version}</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-common</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.seata.codec.kryo;

import io.seata.common.loader.LoadLevel;
import io.seata.core.codec.Codec;
import io.seata.core.protocol.AbstractMessage;

/**
* @author jsbxyyx
*/
@LoadLevel(name = "KRYO")
public class KryoCodec implements Codec {

@Override
public <T> byte[] encode(T t) {
if (t == null || !(t instanceof AbstractMessage)) {
throw new IllegalArgumentException("message is null");
}
KryoSerializer kryoSerializer = KryoSerializerFactory.getInstance().get();
try {
return kryoSerializer.serialize(t);
} finally {
KryoSerializerFactory.getInstance().returnKryo(kryoSerializer);
}
}

@Override
public <T> T decode(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("bytes is null");
}
KryoSerializer kryoSerializer = KryoSerializerFactory.getInstance().get();
try {
return kryoSerializer.deserialize(bytes);
} finally {
KryoSerializerFactory.getInstance().returnKryo(kryoSerializer);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.seata.codec.kryo;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Objects;

/**
* @author jsbxyyx
*/
public class KryoSerializer {

private final Kryo kryo;

public KryoSerializer(Kryo kryo) {
this.kryo = Objects.requireNonNull(kryo);
}

public Kryo getKryo() {
return kryo;
}

public <T> byte[] serialize(T t) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
kryo.writeClassAndObject(output, t);
output.close();
return baos.toByteArray();
}

public <T> T deserialize(byte[] bytes) {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
Input input = new Input(bais);
input.close();
return (T) kryo.readClassAndObject(input);
}

}
Loading

0 comments on commit 031a99f

Please sign in to comment.