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

Lambda serialization problem #469

Closed
andre-paris opened this issue Oct 21, 2016 · 2 comments
Closed

Lambda serialization problem #469

andre-paris opened this issue Oct 21, 2016 · 2 comments

Comments

@andre-paris
Copy link

I'm using Kryo for serialization inside Hazelcast.
The lambda is sent like this:

Hazelcast.newHazelcastInstance()
                .getExecutorService("exec")
                .executeOnKeyOwner(() -> {
                    // some code on Runnable.run()
                });

Serializer read this way:

public Object read(ObjectDataInput in) throws IOException {
    Kryo kryo = new Kryo();
    kryo.register(SerializedLambda.class);
    kryo.register(Closure.class, new ClosureSerializer());

    Input input = new Input((InputStream) in);

    return kryo.readClassAndObject(input);
}

I got this error:

Caused by: java.lang.RuntimeException: Could not serialize lambda
    at com.esotericsoftware.kryo.serializers.ClosureSerializer.write(ClosureSerializer.java:67) ~[kryo-4.0.0.jar:na]
    at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:651) ~[kryo-4.0.0.jar:na]
    at com.paris.sandbox.HazelcastConfig$KryoSerializer.write(HazelcastConfig.java:94) ~[classes/:na]
    at com.hazelcast.internal.serialization.impl.StreamSerializerAdapter.write(StreamSerializerAdapter.java:41) ~[hazelcast-3.7.2.jar:3.7.2]
    at com.hazelcast.internal.serialization.impl.AbstractSerializationService.writeObject(AbstractSerializationService.java:199) ~[hazelcast-3.7.2.jar:3.7.2]
    ... 77 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.paris.sandbox.HazelcastRest$$Lambda$17/70747700.writeReplace()
    at java.lang.Class.getDeclaredMethod(Class.java:2130) ~[na:1.8.0_91]
    at com.esotericsoftware.kryo.serializers.ClosureSerializer.write(ClosureSerializer.java:58) ~[kryo-4.0.0.jar:na]
    ... 81 common frames omitted

I'm missing something?

Thanks

@jerrinot
Copy link

jerrinot commented Dec 28, 2016

Hi @andre-paris,

I believe Kryo requires lambdas to be marked as Serializable. Can you try to change it to

Hazelcast.newHazelcastInstance()
                .getExecutorService("exec")
                .executeOnKeyOwner((Runnable & Serializable) () -> { // <--- instruct lambda factory to create serializable lambdas
                    // some code on Runnable.run()
                }, "key");

It will use Kryo, but the the regular Java serialization. Marking it as (Java) serializable is necessary as otherwise generated lambda classes won't have all necessary methods.

@magro
Copy link
Collaborator

magro commented Jul 13, 2017

@jerrinot thanks for stepping in! I assume this resolved the issue for @andre-paris.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants