Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dvallejo committed Jun 5, 2014
0 parents commit 565fdbe
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 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.
4 changes: 4 additions & 0 deletions 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
13 changes: 13 additions & 0 deletions 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")
1 change: 1 addition & 0 deletions project/build.properties
@@ -0,0 +1 @@
sbt.version=0.13.2
29 changes: 29 additions & 0 deletions 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]
}

38 changes: 38 additions & 0 deletions 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]
}
19 changes: 19 additions & 0 deletions 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)

}
9 changes: 9 additions & 0 deletions 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)

}
29 changes: 29 additions & 0 deletions 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]
}

37 changes: 37 additions & 0 deletions 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]
}

0 comments on commit 565fdbe

Please sign in to comment.