为什么需要使用Scala RemoteActors设置类加载器? [英] Why is setting the classloader necessary with Scala RemoteActors?

查看:69
本文介绍了为什么需要使用Scala RemoteActors设置类加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用Scala RemoteActors时,我得到了一个引用scala.actors.remote.NetKernel的ClassNotFoundException。我复制了其他人的示例,并在Actor中添加了 RemoteActor.classLoader = getClass.getClassLoader ,现在一切正常。为什么需要这样做?

When using Scala RemoteActors I was getting a ClassNotFoundException that referred to scala.actors.remote.NetKernel. I copied someone else's example and added RemoteActor.classLoader = getClass.getClassLoader to my Actor and now everything works. Why is this necessary?

推荐答案

远程Actor使用Java序列化来回发送消息。在actors库中,您将找到一个自定义对象输入流( https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/actors/scala/actors/remote/JavaSerializer.scala )用于将对象与套接字串行化。还有一些路由代码和其他魔术。

Remote Actors use Java serialization to send messages back and forth. Inside the actors library, you'll find a custom object input stream ( https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/actors/scala/actors/remote/JavaSerializer.scala ) that is used to serialize objects to/from a socket. There's also some routing code and other magic.

无论如何,用于远程处理的ClassLoader相当重要。如果您不熟悉Java RMI,建议您查找它。无论如何,Scala在序列化/反序列化actor时选择的ClassLoader是位于RemoteActor上的ClassLoader,默认为null。

In any case, the ClassLoader used for remoting is rather important. I'd recommend looking up Java RMI if you're unfamiliar with it. In any case, the ClassLoader that Scala picks when serializing/deserializing actors is the one Located on RemoteActor which defaults to null.

这意味着默认情况下,如果不指定ClassLoader,您将感到不满意;)。

This means that by default, you will be unhappy without specifying a ClassLoader ;).

在控制类加载器(例如OSGi)的环境中,您需要确保将此值设置为可以访问 all 序列化角色使用的所有类的类加载器。

If you were in an environment that controls classloaders, such as OSGi, you'd want to make sure you set this value to a classloader that has access to all classes used by all serialized actors.

希望有帮助!

这篇关于为什么需要使用Scala RemoteActors设置类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