Your SlideShare is downloading. ×
Actor Based Asyncronous IO in Akka
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Actor Based Asyncronous IO in Akka

3,695
views

Published on

Asynchronous IO is one of the most important building blocks when designing high-performance systems. Over the years various patterns emerged on top of the selector based services provided by the …

Asynchronous IO is one of the most important building blocks when designing high-performance systems. Over the years various patterns emerged on top of the selector based services provided by the operating system. In this talk I will give a quick overview of the most important asynchronous IO patterns from callbacks to iteratees. Finally I will show how these approaches map to the actor world, introducing the new IO model designed by the Akka and Spray team available in Akka 2.2.

Published in: Technology, Business

0 Comments
23 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,695
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
23
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ––
  • 2. ––––
  • 3. ––
  • 4. while (hasData) {output.write(someData) // Blocks until complete}
  • 5. while (input.isOpen) {val event = input.read() // Blocks until event arriveshandleEvent(event) // Dispatch event}
  • 6. ––
  • 7. output.write(someData).onComplete { success =>// What should I do here???}
  • 8.
  • 9. –––
  • 10. ––
  • 11. 
  • 12. class SimpleSender(remote: InetSocketAddress)extends Actor {IO(Udp) ! Udp.SimpleSender // Acquire a managerdef receive = {case Udp.SimpleSenderReady ⇒context.become(ready(sender))}def ready(socket: ActorRef): Receive = {case msg: String ⇒socket ! Udp.Send(ByteString(msg), remote)}}
  • 13. IO(Udp) ! Udp.Bind(self,new InetSocketAddress("localhost", 53))def receive = {case Udp.Bound(local) ⇒context.become(ready(sender))}def ready(socket: ActorRef): Receive = {case Udp.Received(data, remote) ⇒// Process data}
  • 14. –
  • 15. –––
  • 16. –––
  • 17. def receive = {case SendIt(data) ⇒buffer(data)connection ! Write(data, Ack)context.become({case SendIt(data) ⇒ buffer(data)case Ack ⇒// Start serving from buffer …}, discardOld = false)case PeerClosed ⇒ context stop self}
  • 18. ––––
  • 19. ––––
  • 20. class MyStageextends PipelineStage[Ctxt, CmdAbove, CmdBelow, EvtAbove, EvtBelow]
  • 21. class MyStageextends PipelineStage[Ctxt, CmdAbove, CmdBelow, EvtAbove, EvtBelow]
  • 22. override def apply(ctx: PipelineContext) = new PipePair[CmdAbove,CmdBelow, EvtAbove, EvtBelow] {override val commandPipeline = {cmdAbove: CmdAbove ⇒ ctx.singleCommand(someCmdBelow)}override val eventPipeline = {evtBelow: EvtBelow ⇒ ctx.singleEvent(someEvtAbove)}}
  • 23. val init = TcpPipelineHandler.withLogger(log,new StringByteStringAdapter("utf-8") >>new DelimiterFraming(maxSize = 1024, delimiter = ByteString(n)) >>new TcpReadWriteAdapter >>new BackpressureBuffer(lowBytes = 100,highBytes = 1000,maxBytes = 10000))val pipeline = context.actorOf(TcpPipelineHandler.props(init, connectionActor, userListenerActor))