Last active
January 24, 2020 22:23
-
-
Save olbpetersson/ed9d0bac0540f002fbf961cef4c76248 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun receiveMessages() = flow { | |
while(isActive) { | |
val messages = getMesagesFromSqs() // getMessages here takes 1 second | |
logger.info("Nr of received messages $nrOfReturnedMessages") | |
emit(messages) | |
} | |
} | |
} | |
suspend fun processMessage(flow: Flow<List<Message>>) { | |
flow.buffer(1). collect { | |
it.map { | |
GlobalScope.async { | |
logger.info("Processing a message $it") | |
nrOfProcessedMessages++ | |
delay(10000) | |
} | |
}.awaitAll() | |
logger.info("Processed messages: $nrOfProcessedMessages") | |
} | |
} | |
} | |
private suspend fun getMesagesFromSqs(): List<Message> { | |
// TODO: Real impl | |
logger.info("SqsCall to receive messages") | |
delay(1000) | |
nrOfReturnedMessages +=10 | |
return (1..10).map { | |
Message().addAttributesEntry("$it", "$it") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment