ClassNotFound when passing serialized objects via Event Bus

465 views
Skip to first unread message

Pavel Ivashkov

unread,
Jun 4, 2014, 9:11:33 AM6/4/14
to ve...@googlegroups.com
Hello,

I'm facing a ClassNotFoundException when trying to deserialize object coming from event bus.

Here is a sample verticle code:
package proto;

import java.io.Serializable;

import org.apache.commons.lang3.SerializationUtils;
import org.apache.log4j.Logger;
import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.platform.Verticle;


public class MasterVerticle extends Verticle
{
    private static final Logger logger = Logger.getLogger(MasterVerticle.class);


    public static class MyTestClass implements Serializable
    {
        String field;
    }

    @Override
    public void start()
    {
        vertx.eventBus().registerHandler("addr", new Handler<Message<Buffer>>()
        {
            @Override
            public void handle(Message<Buffer> msg)
            {
                MyTestClass test = SerializationUtils.deserialize(msg.body().getBytes());
            }
        });

        vertx.setTimer(1000, new Handler<Long>()
        {
            @Override
            public void handle(Long event)
            {
                MyTestClass tes = new MyTestClass();
                tes.field = "test";
                vertx.eventBus().send("addr", new Buffer(SerializationUtils.serialize(tes)));
            }
        });
    }
}


Command line:

bin/vertx run proto.MasterVerticle -cp "../build/libs/clzloader-1.0.jar:../build/deps/*"



The error I'm getting:

Registering handler
Succeeded in deploying verticle
Sending message
class loader class org.vertx.java.platform.impl.ModuleClassLoader
Handling message
Exception in Java verticle
org.apache.commons.lang3.SerializationException: java.lang.ClassNotFoundException: proto.MasterVerticle$MyTestClass
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:230)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:268)
at proto.MasterVerticle$1.handle(MasterVerticle.java:35)
at proto.MasterVerticle$1.handle(MasterVerticle.java:26)
at org.vertx.java.core.eventbus.impl.DefaultEventBus$11.run(DefaultEventBus.java:943)
at org.vertx.java.core.impl.DefaultContext$3.run(DefaultContext.java:175)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:370)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: proto.MasterVerticle$MyTestClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:626)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1613)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at org.apache.commons.lang3.SerializationUtils.deserialize(SerializationUtils.java:224)
... 9 more

What could be the cause of this?

- pavel

Paulo Lopes

unread,
Jun 4, 2014, 9:25:00 AM6/4/14
to ve...@googlegroups.com
you need to add the class file you are trying to deserialize to all your modules, this is not a vert.x issue it is just how java serialization works...

Pavel Ivashkov

unread,
Jun 4, 2014, 9:41:36 AM6/4/14
to ve...@googlegroups.com
Paulo, look at the sample -- there is single module with single verticle. I'm passing serialized object through event bus into the same verticle.

- pavel

Nick Scavelli

unread,
Jun 4, 2014, 2:07:38 PM6/4/14
to ve...@googlegroups.com
Try making it a module. Running raw verticles is really for very simple scenarios.

Pavel Ivashkov

unread,
Jun 5, 2014, 11:46:44 AM6/5/14
to ve...@googlegroups.com
Running this sample as a module fixes the issue. Thank you.
Reply all
Reply to author
Forward
0 new messages