From 565fdbe46ef1be52af450c4340cfcb08d0df1e05 Mon Sep 17 00:00:00 2001 From: David Vallejo Date: Thu, 5 Jun 2014 18:42:16 +0200 Subject: [PATCH] Initial commit --- LICENSE.txt | 11 +++++++ activator.properties | 4 +++ build.sbt | 13 ++++++++ project/build.properties | 1 + src/main/scala/hazelcast/ChildActor.scala | 29 +++++++++++++++++ src/main/scala/hazelcast/ChildApi.scala | 38 +++++++++++++++++++++++ src/main/scala/hazelcast/Main.scala | 19 ++++++++++++ src/main/scala/hazelcast/Messages.scala | 9 ++++++ src/main/scala/hazelcast/MomActor.scala | 29 +++++++++++++++++ src/main/scala/hazelcast/MomApi.scala | 37 ++++++++++++++++++++++ 10 files changed, 190 insertions(+) create mode 100644 LICENSE.txt create mode 100644 activator.properties create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 src/main/scala/hazelcast/ChildActor.scala create mode 100644 src/main/scala/hazelcast/ChildApi.scala create mode 100644 src/main/scala/hazelcast/Main.scala create mode 100644 src/main/scala/hazelcast/Messages.scala create mode 100644 src/main/scala/hazelcast/MomActor.scala create mode 100644 src/main/scala/hazelcast/MomApi.scala diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..86a8bee --- /dev/null +++ b/LICENSE.txt @@ -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. \ No newline at end of file diff --git a/activator.properties b/activator.properties new file mode 100644 index 0000000..6579ccf --- /dev/null +++ b/activator.properties @@ -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 \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..75c4aec --- /dev/null +++ b/build.sbt @@ -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") \ No newline at end of file diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..ac71617 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.2 \ No newline at end of file diff --git a/src/main/scala/hazelcast/ChildActor.scala b/src/main/scala/hazelcast/ChildActor.scala new file mode 100644 index 0000000..1bcfea3 --- /dev/null +++ b/src/main/scala/hazelcast/ChildActor.scala @@ -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] +} + diff --git a/src/main/scala/hazelcast/ChildApi.scala b/src/main/scala/hazelcast/ChildApi.scala new file mode 100644 index 0000000..7f40ac1 --- /dev/null +++ b/src/main/scala/hazelcast/ChildApi.scala @@ -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] +} diff --git a/src/main/scala/hazelcast/Main.scala b/src/main/scala/hazelcast/Main.scala new file mode 100644 index 0000000..6778957 --- /dev/null +++ b/src/main/scala/hazelcast/Main.scala @@ -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) + +} \ No newline at end of file diff --git a/src/main/scala/hazelcast/Messages.scala b/src/main/scala/hazelcast/Messages.scala new file mode 100644 index 0000000..1bdeb55 --- /dev/null +++ b/src/main/scala/hazelcast/Messages.scala @@ -0,0 +1,9 @@ +package hazelcast + +object Messages { + + case object GetItems + case class AddItem(item: String, amount: Int) + case class RemoveItem(item: String) + +} diff --git a/src/main/scala/hazelcast/MomActor.scala b/src/main/scala/hazelcast/MomActor.scala new file mode 100644 index 0000000..3893112 --- /dev/null +++ b/src/main/scala/hazelcast/MomActor.scala @@ -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] +} + diff --git a/src/main/scala/hazelcast/MomApi.scala b/src/main/scala/hazelcast/MomApi.scala new file mode 100644 index 0000000..3f523b1 --- /dev/null +++ b/src/main/scala/hazelcast/MomApi.scala @@ -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] +}