Skip to content

A collection of runnable and self-contained examples from various akka-streams docs, tutorials and blogs

License

Notifications You must be signed in to change notification settings

kai2002/akka_streams_tutorial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Akka streams tutorial

"It works!" a colleague used to shout across the office when another proof of concept was running it's first few hundred meters along the happy path, well aware that the real work started right there. This repo contains a collection of runnable and self-contained examples from various akka streams docs, tutorials, blogs and postings to provide you with exactly this feeling. See the class comment on how to run each example. These more complex examples are described below:

  • HTTP file download with local cache
  • Windturbine Example
  • Apache Kafka WordCount
  • HL7 V2 over TCP via Kafka to Websockets

These examples all deal with some kind of shared state.

Another group of examples are the *Echo classes, which implement round trips eg HttpFileEcho.scala and WebsocketEcho.scala.

Remarks:

  • Most examples are throttled so you can see from the console output what is happening.
  • Some examples deliberately throw RuntimeException, so you can observe recovery.
  • No unit tests and quirky package names.

HTTP file download with local cache

Use case with shared state:

  • Process a stream of incoming messages with reoccurring TRACE_ID
  • For the first message: download a .zip file from a FileServer and add TRACE_ID→Path to the local cache
  • For subsequent messages with the same TRACE_ID: fetch file from cache to avoid duplicate downloads per TRACE_ID
Class Description
FileServer.scala Local HTTP FileServer for non-idempotent file download simulation
LocalFileCacheCaffeine.scala Akka streams flow, which uses a local file cache implemented with caffeine to share state

Windturbine Example

Working sample from the blog series 1-4 from Colin Breck where classic Actors are used to model shared state, life-cycle management and fault-tolerance in combination with akka-streams. Colin Breck explains these concepts and more in the 2017 Reactive Summit talk Islands in the Stream: Integrating Akka Streams and Akka Actors

Class Description
SimulateWindTurbines.scala Starts n clients which feed measurements to the server
WindTurbineServer.scala Start server which a accumulates measurements

The clients communicate via websockets with the WindTurbineServer. After a restart of SimulateWindTurbines the clients are able to resume. Shutting down the WindTurbineServer results in reporting the clients that the server is not reachable. After restarting WindTurbineServer the clients are able to resume. Since there is no persistence, the processing just continuous.

Apache Kafka WordCount

The ubiquitous word count with additional message count (A message is a sequence of words). Start the classes in the order below and watch the console output.

Class Description
KafkaServer.scala Standalone Kafka/Zookeeper.
WordCountProducer.scala Client which feeds words to topic wordcount-input. Implemented with akka-streams-kafka
WordCountKStreams.java Client which does word and message count. Implemented with Kafka Streams DSL
WordCountConsumer.scala Client which consumes aggregated results from topic wordcount-output and messagecount-output. Implemented with akka-streams-kafka
DeleteTopicUtil.scala Utility to reset the offset

WordCountKStreams.java and WordCountConsumer.scala should yield the same results.

HL7 V2 over TCP via Kafka to Websockets

This PoC in package alpakka.tcp_to_websockets is some kind of Alpakka-Trophy with these stages:

Hl7TcpClientHl7Tcp2KafkaKafkaServerKafka2WebsocketWebsocketServer

The focus is on resilience (= try not to lose messages during the restart of the stages). However, currently messages might reach the WebsocketServer unordered (due to retry in Hl7TcpClient) and in-flight messages may get lost.

About

A collection of runnable and self-contained examples from various akka-streams docs, tutorials and blogs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Scala 79.1%
  • Java 19.8%
  • HTML 1.1%