0

I am new to Scala Akka actor. Based on the akka tutorial example on their website,i coded a similar example with just printing messages with the time they started. But the code does not get executed in my eclipse IDE.

import akka.actor.Actor
import akka.actor.ActorRef
import scala.collection.mutable.ListBuffer
import akka.actor.Props
import akka.routing.RoundRobinRouter
import akka.actor.ActorSystem

object AkaObj extends App{


process()

sealed trait PiMessage
case class Work(value:String) extends PiMessage
case class printComp(valu:String)extends PiMessage
case object Start extends PiMessage 
case class ListObj(cont:Seq[String]) extends PiMessage


class Worker extends Actor{

  def receive()={

    case Work(value:String)=>
      println(value)
      sender ! printComp(value)
  }
}

class Master(listener: ActorRef) extends Actor{

  val nrOfWorkers = 10
  var counter = 0
  var lis = ListBuffer[String]()

  val workerRouter = context.actorOf(
  Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")

  def receive()={
    //handle message
    case Start=>workerRouter ! Work("Start now " + System.currentTimeMillis())
    case printComp(value:String)=> { 
      counter+=1
      lis.append(counter+"-"+value)
      if(counter >=10)
          listener ! ListObj(lis.toSeq)

      // Stops this actor and all its supervised children
      context.stop(self)
      }
  }
 }

class Listener extends Actor{

  def receive()={

    case ListObj(cont:Seq[String])=>
      println("Completed " +cont)
      context.system.shutdown()
  }
}

def process(){
  // Create an Akka system
    val system = ActorSystem("PiSystem")

  // create the result listener, which will print the result and shutdown the system
    val listener = system.actorOf(Props[Listener], name = "listener")

  // create the master
    val master = system.actorOf(Props(new Master(listener)),
      name = "master")

  // start the calculation
    master ! Start
}

}

Issue: The error message i get is: "Error: Could not find or load main class AkaObj" It does not even compiling i think. When i run the project,that's the message i get.

Balaram26
  • 1,349
  • 3
  • 15
  • 32

2 Answers2

0

Actually, I managed to run your code just after dependencies were resolved. So the problem is in your environment configuration. I use Intellij Idea IDE.

Try this Eclipse Error: Could not find or load main class

Community
  • 1
  • 1
ka4eli
  • 5,294
  • 3
  • 23
  • 43
  • I tested the code in Scala cmd in Windows. Still i get the same error. So i think the issue could not be with Eclipse or the dependencies. – Balaram26 Apr 22 '14 at 16:53
0

The actual problem is the use of object AkaObj extends App. When programming scala I have noticed that using scala.App is restricted to simple programs (at least in IDEs). As soon as you start defining objects and classes inside scala.App Eclipse starts complaining about not finding the entry point of the program.

Try to fix this as follows:

import akka.actor.Actor
import akka.actor.ActorRef
import scala.collection.mutable.ListBuffer
import akka.actor.Props
import akka.routing.RoundRobinRouter
import akka.actor.ActorSystem

object AkaObj {

  def main(args:Array[String]) {
    process()
  }

  def process() {
    // Create an Akka system
      val system = ActorSystem("PiSystem")

    // create the result listener, which will print the result and shutdown the system
      val listener = system.actorOf(Props[Listener], name = "listener")

    // create the master
      val master = system.actorOf(Props(new Master(listener)),
        name = "master")

    // start the calculation
      master ! Start
  }

  sealed trait PiMessage
  case class Work(value:String) extends PiMessage
  case class printComp(valu:String)extends PiMessage
  case object Start extends PiMessage 
  case class ListObj(cont:Seq[String]) extends PiMessage


  class Worker extends Actor{

    def receive()={

      case Work(value:String)=>
        println(value)
        sender ! printComp(value)
    }
  }

  class Master(listener: ActorRef) extends Actor{

    val nrOfWorkers = 10
    var counter = 0
    var lis = ListBuffer[String]()

    val workerRouter = context.actorOf(
    Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")

    def receive()={
      //handle message
      case Start=>workerRouter ! Work("Start now " + System.currentTimeMillis())
      case printComp(value:String)=> { 
        counter+=1
        lis.append(counter+"-"+value)
        if(counter >=10)
            listener ! ListObj(lis.toSeq)

        // Stops this actor and all its supervised children
        context.stop(self)
        }
    }
  }

  class Listener extends Actor{

    def receive()={

      case ListObj(cont:Seq[String])=>
        println("Completed " +cont)
        context.system.shutdown()
    }
  } 
}

Using def main(args:Array[String]) fixes the problem in most cases.

lSoleyl
  • 309
  • 1
  • 7