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
object Pub { | |
println("starting publishing service ..") | |
val p = new Publisher(new RedisClient("localhost", 6379)) | |
p.start | |
def publish(channel: String, message: String) = { | |
p ! Publish(channel, message) | |
} | |
} |
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
sealed trait PubSubMessage | |
case class S(channel: String, noSubscribed: Int) extends PubSubMessage | |
case class U(channel: String, noSubscribed: Int) extends PubSubMessage | |
case class M(origChannel: String, message: String) extends PubSubMessage |
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
1. Open a shell and set AKKA_HOME to the distribution root | |
2. cd $AKKA_HOME | |
3. sbt console | |
4. scala> import sample.pubsub._ | |
5. scala> Sub.sub("a", "b") // starts Subscription server & subscribes to channels "a" and "b" | |
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
1. Open up another shell similarly as the above and set AKKA_HOME | |
2. cd $AKKA_HOME | |
3. sbt console | |
4. scala> import sample.pubsub._ | |
5. scala> Pub.publish("a", "hello") // the first shell should get the message | |
6. scala> Pub.publish("c", "hi") // the first shell should NOT get this message | |
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
Open up a redis-client from where you installed redis and issue a publish command | |
./redis-cli publish a "hi there" ## the first shell should get the message |
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
1. Go back to the first shell | |
2. Sub.unsub("a") // should unsubscribe the first shell from channel "a" | |
3. Study the callback function defined below. It supports many other message formats. | |
4. In the second shell window do the following: | |
scala> Pub.publish("b", "+c") // will subscribe the first window to channel "c" | |
scala> Pub.publish("b", "+d") // will subscribe the first window to channel "d" | |
scala> Pub.publish("b", "-c") // will unsubscribe the first window from channel "c" | |
scala> Pub.publish("b", "exit") // will unsubscribe the first window from all channels |
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
class Publisher(client: RedisClient) extends Actor { | |
def receive = { | |
case Publish(channel, message) => | |
client.publish(channel, message) | |
reply(true) | |
} | |
} |
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
newOrder.to.buy(100.shares.of('IBM')) { | |
limitPrice 300 | |
allOrNone true | |
valueAs {qty, unitPrice -> qty * unitPrice - 500} | |
} |
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
ScriptEngineManager factory = new ScriptEngineManager(); | |
ScriptEngine engine = factory.getEngineByName("groovy"); | |
List<?> orders = (List<?>) | |
engine.eval(new InputStreamReader( | |
new BufferedInputStream( | |
new SequenceInputStream( | |
new FileInputStream("ClientOrder.groovy"), | |
new FileInputStream("order.dsl"))))); |
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
public class RunScript { | |
public static void main(String[] args) | |
throws CompilationFailedException, IOException, | |
InstantiationException, IllegalAccessException { | |
final ClientOrder clientOrder = new ClientOrder(); | |
clientOrder.run(); | |
final Closure dsl = |