0

I'm trying to implement a very simple service connected to an AMQP broker with Alpakka. I just want it to consume messages from its queue as a stream at the moment they are pushed on a given exchange/topic.

Everything seemed to work fine in my tests, but when I tried to start my service, I realized that my stream was only consuming my messages once and then exited.

Basically I'm using the code from Alpakka documentation :

def consume()={
    val amqpSource = AmqpSource.committableSource(
      TemporaryQueueSourceSettings(connectionProvider, exchangeName)
        .withDeclaration(exchangeDeclaration)
        .withRoutingKey(topic),
      bufferSize = prefetchCount
    )

    val amqpSink = AmqpSink.replyTo(AmqpReplyToSinkSettings(connectionProvider))

    amqpSource.mapAsync(4)(msg => onMessage(msg)).runWith(amqpSink)
}

I tried to schedule the consume() execution every second, but I experienced OutOfMemoryException issues.

Is there any proper way to make this code run as an infinite loop ?

Nicolas Delaforge
  • 1,434
  • 1
  • 10
  • 12

1 Answers1

2

If you want to have a Source restarted when it fails or is cancelled, wrap it with RestartSource.withBackoff.

dvim
  • 2,223
  • 1
  • 17
  • 17
  • I already tried to use `RestartSource.withBackoff` but that didn't restart the Source when it's completed, only when it fails. I'm looking for a Source that never ends. – Nicolas Delaforge Mar 14 '19 at 16:28
  • `onFailuresWithBackoff` does that. `withBackoff` restarts both, on failure and completion. – dvim Mar 14 '19 at 22:29
  • 1
    You are right, RestartSource did it, thank you ! My problem with `RestartSource` came from my Sink. Everything seems to work fine now. – Nicolas Delaforge Mar 15 '19 at 08:15