1

I need to start two Finagle ListeningServers at once because I have to implement two different traits that extend ListeningServer.

/* A simplified example to give you an idea of what I'm trying to do */

trait FirstListeningServer extends ListeningServer {

  def buildFirstServer() = ???

  def main(): Unit = {
    val server = buildFirstServer()
    closeOnExit(server)
    Await.ready(server)
  }

}

trait SecondListeningServer extends ListeningServer {

  def buildSecondServer() = ???

  def main(): Unit = {
    val server = buildSecondServer()
    closeOnExit(server)
    Await.ready(server)
  }

}

Basically, each ListeningServer is a com.twitter.util.Awaitable and whenever I have to instantiate a new ListeningServer I use Await.ready(myListeningServer).

class MyServer extends FirstListeningServer with SecondListeningServer {

  override def main(): Unit = {
    val firstServer = buildFirstServer()
    closeOnExit(firstServer)
    val secondServer = buildSecondServer()
    closeOnExit(secondServer)
    Await.all(firstServer, secondServer)
  }
}

Now I'm not sure if using Await.all() is the right choice in order to start several ListeningServers concurrently. I would have used com.twitter.util.Future.collect() but I have two Awaitables.

def all(awaitables: Awaitable[_]*): Unit

Returns after all actions have completed.

I'm using Scala 2.12 and Twitter 20.3.0.

sentenza
  • 1,608
  • 3
  • 29
  • 51
  • Instead of awaiting multiple times, it will be good if you can return future from both buildFirstServer and buildSecondServer and then use join and then await on the same. https://twitter.github.io/util/docs/com/twitter/util/Future.html#join[B](other:com.twitter.util.Future[B]):com.twitter.util.Future[(A,B)] – Shankar Shastri Jun 09 '20 at 10:38
  • What I didn't say is that I cannot refactor/change `FirstListeningServer` and `SecondListeningServer`. I just receive the Awaitables. It would be easier to have a series of Futures in order to use a `Future.collect()`. – sentenza Jun 09 '20 at 10:44
  • I'm not sure about the await.all, but the problem since you are awaiting it will be awaiting till the HTTP server is running, so the second listening server wont start is the question right? – Shankar Shastri Jun 09 '20 at 10:48
  • I'm not sure if it will actually start concurrently or if it will wait for the first server to return (and actually be closed). – sentenza Jun 09 '20 at 11:34

1 Answers1

0
com.twitter.util.Await.all(listeningServer1, listeningServer2)

will start both in parallel.

Independent from this You can stop them both e.g. with the help of:

  listeningServer1.close(10.seconds)
  listeningServer2.close(10.seconds)
Hartmut Pfarr
  • 5,534
  • 5
  • 36
  • 42