Akka 2 future example problem

1,815 views
Skip to first unread message

Mic

unread,
May 6, 2012, 5:42:12 AM5/6/12
to akka...@googlegroups.com
Hello, 
I am not able to compile the AKKA 2 example http://doc.akka.io/docs/akka/2.0/scala/futures.html#Use_Directly 

import akka.dispatch.Await
import akka.dispatch.Future
import akka.util.duration._


object HelloWorldFuture {
  def main(args: Array[String]) {
    val future = Future {
      "Hello" + "World"
    }
    val result = Await.result(future, 1 second)
  }
 

$ fsc -cp ../jars/akka/lib/akka/akka-actor-2.0.1.jar:. HelloWorldFuture.scala
HelloWorldFuture.scala:8: error: could not find implicit value for parameter executor: akka.dispatch.ExecutionContext
    val future = Future {
                        ^
one error found


What did I do wrong?

Thank you in advance.

Mic

Viktor Klang

unread,
May 6, 2012, 8:05:18 AM5/6/12
to akka...@googlegroups.com
Hi Mic,

You didn't read the docs :-)


Cheers,
 

Thank you in advance.

Mic

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/1d4UMkNW8UQJ.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Mic

unread,
May 6, 2012, 8:13:22 AM5/6/12
to akka...@googlegroups.com
Hi Vic,
I read the docs and the example is from the docs, but unfortunately I did not understand the docs.

Mic
Hi Mic,


Thank you in advance.
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.
Hi Mic,


Thank you in advance.
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.
Hi Mic,


Thank you in advance.
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.

√iktor Ҡlang

unread,
May 6, 2012, 8:15:00 AM5/6/12
to akka...@googlegroups.com
Hi Mic,

If you tell me which part of the following you do not understand, then I'll try to elaborate:

"

Execution Contexts

In order to execute callbacks and operations, Futures need something called an ExecutionContext, which is very similar to a java.util.concurrent.Executor. if you have an ActorSystem in scope, it will use its default dispatcher as the ExecutionContext, or you can use the factory methods provided by the ExecutionContextcompanion object to wrap Executors and ExecutorServices, or even create your own.

  1. import akka.dispatch.{ ExecutionContext, Promise }
  2.  
  3. implicit val ec = ExecutionContext.fromExecutorService(yourExecutorServiceGoesHere)
  4.  
  5. // Do stuff with your brand new shiny ExecutionContext
  6. val f = Promise.successful("foo")
  7.  
  8. // Then shut your ExecutionContext down at some
  9. // appropriate place in your program/application
  10. ec.shutdown()

Cheers,

To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/FOazw6cJVpgJ.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

Mic

unread,
May 6, 2012, 10:01:07 AM5/6/12
to akka...@googlegroups.com
Thank you, the following complete code runs:

import akka.dispatch.{Await, Future, ExecutionContext} 
import akka.util.duration._ 
import java.util.concurrent.Executors 

object HelloWorldFuture {
  def main(args: Array[String]) {
    val pool = Executors.newCachedThreadPool() 
    implicit val ec = ExecutionContext.fromExecutorService(pool) 
    val future = Future {
      "Hello" + "World"
    }
    val result = Await.result(future, 1 second)
    println(result)
    pool.shutdown()
  }

I have found the Actor doc better, because on the end there was a complete code. 

Mic

√iktor Ҡlang

unread,
May 6, 2012, 10:30:37 AM5/6/12
to akka...@googlegroups.com
Hi Mic,

You'll want to avoid doing Await, it does do blocking, and blocking is bad.

    implicit val ec = ExecutionContext.fromExecutorService(Executors.newCachedThreadPool()
    Future { "Hello" + "World" } map println andThen { case _ => ec.shutdown }

The code above will run "Hello" + "World" async, then the println async and then shut your ExecutionContext down when all is done.

Cheers,

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/f-2AIM4QU_EJ.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Mic

unread,
May 6, 2012, 7:49:33 PM5/6/12
to akka...@googlegroups.com
Hi Viktor,
Thank you. How to improve the output from the following code:
import akka.dispatch.{Future, ExecutionContext} 
import akka.util.duration._ 
import java.util.concurrent.Executors 

object Communication {
  def main(args: Array[String]) {
 implicit val ec = ExecutionContext.fromExecutorService(
     Executors.newCachedThreadPool()); 
 // send tasks to the dispather
 val results = Future.traverse(List.range(0,10)){i => 
   Future("%d x %d = %d" format (i, i, i*i))
 } map println andThen { case _ => ec.shutdown }
  
  }
}
Current OUTPUT:
List(0 x 0 = 0, 1 x 1 = 1, 2 x 2 = 4, 3 x 3 = 9, 4 x 4 = 16, 5 x 5 = 25, 6 x 6 = 36, 7 x 7 = 49, 8 x 8 = 64, 9 x 9 = 81)

Thank you in advance.

Mic
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

√iktor Ҡlang

unread,
May 6, 2012, 7:53:47 PM5/6/12
to akka...@googlegroups.com
Hi Mic,

On Mon, May 7, 2012 at 1:49 AM, Mic <mict...@gmail.com> wrote:
Hi Viktor,
Thank you. How to improve the output from the following code:

How do I improve something without being given a metric?

Cheers,
 
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/kxwpgm6EPNgJ.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Mic

unread,
May 6, 2012, 8:03:33 PM5/6/12
to akka...@googlegroups.com
Hi Viktor,
I am sorry. I meant to get something like this:
0 x 0 = 0
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81

Cheers,
Mic

Roland Kuhn

unread,
May 7, 2012, 2:46:13 AM5/7/12
to akka...@googlegroups.com
Hi Mic,

you might be interested in searching for mkString here: http://www.scala-lang.org/api/current/index.html#scala.collection.Seq

Regards,

Roland

To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/nxeatUyc_G0J.

To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


Mic

unread,
May 7, 2012, 9:33:05 AM5/7/12
to akka...@googlegroups.com
Hi Roland,
I tried it out unsuccessful with the following code:
import akka.dispatch.{Future, ExecutionContext} 
import akka.util.duration._ 
import java.util.concurrent.Executors 

object Communication {
  def main(args: Array[String]) {
 implicit val ec = ExecutionContext.fromExecutorService(
     Executors.newCachedThreadPool()); 
 // send tasks to the dispather
 val results = Future.traverse(List.range(0,10)){i => 
   Future("%d x %d = %d" format (i, i, i*i))
 } map(_.mkString("\n")) println andThen { case _ => ec.shutdown }
   
  }
}

What did I do wrong?

Thank you in advance.

Mic

√iktor Ҡlang

unread,
May 7, 2012, 9:40:12 AM5/7/12
to akka...@googlegroups.com
On Mon, May 7, 2012 at 3:33 PM, Mic <mict...@gmail.com> wrote:
Hi Roland,
I tried it out unsuccessful with the following code:
import akka.dispatch.{Future, ExecutionContext} 
import akka.util.duration._ 
import java.util.concurrent.Executors 

object Communication {
  def main(args: Array[String]) {
 implicit val ec = ExecutionContext.fromExecutorService(
     Executors.newCachedThreadPool()); 
 // send tasks to the dispather
 val results = Future.traverse(List.range(0,10)){i => 
   Future("%d x %d = %d" format (i, i, i*i))
 } map(_.mkString("\n")) println andThen { case _ => ec.shutdown }
   
  }
}

What did I do wrong?

Usually when one reports a problem, they include what the actual problem was.

Follow the Scala compiler:

<console>:19: error: value println is not a member of akka.dispatch.Future[String]
           } map(_.mkString("\n")) println andThen { case _ => ec.shutdown }
 
Cheers,
Message has been deleted

Mic

unread,
May 7, 2012, 10:14:32 PM5/7/12
to akka...@googlegroups.com
Hi Viktor,
I am sorry, but Scala-IDE only print out:
Error: Could not find or load main class Communication

In the future I will provide scalac direct output.

Is the following solution a good solution:

import akka.dispatch.{Future, ExecutionContext} 
import akka.util.duration._ 
import java.util.concurrent.Executors 

object Communication {
  def main(args: Array[String]) {
  implicit val ec = ExecutionContext.fromExecutorService(
      Executors.newCachedThreadPool()); 
  // send tasks to the dispather
  val results = Future.traverse(List.range(0,10)){i => 
    Future("%d x %d = %d" format (i, i, i*i))
  } andThen { case _ => ec.shutdown }
  
  results.foreach{ res =>
    println(res.mkString("\n"))
  }   
  }
}

Output:
0 x 0 = 0
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81

Thank you in advance.

Mic

Jonas Boner

unread,
May 11, 2012, 8:24:25 AM5/11/12
to akka...@googlegroups.com
Hi Mic.

This is a question for the Scala IDE docs I think. Nothing to do with Akka.

/Jonas
>>>>>>>> akka-user+...@googlegroups.com.
>>>>>>>> For more options, visit this group at
>>>>>>>> http://groups.google.com/group/akka-user?hl=en.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Viktor Klang
>>>>>>>
>>>>>>> Akka Tech Lead
>>>>>>> Typesafe - The software stack for applications that scale
>>>>>>>
>>>>>>> Twitter: @viktorklang
>>>>>>>
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Akka User List" group.
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msg/akka-user/-/kxwpgm6EPNgJ.
>>>>>>
>>>>>> To post to this group, send email to akka...@googlegroups.com.
>>>>>> To unsubscribe from this group, send email to
> --
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To post to this group, send email to akka...@googlegroups.com.
> To unsubscribe from this group, send email to
> akka-user+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/akka-user?hl=en.



--
Jonas Bonér
CTO
Typesafe - The software stack for applications that scale
Phone: +46 733 777 123
Twitter: @jboner
Reply all
Reply to author
Forward
0 new messages