Akka with java

Table of contents
Reading Time: 3 minutes

Hello friends ,

In last few days i was working on a project with akka using java . This was really an amazing experience in akka .

Here we will discuss that how to use Akka in java and write the test case for the same .

If we see documentation of Akka they extends a class named UntypedActor to create an actor .  But here we will discuss about AbstractActor which leads less cost and hence seems  pretty and concise   .

At first add following dependency for Akka and testcase :

"com.typesafe.akka" %% "akka-slf4j" % "2.4.8"

Now to create an actor at first we need to create Props   :

Then after we write the responsiblity of an actor as following :

Now  we need to find out the message passed to this actor and perform accordingly .  We can do this as follow :

Now everybody will need an Supervisior . As we know Supervisior is an another Actor to supervise actors .  Following code may be the way to supervise actor :

Now Cheers !!! we have an architecture of akka actor.

After that i decided to introduce Google guice injection . So to proceed with this we need to add following dependency at first :

"com.google.inject" % "guice" % "4.1.0"

we will inject our service to actor , so we here go to create a simple service :

Now create an another class to define injections as follow  :

and also modify the actor as :

Now we can write our launcher to start actor’s execution as  :

Now we developer always curious about how to write unit test case . so at first add the following sbt dependency :

"junit" % "junit" % "4.12" % "test" exclude("hamcrest-core", "org.hamcrest"),
"com.typesafe.akka" %% "akka-http-testkit" % "2.4.8" % "test",
"org.mockito" % "mockito-core" % "1.10.19" % "test" exclude("hamcrest-core", "org.hamcrest"),
"org.hamcrest" % "hamcrest-all" % "1.3" % "test"

and here we go for unit test case :

For complete code click here 

 

Thanks

KNOLDUS-advt-sticker

 

Written by 

Software Consultant At Knoldus

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading