Code not running when main method is added - scala

I',m following the akka tutorial from http://doc.akka.io/docs/akka/2.2.0/AkkaScala.pdf and below is the HelloWorld program. The main method is added by me but the I do not receive the "run as scala application" in Eclipse when I attempt to run it. According to the doc to run the program from the command line : "java -classpath akka.Main com.example.HelloWorld" but I require to run it from Eclipse so have added my own main method. Why will the below code not run ?
import akka.actor.Actor
import akka.actor.Props
class HelloWorld extends Actor {
override def preStart(): Unit = {
// create the greeter actor
val greeter = context.actorOf(Props[Greeter], "greeter")
// tell it to perform the greeting
greeter ! Greeter.Greet
}
def receive = {
// when the greeter is done, stop this actor and with it the application
case Greeter.Done => context.stop(self)
}
object Greeter {
def main(args: Array[String]) {
new HelloWorld
}
case object Greet
case object Done
}
class Greeter extends Actor {
def receive = {
case Greeter.Greet =>
println("Hello World!")
sender ! Greeter.Done
}
}
}

A main method has to be within a standalone object. Here you have it within an object within a class.

Related

Why args are compulsory for main

If I use like below considering I don't need to take arguments, it doesn't detect for Scala in eclipse.
object HelloWorld {
def main(): Unit = {
println("Hello Scala!!!")
}
}
It works fine with args: Array[String]
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello Scala!!!")
}
}
Well it's simply a convention on the JVM. You won't be able to invoke your object as entry point when running your program. For example, in Scala.js you have main() without arguments.
If you don't need the arguments you can mixin the App trait:
object HelloWorld extends App {
println("Hello Scala!!!")
}

How to test controllers that has akka system injected for Play framework?

Below is my controller:
package controllers
import java.util.TimeZone
import akka.actor.{ActorNotFound, ActorSystem}
import akka.util.Timeout
import com.google.inject.Inject
import com.typesafe.akka.extension.quartz.QuartzSchedulerExtension
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.{JsValue, JsError, JsSuccess}
import play.api.mvc._
import scala.concurrent.Future
import scala.concurrent.duration._
class ScheduleController #Inject()(system: ActorSystem) extends Controller {
val scheduler = QuartzSchedulerExtension.get(system)
implicit val timeout = new Timeout(5.seconds)
def index = Action.async {
Future.successful(Ok("hi"))
}
def run = Action.async { request =>
// find actor and "start" it by sending message to it, return Ok() if found
// if actor is not found, return BadRequest()
// if error return InternalServerError()
}
}
I've been looking for tutorials but most are outdated since they deal with Play 2.3.
I solved a very similar problem with Play2 controllers and actors except in my case I'm not injecting an ActorSystem into my controller. Rather, I use Play's default actor system and create actors from my global settings object when my application starts. For a given actor I provide a selection method in its companion object. For example:
class MyActor extends Actor {
def receive = {
case Request => sender ! Response
}
}
object MyActor {
val path = // assuming fixed actor path
val sys = // reference to play actor system
def select: ActorSelection = sys.actorSelection(path)
// my actor messages
case object Request
case object Response
}
The way I have this actor setup I plan on asking it from inside my controller method using a Request and expect to process a Response object back from it. The controller is a non-actor sender in this scenario. By calling ask on the actor selection, I close over the future and map the result to a partial function that matches the expected response. Here's what my controller looks like:
class MyController extends Controller {
implicit val timeout = // set your timeout duration
def index = Action.async {
MyActor.select ? Request map {
case Response => Ok("success")
}
}
}
Testing this controller endpoint is VERY challenging because your actor might not be in a ready state to start handling messages. I spent about two days trying to figure out how to properly block on the my actor's lifecycle state using the actor selection I've exposed through its companion object. What I ended up doing inside my test class that worked for me was to create a method that takes in a function parameter and loops over my actor state until I can resolve its ActorRef. I'm a fan of ScalaTest so this is what my unit tests amounted to:
class MySpec extends FlatSpec with Matchers with BeforeAndAfterAll {
implicit val fakeApp: FakeApplication = new FakeApplication()
override def beforeAll() = Play.start(fakeApp)
override def afterAll() = Play.stop(fakeApp)
def awaitableActorTest(assertions: => Any): Unit = {
val timeout = 1.second
var isActorReady = false
while(!isActorReady) {
val futureRef = Await.ready(MyActor.select.resolveOne(timeout), timeout)
futureRef.value.get match {
case Success(_) =>
assertions
isActorReady = true
case Failure(_) =>
Thread.sleep(2000)
}
}
}
it should "process actor request and response via controller endpoint" in {
awaitableActorTest {
val result = route(routes.MyController.index).get
status(result) shouldBe OK
contentAsString(result) shouldBe "success"
}
}
}
What I get out of this awaitableActorTest pattern is a really clean way of reliably hitting my controller endpoint only when my actor is alive and available to process messages. If it's not, my ActorRef using its selection companion won't resolve in the future with success -- instead it completes with failure. When it fails I sleep a few seconds and repeat the loop. I break the loop when I its selection successfully resolved an ActorRef.
I'm unfamiliar with the QuartzSchedulerExtension to which you refer in your code snippet, but I hope my example is useful.

