1
votes

I'm trying to write server which listen for tcp connection. I'm using nonblockig java.nio.

My selector.select() starts when actor receive "STARTLISTEN" message and it's ok, but when i sent messages to actor it not receive messages until selector not received any data.

How to avoid this? For example i want to send message "STOP" to my actor to stop selecting? Is only way to call select with timeout or selectNow()?

I'm not going to use akka.io.

It's not full code but it illustrates the problem:

class MyActor(val host: String, port: Int) extends Actor {

  val serverChannel = ServerSocketChannel.open()    
  val channelSelector = Selector.open()

  serverChannel.configureBlocking(false);       
  serverChannel.bind(new InetSocketAddress( InetAddress.getByName(host), port ))

  serverChannel.register(channelSelector, SelectionKey.OP_ACCEPT);                  

  override def receive = {
      case "STARTLISTEN" => {
         val keys = channelSelector.select()           
         // processing read write etc....
      }
      case "STOP" => {
         // I want to stop selection here....
      }
  }
}
2
Very unclear, you should share relevant snippets of your code. - vptheron

2 Answers

1
votes

Even though you dismiss it out of hand, I’d like to point out (on the record) that using the proven implementation of exactly this which ships with Akka already is probably the better way to go. (I’m linking to the 2.3.0-RC2 documentation because some of the experimental features which were introduced in the 2.2.x series will not be part of Akka going forward—survival of the fittest.)

0
votes

You might be able to accomplish this by splitting your actor into multiple actors. The first actor receives messages from outside, and the second blocks on select(). The first actor can use wakeup() to stop the second from blocking before sending it any messages.