The isAlive() method didn't work for me, but I found a presentation with a 
solution at 
http://www.slideshare.net/normation/nrm-scala-ioscalaetzeromqv10.

It seems that Akka "ask" doesn't work directly with the akka-zeromq socket, 
but you can add an extra ZmqClient actor in the middle (between Akka and 
ZMQ) that keeps track of the actorRef that sent the command and uses that 
to send the reply. Then you can use "ask" with that actor, so that the 
message times out if the process is not running.

import akka.zeromq._
> import akka.actor.{Props, ActorRef, Actor, ActorLogging}
> import akka.zeromq.Connect
> import akka.zeromq.Listener
> import akka.util.ByteString
> object ZmqClient {
>   def props(url: String): Props = Props(classOf[ZmqClient], url)
>   // Type of a command sent to the ZMQ socket
>   case class Command(m: ByteString)
> }
> /**
>  * Akka ask (?) doesn't work when used on the akka-zeromq socket,
>  * so this class can be put in between to make it work. See page 30 of:
>  * http://www.slideshare.net/normation/nrm-scala-ioscalaetzeromqv10
>  */
> class ZmqClient(url: String) extends Actor with ActorLogging {
>   val clientSocket = ZeroMQExtension(context.system).newSocket(
>     SocketType.Req,
>     Listener(self),
>     Connect(url))
>   override def receive: Receive = waitingForCommand
>   def waitingForCommand: Receive = {
>     case ZmqClient.Command(byteString) ⇒
>       clientSocket ! ZMQMessage(byteString)
>       context.become(waitingForReply(sender))
>     case x ⇒ log.info(s"Unexpected Message from ZMQ: $x")
>   }
>   // Wait for the reply from the ZMQ socket and then forward it to the 
> replyTo actor
>   def waitingForReply(replyTo: ActorRef): Receive = {
>     case m: ZMQMessage ⇒
>       replyTo ! m
>       context.become(waitingForCommand)
>     case ZmqClient.Command(byteString) ⇒
>       // Wait may have timed out, just continue with this new command
>       clientSocket ! ZMQMessage(byteString)
>       context.become(waitingForReply(sender))
>     case x ⇒ log.info(s"Unexpected reply from ZMQ: $x")
>   }
> }



On Friday, January 24, 2014 11:50:07 PM UTC+1, Oleg Zhurakousky wrote:
>
> There may be a better way, but this may be all you need
>
> def isAlive(host:String, port:Int) = try {
>
>     val s = new Socket(host, port)
>
>     s.close()
>
>     true
>
>   } catch {
>
>     case e: Exception => false
>
>   }
>
>
> On Fri, Jan 24, 2014 at 4:23 PM, Allan Brighton 
> <[email protected]<javascript:>
> > wrote:
>
>> Hi,
>>
>> If I create an akka-zeromq client socket like this:
>>
>>   val clientSocket = 
>> ZeroMQExtension(context.system).newSocket(SocketType.Req,
>>     Listener(self), Connect(url))
>>
>> and there is no ZMQ process running or listening at the given URL, the 
>> client socket is still returned normally.
>> And later I can send a message like this:
>>
>>   clientSocket ! ZMQMessage(zmqMsg)
>>
>> but there will not be a reply. What is the best way to report an error if 
>> there is no ZMQ process running?
>> I could use ask (?) and let it time out, but is there a better way?
>>
>> (Note: in this case, the Akka client is sending a message first and then 
>> waiting for a response from a ZMQ process.)
>>
>> Thanks,
>> Allan
>>
>>
>>
>>  -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: http://akka.io/faq/
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: http://akka.io/faq/
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to