How to mock using external call in Akka Actor using ScalaTest

I am new to entire ecosystem including Scala, Akka and ScalaTest
I am working on a problem where my Actor gives call to external system.
case object LogProcessRequest
class LProcessor extends Actor {
val log = Logging(context.system, this)
def receive = {
case LogProcessRequest =>
log.debug("starting log processing")
LogReaderDisruptor main(Array())
}
}
The LogReaderDisruptor main(Array()) is a Java class that does many other things.
The test I have currently looks like
class LProcessorSpec extends UnitTestSpec("testSystem") {
"A mocked log processor" should {
"be called" in {
val logProcessorActor = system.actorOf(Props[LProcessor])
logProcessorActor ! LogProcessRequest
}
}
}
where UnitTestSpec looks like (and inspired from here)
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.matchers.MustMatchers
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}
abstract class UnitTestSpec(name: String)
extends TestKit(ActorSystem(name))
with WordSpecLike
with MustMatchers
with BeforeAndAfterAll
with ImplicitSender {
override def afterAll() {
system.shutdown()
}
}
Question
How can I mock the call to LogReaderDisruptor main(Array()) and verify that it was called?
I am coming from Java, JUnit, Mockito land and something that I would have done here would be
doNothing().when(logReaderDisruptor).main(Matchers.<String>anyVararg())
verify(logReaderDisruptor, times(1)).main(Matchers.<String>anyVararg())
I am not sure how to translate that with ScalaTest here.
Also, This code may not be idiomatic, since I am very new and learning
There are a few ways to do this. The kind of OO way is to wrap logDisrupter as an object and pass it in. I would set up a companion object to instantiate the actor. Something like below. Then you can pass alternate implementation. You can also achieve a similar approach by using traits and mixing in an alternative logDisrupter only as needed.
object LProcessor {
def props(logDisrupter : LogDisrupter) = Props(new LProcessor(logDisrupter))
}
class LProcessor(logDisrupter : LogDisrupter) extends Actor {
val log = Logging(context.system, this)
def receive = {
case LogProcessRequest =>
log.debug("starting log processing")
logDisrupter.doSomething();
}
}
Then instatiate as
val logProcessorActor = system.actorOf(LProcessor.props(logDisrupter))

Creating a TestActorRef results in NullPointerException

