forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support kryo codec (apache#1437)
- Loading branch information
1 parent
1f9615f
commit 031a99f
Showing
11 changed files
with
448 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
55 changes: 55 additions & 0 deletions
55
codec/seata-codec-kryo/src/main/java/io/seata/codec/kryo/KryoCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
codec/seata-codec-kryo/src/main/java/io/seata/codec/kryo/KryoSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.