我正在使用 scala 中的 akka zeromq 编写这个简单的 hello -world 有点玩具程序。我的订阅者似乎没有收到任何消息。我从发布者的接收方法看到发布者发布的消息,但仅此而已。谁能帮我让这个程序正常工作!
先感谢您。
import akka.util.ByteString
import akka.zeromq._
import akka.actor._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
class Sub extends Actor{
def receive = {
//case m:ZMQMessage if m.frames(0).utf8String == "hello" => println(m.frames(1).toString())
case m:ZMQMessage => println("************* from subscriber *****************") //;println(m.frames(1).toString())
case ss :String => println (" ---- some string appeared ")
//case _ =>
}
}
class Pub extends Actor{
//creates a publisher socket bound to host at port 1235
val pubSocket = ZeroMQExtension(context.system).newSocket(SocketType.Pub, Bind("tcp://*:1235"))
def receive={
case msg:String =>
println(s"Got Message $msg")
pubSocket ! ZMQMessage(ByteString("hello"), ByteString("this is test message"))
}
}
object main extends App {
println("Starting App.")
val system = ActorSystem("system")
val subActor = system.actorOf(Props[Sub])
val pubActor = system.actorOf(Props[Pub])
// sockets are created using ZeroMQExtension
// creates a subscriber socket
val subSocket = ZeroMQExtension(system).newSocket(SocketType.Sub, Listener(subActor), Connect("tcp://*:1235"), SubscribeAll)
system.scheduler.schedule(1 seconds, 0 minutes)(pubActor ! "Message to Pub")
println("Exiting main.")
}