I am trying to get a TestActorRef like that
class NotifySenderTest(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfter {
def this() = this(ActorSystem("NotifySenderTest"))
override def afterAll {
TestKit.shutdownActorSystem(system)
}
"A NotifySender" must {
"be able to process the required messages" in {
val actorRef = TestActorRef[NotifySender] //Line 92
}
}
the this actor
class NotifySender extends Actor with Stash {
import Tcp._
import context.system
def receive = {
[...]
}
}
But this leaves me with the following stacktrace
java.lang.NullPointerException: at
akka.actor.dungeon.Dispatch$class.init(Dispatch.scala:62) at
akka.actor.ActorCell.init(ActorCell.scala:338) at
akka.actor.LocalActorRef.(ActorRef.scala:304) at
akka.testkit.TestActorRef.(TestActorRef.scala:21) at
akka.testkit.TestActorRef$.apply(TestActorRef.scala:141) at
akka.testkit.TestActorRef$.apply(TestActorRef.scala:137) at
akka.testkit.TestActorRef$.apply(TestActorRef.scala:146) at
akka.testkit.TestActorRef$.apply(TestActorRef.scala:144) at
actor.NotifySenderTest$$anonfun$2$$anonfun$apply$mcV$sp$4.apply$mcV$sp(NotifySenderTest.scala:92)
at
actor.NotifySenderTest$$anonfun$2$$anonfun$apply$mcV$sp$4.apply(NotifySenderTest.scala:91)
...
Edit: It seems to have something to do with this actor in particular. Getting a TestActorRef to another actor class is working correctly. I read that there was a problem with TextActorRefs for actors that have the Stash trait, but this was said to be resolved in the current version. (Reference)
Edit2: Ok. I was wrong. The current release is not 2.3. So I have to wait?!
Verified that upgrading to akka 2.3.0 is the correct answer for fixing TestActorRef with the Stash trait.
Instantiate the actor:
val actorRef = TestActorRef(new NotifySender())
That's how I always go about it anyway. :)

Scala remote actors on same machine

I am new to scala and trying to use the Actor model.
I have worked my way with using actors on the same machine.
Now, I want to use remote actors to go a step further.
As I have only one box to play with, I am planning to start a scala process which will act as a remote actor
remote.scala looks like
import scala.actors._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._
import Actor._
import scala.math._
class remoteActor extends Actor{
def act(){
alive(9010)
register('myActor, self)
while (true)
{
println("Started Remote Actor")
receive {
case (caller :Actor, index :Int, length :Int) =>
{ // Do some stuff
}
}
}
}
}
object hello {
def main(args: Array[String]): Unit = {
println("Hello")
val act = new remoteActor
act.start
}
}
The main program which will use this remote actor
actor.scala
import scala.actors._
import scala.actors.remote._
import scala.actors.remote.RemoteActor._
import Actor._
class masterActor extends Actor{
def act()
{
val myRemoteActor = select(Node("localhost", 9010), 'myActor)
myRemoteActor ! (self,3,2)
}
}
object hello {
def main(args: Array[String]): Unit = {
val sample = new masterActor
sample.start
}
}
The problem is when I run remote.scala using eclipse (by installing the scala plugin for eclipse) I get the
Hello
Started Remote Actor
message printed in the eclipse console.
But when I run the same program from Windows command line after installing scala.
C:\Program Files (x86)\scala\bin>scala remote.scala
then the messages do not get printed. However, if I change the remote.scala file to
just include the hello world message, (i.e. remote.scala looks like)
object hello {
def main(args: Array[String]): Unit = {
println("Hello")
}
}
then the Hello message gets printed on the Windows command prompt.
Why is Windows command prompt not printing the messages in case of original remote.scala?
What I want is to start this scala process where the remote actor is registered and the process is still alive. I then want to start the main.scala program from another command prompt so that it sends messages to the remote actor to perform the computations.
How can I make sure that the remote actor is still alive and registered and the process is running?
Don't include the actor reference in your message. The receiving pattern should be:
receive {
case (index :Int, length :Int) =>
{ // Do some stuff
}
}
And the message should be sent as myRemoteActor ! (3,2)
In the receiver you can just write sender to refer to the original message sender. There are other useful functions, e.g. reply(msg) - replies to the original sender with msg