Skip to content

Latest commit

 

History

History
149 lines (126 loc) · 11 KB

README.md

File metadata and controls

149 lines (126 loc) · 11 KB

Akka examples

Akka project Directory examples\ contains Akka code examples coming from various websites - mostly from the Akka project.

akka-quickstart Example

We have implemented this code example in 3 different programming languages :

Source files Batch Gradle Maven
Java AkkaQuickstart.java, Greeter.java build.bat build.gradle pom.xml
Kotlin AkkaQuickstart.kt, Greeter.kt build.bat build.gradle pom.xml
Scala AkkaQuickstart.scala, Greeter.scala build.bat build.gradle pom.xml

Here are 3 command executions for the Java code example :

> cd akka-quickstart-java
 
> build -verbose run
Compile 4 Java source files to directory "target\classes"
[helloakka-akka.actor.default-dispatcher-6] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
>>> Press ENTER to exit <<<
[helloakka-akka.actor.default-dispatcher-3] INFO com.example.Greeter - Hello Charles!
[helloakka-akka.actor.default-dispatcher-6] INFO com.example.GreeterBot - Greeting 1 for Charles
[helloakka-akka.actor.default-dispatcher-3] INFO com.example.Greeter - Hello Charles!
[helloakka-akka.actor.default-dispatcher-6] INFO com.example.GreeterBot - Greeting 2 for Charles
[helloakka-akka.actor.default-dispatcher-3] INFO com.example.Greeter - Hello Charles!
[helloakka-akka.actor.default-dispatcher-6] INFO com.example.GreeterBot - Greeting 3 for Charles

[helloakka-akka.actor.default-dispatcher-3] INFO akka.actor.CoordinatedShutdown - Running CoordinatedShutdown with reason [ActorSystemTerminateReason]
 
> gradle -q run
SLF4J: A number (1) of logging calls during the initialization phase have been intercepted and are
SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#replay
[2022-02-10 17:54:31,240] [INFO] [akka.event.slf4j.Slf4jLogger] [helloakka-akka.actor.default-dispatcher-3] [] - Slf4jLogger started
>>> Press ENTER to exit <<<
[2022-02-10 17:54:31,289] [INFO] [com.example.Greeter] [helloakka-akka.actor.default-dispatcher-7] [akka://helloakka/user/greeter] - Hello Charles!
[2022-02-10 17:54:31,289] [INFO] [com.example.GreeterBot] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/Charles] - Greeting 1 for Charles
[2022-02-10 17:54:31,299] [INFO] [com.example.Greeter] [helloakka-akka.actor.default-dispatcher-7] [akka://helloakka/user/greeter] - Hello Charles!
[2022-02-10 17:54:31,299] [INFO] [com.example.GreeterBot] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/Charles] - Greeting 2 for Charles
[2022-02-10 17:54:31,299] [INFO] [com.example.Greeter] [helloakka-akka.actor.default-dispatcher-7] [akka://helloakka/user/greeter] - Hello Charles!
[2022-02-10 17:54:31,299] [INFO] [com.example.GreeterBot] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/Charles] - Greeting 3 for Charles
<=========----> 75% EXECUTING [3s]
 
> mvn -q compile exec:java
SLF4J: A number (1) of logging calls during the initialization phase have been intercepted and are[2022-02-10 17:50:47,070] [INFO] [akka.event.slf4j.Slf4jLogger] [helloakka-akka.actor.default-dispatcher-3] [] - Slf4jLogger started
 
SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#replay
>>> Press ENTER to exit <<<
[2022-02-10 17:50:47,120] [INFO] [com.example.Greeter] [helloakka-akka.actor.default-dispatcher-5] [akka://helloakka/user/greeter] - Hello Charles!
[2022-02-10 17:50:47,130] [INFO] [com.example.GreeterBot] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/Charles] - Greeting 1 for Charles
[2022-02-10 17:50:47,130] [INFO] [com.example.Greeter] [helloakka-akka.actor.default-dispatcher-5] [akka://helloakka/user/greeter] - Hello Charles!
[2022-02-10 17:50:47,130] [INFO] [com.example.GreeterBot] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/Charles] - Greeting 2 for Charles
[2022-02-10 17:50:47,130] [INFO] [com.example.Greeter] [helloakka-akka.actor.default-dispatcher-5] [akka://helloakka/user/greeter] - Hello Charles!
[2022-02-10 17:50:47,130] [INFO] [com.example.GreeterBot] [helloakka-akka.actor.default-dispatcher-3] [akka://helloakka/user/Charles] - Greeting 3 for Charles
 
[2022-02-10 17:50:49,880] [INFO] [akka.actor.CoordinatedShutdown] [helloakka-akka.actor.default-dispatcher-5] [CoordinatedShutdown(akka://helloakka)] - Running CoordinatedShutdown with reason [ActorSystemTerminateReason]

shopping-cart-service Example

We add 3 socks to a cart (see section 6.1 Exercise the service) with the grpcurl1 tool :

> where grpcurl
C:\opt\grpcurl\grpcurl.exe
 
> grpcurl -d "{\"cartId\":\"cart1\", \"itemId\":\"socks\", \"quantity\":3}" -plaintext 127.0.0.1:8101 shoppingcart.ShoppingCartService.AddItem
{
  "items": [
    {
      "itemId": "socks",
      "quantity": 3
    }
  ]
}

We add 2 t-shirts to the same cart (see section 6.1 Exercise the service):

> grpcurl -d "{\"cartId\":\"cart1\", \"itemId\":\"t-shirt\", \"quantity\":2}" -plaintext 127.0.0.1:8101 shoppingcart.ShoppingCartService.AddItem
{
  "items": [
    {
      "itemId": "t-shirt",
      "quantity": 2
    }
  ]
}

Receptionist Example

The Receptionist example is adapted from Alvin's tutorial on Typed Akka.

Command build.bat clean run generates the Akka application from the source files Brain.scala, Mouth.scala, ReceptionistApp.scala and Supervisor.scala and executes it :

> build -verbose clean run
Delete directory "target"
Compile 4 Scala source files to directory "target\classes"
Copy resource files to directory "target\classes"
Run Scala program "akka.examples.ReceptionistApp"
[Supervisor-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
SLF4J: A number (1) of logging calls during the initialization phase have been intercepted and are
SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system.
SLF4J: See also https://www.slf4j.org/codes.html#replay
[Supervisor-akka.actor.internal-dispatcher-4] DEBUG akka.actor.typed.internal.receptionist.LocalReceptionist - Actor was registered: ServiceKey[akka.examples.Mouth$MessageToMouth](Mouth) Actor[akka://Supervisor/user/Mouth#-1231001945]Supervisor got a Start message

Brain: got a FindTheMouth message
listingAdapter:listing: Listing(ServiceKey[akka.examples.Mouth$MessageToMouth](Mouth),Set(Actor[akka://Supervisor/user/Mouth#-1231001945]),Set(Actor[akka://Supervisor/user/Mouth#-1231001945]),true)
Brain: got a ListingResponse message
Mouth: got a msg: Brain says hello to Mouth
listingAdapter:listing: Listing(ServiceKey[akka.examples.Mouth$MessageToMouth](Mouth),Set(Actor[akka://Supervisor/user/Mouth#-1231001945]),Set(Actor[akka://Supervisor/user/Mouth#-1231001945]),true)
Brain: got a ListingResponse message
Mouth: got a msg: Brain says hello to Mouth
[Supervisor-akka.actor.default-dispatcher-5] INFO akka.actor.CoordinatedShutdown - Running CoordinatedShutdown with reason [ActorSystemTerminateReason]
[Supervisor-akka.actor.internal-dispatcher-4] DEBUG akka.actor.typed.internal.receptionist.LocalReceptionist - Registered actor terminated: [ServiceKey[akka.examples.Mouth$MessageToMouth](Mouth)] Actor[akka://Supervisor/user/Mouth#-1231001945]
[Supervisor-akka.actor.internal-dispatcher-4] DEBUG akka.actor.typed.internal.receptionist.LocalReceptionist - Subscribed actor terminated: [ServiceKey[akka.examples.Mouth$MessageToMouth](Mouth)] Actor[akka://Supervisor/user/Brain/$$a-adapter#741795935]

Footnotes

[1] grpcurl

Command line clients for gRPC - grpcurl, April 2018.

mics/November 2024