Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1060 Update kryo library to 5.3.0 #1062

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
log4jVersion = '1.2.17'
springBootVersion = '2.7.0'
eclipsePersistenceVersion = '2.1.1'
kryoVersion = '4.0.2'
kryoVersion = '5.3.0'
springCloudClusterVersion = '1.0.2.RELEASE'
springShellVersion = '1.1.0.RELEASE'
eclipseEmfXmiVersion = '2.11.1-v20150805-0538'
Expand Down Expand Up @@ -95,7 +95,7 @@ configure(allprojects) {
dependencies {
dependency "log4j:log4j:$log4jVersion"
dependency "org.eclipse.persistence:javax.persistence:$eclipsePersistenceVersion"
dependency "com.esotericsoftware:kryo-shaded:$kryoVersion"
dependency "com.esotericsoftware.kryo:kryo5:$kryoVersion"
dependency "org.springframework.shell:spring-shell:$springShellVersion"
dependency "org.eclipse.uml2:uml:$eclipseUml2UmlVersion"
dependency "org.eclipse.uml2:types:$eclipseUml2TypesVersion"
Expand Down Expand Up @@ -297,7 +297,7 @@ project('spring-statemachine-kryo') {

dependencies {
compile project(':spring-statemachine-core')
compile 'com.esotericsoftware:kryo-shaded'
compile 'com.esotericsoftware.kryo:kryo5'

testCompile (project(':spring-statemachine-test')) { dep ->
exclude group: 'junit', module: 'junit'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.UUID;

import org.springframework.statemachine.kryo.StateMachineContextSerializer;
import org.springframework.statemachine.kryo.MessageHeadersSerializer;
import org.springframework.statemachine.kryo.UUIDSerializer;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.messaging.MessageHeaders;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachineContextRepository;
import org.springframework.statemachine.kryo.MessageHeadersSerializer;
import org.springframework.statemachine.kryo.StateMachineContextSerializer;
import org.springframework.statemachine.kryo.UUIDSerializer;

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

/**
* A {@link StateMachineContextRepository} backed by a redis and kryo serialization.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package org.springframework.statemachine.kryo;
/*
* Copyright 2017-2018 the original author or authors.
*
Expand All @@ -13,24 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.kryo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.esotericsoftware.kryo.kryo5.Kryo;
import com.esotericsoftware.kryo.kryo5.io.Input;
import com.esotericsoftware.kryo.kryo5.io.Output;
import com.esotericsoftware.kryo.kryo5.util.Pool;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.service.StateMachineSerialisationService;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.pool.KryoCallback;
import com.esotericsoftware.kryo.pool.KryoFactory;
import com.esotericsoftware.kryo.pool.KryoPool;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Abstract base implementation for {@link StateMachineSerialisationService} using kryo.
Expand All @@ -42,115 +39,102 @@
*/
public abstract class AbstractKryoStateMachineSerialisationService<S, E> implements StateMachineSerialisationService<S, E> {

protected final KryoPool pool;

protected AbstractKryoStateMachineSerialisationService() {
KryoFactory factory = new KryoFactory() {

@Override
public Kryo create() {
Kryo kryo = new Kryo();
// kryo is really getting trouble checking things if class loaders
// doesn't match. for now just use below trick before we try
// to go fully on beans and get a bean class loader.
kryo.setClassLoader(ClassUtils.getDefaultClassLoader());
configureKryoInstance(kryo);
return kryo;
}
};
this.pool = new KryoPool.Builder(factory).softReferences().build();
}

@Override
public byte[] serialiseStateMachineContext(StateMachineContext<S, E> context) throws Exception {
return encode(context);
}

@SuppressWarnings("unchecked")
@Override
public StateMachineContext<S, E> deserialiseStateMachineContext(byte[] data) throws Exception {
return decode(data, StateMachineContext.class);
}

/**
* Subclasses implement this method to encode with Kryo.
*
* @param kryo the Kryo instance
* @param object the object to encode
* @param output the Kryo Output instance
*/
protected abstract void doEncode(Kryo kryo, Object object, Output output);

/**
* Subclasses implement this method to decode with Kryo.
*
* @param kryo the Kryo instance
* @param input the Kryo Input instance
* @param type the class of the decoded object
* @param <T> the type for decoded object
* @return the decoded object
*/
protected abstract <T> T doDecode(Kryo kryo, Input input, Class<T> type);

/**
* Subclasses implement this to configure the kryo instance.
* This is invoked on each new Kryo instance when it is created.
*
* @param kryo the kryo instance
*/
protected abstract void configureKryoInstance(Kryo kryo);

private byte[] encode(Object object) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
encode(object, bos);
byte[] bytes = bos.toByteArray();
bos.close();
return bytes;
}

private void encode(final Object object, OutputStream outputStream) throws IOException {
Assert.notNull(object, "cannot encode a null object");
Assert.notNull(outputStream, "'outputSteam' cannot be null");
final Output output = (outputStream instanceof Output ? (Output) outputStream : new Output(outputStream));
this.pool.run(new KryoCallback<Void>() {

@Override
public Void execute(Kryo kryo) {
doEncode(kryo, object, output);
return null;
}
});
output.close();
}

private <T> T decode(byte[] bytes, Class<T> type) throws IOException {
Assert.notNull(bytes, "'bytes' cannot be null");
final Input input = new Input(bytes);
try {
return decode(input, type);
}
finally {
input.close();
}
}

private <T> T decode(InputStream inputStream, final Class<T> type) throws IOException {
Assert.notNull(inputStream, "'inputStream' cannot be null");
Assert.notNull(type, "'type' cannot be null");
final Input input = (inputStream instanceof Input ? (Input) inputStream : new Input(inputStream));
T result = null;
try {
result = this.pool.run(new KryoCallback<T>(){

@Override
public T execute(Kryo kryo) {
return doDecode(kryo, input, type);
}
});
}
finally {
input.close();
}
return result;
}
protected final Pool<Kryo> pool;

protected AbstractKryoStateMachineSerialisationService() {
this.pool= new Pool<Kryo>(true,false,10) {
@Override
protected Kryo create() {
Kryo kryo=new Kryo();
kryo.setClassLoader(ClassUtils.getDefaultClassLoader());
configureKryoInstance(kryo);
return kryo;
}
};
}

@Override
public byte[] serialiseStateMachineContext(StateMachineContext<S, E> context) throws Exception {
return encode(context);
}

@SuppressWarnings("unchecked")
@Override
public StateMachineContext<S, E> deserialiseStateMachineContext(byte[] data) throws Exception {
return decode(data, StateMachineContext.class);
}

/**
* Subclasses implement this method to encode with Kryo.
*
* @param kryo the Kryo instance
* @param object the object to encode
* @param output the Kryo Output instance
*/
protected abstract void doEncode(Kryo kryo, Object object, Output output);

/**
* Subclasses implement this method to decode with Kryo.
*
* @param kryo the Kryo instance
* @param input the Kryo Input instance
* @param type the class of the decoded object
* @param <T> the type for decoded object
* @return the decoded object
*/
protected abstract <T> T doDecode(Kryo kryo, Input input, Class<T> type);

/**
* Subclasses implement this to configure the kryo instance.
* This is invoked on each new Kryo instance when it is created.
*
* @param kryo the kryo instance
*/
protected abstract void configureKryoInstance(Kryo kryo);

private byte[] encode(Object object) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
encode(object, bos);
byte[] bytes = bos.toByteArray();
bos.close();
return bytes;
}

private void encode(final Object object, OutputStream outputStream) throws IOException {
Assert.notNull(object, "cannot encode a null object");
Assert.notNull(outputStream, "'outputSteam' cannot be null");
final Output output = (outputStream instanceof Output ? (Output) outputStream : new Output(outputStream));
Kryo kryo=this.pool.obtain();
try{
doEncode(kryo,object,output);
}finally {
pool.free(kryo);
output.close();
}
}

private <T> T decode(byte[] bytes, Class<T> type) throws IOException {
Assert.notNull(bytes, "'bytes' cannot be null");
final Input input = new Input(bytes);
try {
return decode(input, type);
}
finally {
input.close();
}
}

private <T> T decode(InputStream inputStream, final Class<T> type) throws IOException {
Assert.notNull(inputStream, "'inputStream' cannot be null");
Assert.notNull(type, "'type' cannot be null");
final Input input = (inputStream instanceof Input ? (Input) inputStream : new Input(inputStream));
T result = null;
Kryo kryo=this.pool.obtain();
try{
return doDecode(kryo,input,type);
}finally {
pool.free(kryo);
input.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package org.springframework.statemachine.kryo;
/*
* Copyright 2017 the original author or authors.
*
Expand All @@ -13,17 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.statemachine.kryo;

import java.util.UUID;

import com.esotericsoftware.kryo.kryo5.Kryo;
import com.esotericsoftware.kryo.kryo5.io.Input;
import com.esotericsoftware.kryo.kryo5.io.Output;
import org.springframework.messaging.MessageHeaders;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.service.StateMachineSerialisationService;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.util.UUID;

/**
* Implementation for {@link StateMachineSerialisationService} using kryo.
Expand All @@ -35,20 +34,21 @@
*/
public class KryoStateMachineSerialisationService<S, E> extends AbstractKryoStateMachineSerialisationService<S, E> {

@Override
protected void doEncode(Kryo kryo, Object object, Output output) {
kryo.writeObject(output, object);
}
@Override
protected void doEncode(Kryo kryo, Object object, Output output) {
kryo.writeObject(output, object);
}

@Override
protected <T> T doDecode(Kryo kryo, Input input, Class<T> type) {
return kryo.readObject(input, type);
}
@Override
protected <T> T doDecode(Kryo kryo, Input input, Class<T> type) {
return kryo.readObject(input, type);
}

@Override
protected void configureKryoInstance(Kryo kryo) {
kryo.addDefaultSerializer(StateMachineContext.class, new StateMachineContextSerializer<S, E>());
kryo.addDefaultSerializer(MessageHeaders.class, new MessageHeadersSerializer());
kryo.addDefaultSerializer(UUID.class, new UUIDSerializer());
}
@Override
protected void configureKryoInstance(Kryo kryo) {
kryo.setRegistrationRequired(false);
kryo.addDefaultSerializer(StateMachineContext.class, new StateMachineContextSerializer<S, E>());
kryo.addDefaultSerializer(MessageHeaders.class, new MessageHeadersSerializer());
kryo.addDefaultSerializer(UUID.class, new UUIDSerializer());
}
}
Loading