How about something like this?
Source.repeat(NotUsed).flatMapConcat { _ =>
val items = service.getItems
if (items.isEmpty)
Source.tick(30.seconds, 1.second, Seq.empty[Item]).take(1).drop(1) //
<-- more than likely easier ways to do this, but it is late here.
else
Source.single(items)
}
On Fri, Dec 30, 2016 at 11:53 PM, Konrad Malawski <
[email protected]> wrote:
> Read the docs about GraphStage and custom stream processing elements. It
> had all the utilities needed for this. We also explained them on the Akka
> blog: Akka.io/blog so check that out :)
>
> On Dec 30, 2016 23:29, "Drew Kutcharian" <[email protected]> wrote:
>
>> Hi,
>>
>> I have a use case where I poll a datasource for items and as long as
>> there are items present I process them as quickly as possible, but when
>> there are no items, I would like to poll every X seconds. Is there a
>> built-in construct in Akka Streams that I can use?
>>
>> I'm currently using `Source.unfoldAsync` to get the records from the data
>> source. One "bad" option is to just call `Thread.sleep(XXX)` inside
>> `Source.unfoldAsync` when the datasource returns no results (as
>> demonstrated below), but I want to see if there's a better option.
>>
>> Source.unfoldAsync[NotUsed, Seq[Item]](NotUsed) { _ =>
>> service.getItems flatMap { items =>
>> if (items.isEmpty) {
>> //TODO Fix this!!!
>> Thread.sleep(30 * 1000) //30 seconds
>> }
>> Future.successful(Some((NotUsed, items)))
>> }
>> }
>>
>> I can probably improve on the `Thread.sleep(XXX)` by using having some
>> "State" object and have smaller waits, but I feel like that's not going to
>> be that much better.
>>
>> Any input is appreciated.
>>
>> Thanks,
>>
>> Drew
>>
>> --
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/c
>> urrent/additional/faq.html
>> >>>>>>>>>> Search the archives: https://groups.google.com/grou
>> p/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 https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
> current/additional/faq.html
> >>>>>>>>>> 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 https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
--
Cheers,
√
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> 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 https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.