开发者

a scala remote actor exception

开发者 https://www.devze.com 2022-12-28 18:21 出处:网络
i with a scala code like this for echo service. import scala.actors.Actor import scala.actors.Actor._ import scala.actors.remote.RemoteActor._

i with a scala code like this for echo service.

import scala.actors.Actor import scala.actors.Actor._ import scala.actors.remote.RemoteActor._

class Echo extends Actor {
   def act() {
        alive(9010)
        register('myName, self)

     loop {
       react {
            case msg  =>  println(msg)
       }
     }开发者_JAVA百科
   }
}
object EchoServer  {

   def main(args: Array[String]): unit = {
        val echo = new Echo
        echo.start
        println("Echo server started")
   }
} 
EchoServer.main(null)

but there has some exception.

java.lang.NoClassDefFoundError: Main$$anon$1$Echo$$anonfun$act$1
 at Main$$anon$1$Echo.act((virtual file):16)
 at scala.actors.Reaction.run(Reaction.scala:76)
 at scala.actors.Actor$$anonfun$start$1.apply(Actor.scala:785)
 at scala.actors.Actor$$anonfun$start$1.apply(Actor.scala:783)
 at scala.actors.FJTaskScheduler2$$anon$1.run(FJTaskScheduler2.scala:160)
 at scala.actors.FJTask$Wrap.run(Unknown Source)
 at scala.actors.FJTaskRunner.scanWhileIdling(Unknown Source)
 at scala.actors.FJTaskRunner.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Main$$anon$1$Echo$$anonfun$act$1
 at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
 ... 8 more

i don't konw how can cause it. by the way .my scala version is 2.7.5


ClassNotFoundException indicates that something was probably not compiled, that should have been compiled. How did you compile it? Manually using scalac?

Try the following rm *.class scalac *.scala scala EchoServer.

The following works:


EchoServer.scala

import scala.actors.Actor
import scala.actors.Actor._
import scala.actors.remote.RemoteActor._

class Echo extends Actor {
   def act() {
        alive(9010)
        register('myName, self)

     loop {
       react {
            case msg  =>  println(msg)
       }
     }
   }
}

object EchoServer  {

   def main(args: Array[String]): unit = {
        val echo = new Echo
        echo.start
        println("Echo server started")
   }
} 

Client.scala

import scala.actors.Actor._
import scala.actors.remote.Node
import scala.actors.remote.RemoteActor._

object Client extends Application {
    override def main(args: Array[String]) {
        if (args.length < 1) {
            println("Usage: scala Client [msg]")
            return
        }

        actor {
            val remoteActor = select(Node("localhost", 9010), 'myName)

            remoteActor !? args(0) match {
                case msg => println( "Server's response is [" + msg + "]" )
            }
        }
    }
}

Command line:

rm *.class && scalac *.scala  && scala EchoServer

And in other terminal:

scala Client hello


You need to set the classloader on the remote actors.

Before the act() method, add the line:

RemoteActor.classLoader = getClass.getClassLoader

Why is setting the classloader necessary with Scala RemoteActors?

0

精彩评论

暂无评论...
验证码 换一张
取 消