Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 565fdbe
Showing
10 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name=Hazelcast-Spray-Akka | ||
title=Shopping list with Akka and Scala | ||
description=Shared shopping list using Hazelcast. | ||
tags=scala,akka,spray,hazelcast,scaladays2014 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name := "hazelcast-spray-akka" | ||
|
||
version := "0.1.0" | ||
|
||
val sprayV = "1.2.0" | ||
|
||
libraryDependencies ++= Seq( | ||
"io.spray" % "spray-can" % sprayV, | ||
"io.spray" % "spray-routing" % sprayV, | ||
"io.spray" % "spray-httpx" % sprayV, | ||
"com.typesafe.akka" %% "akka-actor" % "2.2.3", | ||
"com.hazelcast" % "hazelcast" % "3.2", | ||
"com.hazelcast" % "hazelcast-client" % "3.2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=0.13.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hazelcast | ||
|
||
import akka.actor.{ Actor, Props } | ||
|
||
import com.hazelcast.client.HazelcastClient | ||
import com.hazelcast.client.config.ClientConfig | ||
import Messages._ | ||
|
||
import scala.collection.JavaConversions._ | ||
|
||
class ChildActor extends Actor { | ||
|
||
val hazelcastClient = HazelcastClient.newHazelcastClient(new ClientConfig()) | ||
val shoppingList = hazelcastClient.getMap[String, Int]("shoppingList") | ||
|
||
def receive = { | ||
|
||
case GetItems => | ||
val items = (shoppingList.keys.toList zip shoppingList.values.toList).mkString("\n") | ||
sender ! items | ||
|
||
} | ||
} | ||
|
||
object ChildActor { | ||
|
||
def props = Props[ChildActor] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package hazelcast | ||
|
||
import akka.actor.{ Actor, Props } | ||
import akka.pattern.ask | ||
import akka.util.Timeout | ||
|
||
import reflect.ClassTag | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
import spray.routing._ | ||
import spray.routing.Directives._ | ||
|
||
import Messages._ | ||
|
||
class ChildApiActor extends HttpService with Actor { | ||
|
||
val child = context.actorOf(ChildActor.props) | ||
|
||
def actorRefFactory = context | ||
|
||
implicit val timeout = Timeout(3 seconds) | ||
|
||
def receive = runRoute( | ||
pathPrefix("list") { | ||
get { | ||
complete( | ||
(child ? GetItems).mapTo[String] | ||
) | ||
} | ||
} | ||
) | ||
|
||
} | ||
|
||
object ChildApiActor { | ||
def props = Props[ChildApiActor] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package hazelcast | ||
|
||
import akka.actor.{Props, ActorSystem} | ||
import akka.io.IO | ||
import spray.can.Http | ||
|
||
object Main extends App { | ||
|
||
implicit val system = ActorSystem("Shopping-List") | ||
|
||
val momApi = system.actorOf(MomApiActor.props) | ||
|
||
IO(Http) ! Http.Bind(momApi, interface = "0.0.0.0", port = 8888) | ||
|
||
val childApi = system.actorOf(ChildApiActor.props) | ||
|
||
IO(Http) ! Http.Bind(childApi, interface = "0.0.0.0", port = 9999) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hazelcast | ||
|
||
object Messages { | ||
|
||
case object GetItems | ||
case class AddItem(item: String, amount: Int) | ||
case class RemoveItem(item: String) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hazelcast | ||
|
||
import akka.actor.{ Actor, Props } | ||
|
||
import com.hazelcast.core.Hazelcast | ||
import com.hazelcast.config.Config | ||
import Messages._ | ||
|
||
class MomActor extends Actor { | ||
|
||
val hazelcastInstance = Hazelcast.newHazelcastInstance(new Config()) | ||
val shoppingList = hazelcastInstance.getMap[String, Int]("shoppingList") | ||
|
||
def receive = { | ||
|
||
case AddItem(item, amount) => | ||
sender ! shoppingList.put(item, amount) | ||
case RemoveItem(item) => | ||
sender ! shoppingList.remove(item) | ||
|
||
} | ||
|
||
} | ||
|
||
object MomActor { | ||
|
||
def props = Props[MomActor] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package hazelcast | ||
|
||
import akka.actor.{ Actor, Props } | ||
|
||
import spray.routing._ | ||
import spray.routing.Directives._ | ||
|
||
import Messages._ | ||
|
||
class MomApiActor extends HttpService with Actor { | ||
|
||
val mom = context.actorOf(MomActor.props) | ||
|
||
def actorRefFactory = context | ||
|
||
def receive = runRoute( | ||
pathPrefix("list") { | ||
path("(.+)".r) { item => | ||
parameter("n") { amount => | ||
post { | ||
mom ! AddItem(item, amount.toInt) | ||
complete("OK, mum") | ||
} | ||
} ~ | ||
delete { | ||
mom ! RemoveItem(item) | ||
complete("OK, mum") | ||
} | ||
} | ||
} | ||
) | ||
|
||
} | ||
|
||
object MomApiActor { | ||
def props = Props[MomApiActor] | ||
} |