Akka Rest Service Example Java

Building a fully functional REST service using Akka actors

Setting up the project

img 1.0: Project Structure

The sbt build file (build.sbt)

                      name            := "show-tix"

version := "0.1"

scalaVersion := "2.12.6"

organization := "com.showtix"

libraryDependencies ++= {
val akkaVersion = "2.5.12"
val akkaHttp = "10.1.1"
Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-http-core" % akkaHttp,
"com.typesafe.akka" %% "akka-http" % akkaHttp,
"com.typesafe.play" %% "play-ws-standalone-json" % "1.1.8",
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"ch.qos.logback" % "logback-classic" % "1.2.3",
"de.heikoseeberger" %% "akka-http-play-json" % "1.17.0",
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % "test",
"org.scalatest" %% "scalatest" % "3.0.5" % "test"
)
}

                      organization  %   module  %   version                  
          sbt compile        

REST service

gist 1.0: Rest api requirements

App structure

img 1.1: Application Structure

img 1.3: resources folder
gist 1.1: application.conf
gist 1.2: logback.xml

img 1.4: ServiceMain object file
gist 1.3: ServiceMain object

Messages

img 1.5: messages package
gist 1.4: Coachella messages
gist 1.5: TicketSeller messages

Actors

gist 1.6: The TicketSeller actor

img 1.6: props method in TicketSeller message object

The parent actor (Coachella)

gist 1.7: Coachella actor

                      self.ask(GetEvent(child.path.name)).mapTo[Option[Event]]        
                      def            convertToEvents(f: Future[Iterable[Option[Event]]]) = {
f.map(_.flatten).map(l ⇒ Events(l.toVector))
}
          pipe(convertToEvents(Future.sequence(getEvents))) to sender()        

                      import            scala.concurrent.ExecutionContext.Implicits.global                  

          object Future {
def apply[T](body: =>T)(implicit @deprecatedName('execctx) executor: ExecutionContext): Future[T] =
unit.map(_ => body)
}

HTTP Routes (RestApi)

gist 1.8: EventMarshaller.scala

gist 1.9: RestApi.scala with a create event endpoint implementation
gist 2.0: Updated ServiceMain.scala file

          POST show-tix/v1/events/event_name        

gist 2.1: RestApi.scala with all requirements.
          POST show-tix/v1/events/event_name        

          GET show-tix/v1/events        

          POST show-tix/v1/events/:event/tickets        

          {
"event": "HoboJohnson",
"entries": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
},
{
"id": 5
},
{
"id": 6
},
{
"id": 7
},
{
"id": 8
},
{
"id": 9
},
{
"id": 10
},
{
"id": 11
},
{
"id": 12
},
{
"id": 13
},
{
"id": 14
},
{
"id": 15
},
{
"id": 16
},
{
"id": 17
},
{
"id": 18
},
{
"id": 19
},
{
"id": 20
},
{
"id": 21
},
{
"id": 22
},
{
"id": 23
},
{
"id": 24
},
{
"id": 25
},
{
"id": 26
},
{
"id": 27
},
{
"id": 28
},
{
"id": 29
},
{
"id": 30
}
]
}

Deploying to Heroku

          heroku login        
gist 2.2: plugins.sbt
                      enablePlugins(JavaServerAppPackaging)        

gist 2.3: Procfile

          git push heroku master        

arenasitth1936.blogspot.com

Source: https://medium.com/@ian.shiundu/building-a-fully-functional-rest-service-using-akka-actors-8b5c12978380

0 Response to "Akka Rest Service Example Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel