Created
August 29, 2016 11:46
-
-
Save alexcpn/9d96363c4131203dbe50f38529b19bdb to your computer and use it in GitHub Desktop.
Scala Remote Actor Communicaiton
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
package run | |
import java.io.File | |
import akka.actor.{Actor, ActorRef, ActorSelection, ActorSystem, Props} | |
import com.typesafe.config.ConfigFactory | |
import org.slf4j.LoggerFactory | |
/** | |
* Created by acp on 25-08-2016. | |
*/ | |
sealed trait CoffeeRequest | |
case object CoffeeRequest extends CoffeeRequest | |
case object ExpressoRequest extends CoffeeRequest | |
case class Bill(cents: Int) | |
case object closingTime | |
case object CaffineWithdrawal | |
/** | |
* Class for Starting up the Servers | |
*/ | |
object BootUp { | |
val logger = LoggerFactory.getLogger(this.getClass) | |
val configFile = getClass.getClassLoader. | |
getResource("remote_application.conf").getFile | |
val config_remote = ConfigFactory.parseFile(new File(configFile)) | |
val system = ActorSystem("BaristaAS", config_remote) | |
val barista: ActorRef = system.actorOf(Props[BaristaShop], "Barista") | |
def main(args: Array[String]) { | |
logger.info("Bootup f ") | |
Thread.sleep(30000) | |
barista ! closingTime | |
Thread.sleep(3000) | |
} | |
} | |
object Client { | |
val logger = LoggerFactory.getLogger(this.getClass) | |
val configFile = getClass.getClassLoader. | |
getResource("local_configuration.conf").getFile | |
val config_local = ConfigFactory.parseFile(new File(configFile)) | |
val system = ActorSystem("CustomerAS",config_local) | |
def main(args: Array[String]) { | |
logger.info("Client for CrowdCellController ") | |
val barista = system.actorSelection("akka.tcp://BaristaAS@127.0.0.1:5150/user/Barista") | |
val customer: ActorRef = system.actorOf(Props(classOf[Customer], barista), "Customer") | |
for( i <- 1 to 1000000) { | |
customer ! CaffineWithdrawal | |
} | |
logger.info("Client Send") | |
Thread.sleep(30000) | |
} | |
} | |
class BaristaShop extends Actor { | |
var coffee_request =0 | |
def receive = { | |
case CoffeeRequest => | |
//println("Coffee Request") | |
coffee_request+=1 | |
sender ! Bill(20) | |
//println("Going to prepare Coffee") | |
case ExpressoRequest => println("Exspresso Request") | |
case closingTime => | |
println(s"Number of CofeeRequest is $coffee_request") | |
sender ! closingTime | |
context.system.shutdown() | |
} | |
} | |
class Customer(coffeeSource: ActorSelection) extends Actor { | |
/** | |
* Connect to Remote Actor | |
*/ | |
var money_to_pay = 0 | |
def receive = { | |
case CaffineWithdrawal => coffeeSource ! CoffeeRequest | |
case Bill(cents) => | |
//println(s"I will have to pay $cents") | |
money_to_pay+=1 | |
case closingTime => | |
println(s"I will have to pay $money_to_pay") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment