|
| 1 | +@file:JvmName("KtorApplicationExample") |
| 2 | +package com.jashmore.sqs.examples |
| 3 | + |
| 4 | +import com.jashmore.sqs.container.MessageListenerContainer |
| 5 | +import com.jashmore.sqs.elasticmq.ElasticMqSqsAsyncClient |
| 6 | +import com.jashmore.sqs.ktor.container.batchingMessageListener |
| 7 | +import com.jashmore.sqs.ktor.container.messageListener |
| 8 | +import com.jashmore.sqs.ktor.container.prefetchingMessageListener |
| 9 | +import io.ktor.application.call |
| 10 | +import io.ktor.application.log |
| 11 | +import io.ktor.http.ContentType |
| 12 | +import io.ktor.http.HttpStatusCode |
| 13 | +import io.ktor.response.respond |
| 14 | +import io.ktor.response.respondText |
| 15 | +import io.ktor.routing.get |
| 16 | +import io.ktor.routing.routing |
| 17 | +import io.ktor.server.engine.embeddedServer |
| 18 | +import io.ktor.server.netty.Netty |
| 19 | +import kotlinx.coroutines.GlobalScope |
| 20 | +import kotlinx.coroutines.delay |
| 21 | +import kotlinx.coroutines.future.await |
| 22 | +import kotlinx.coroutines.future.future |
| 23 | +import kotlinx.coroutines.launch |
| 24 | +import software.amazon.awssdk.services.sqs.model.Message |
| 25 | +import java.time.Duration |
| 26 | + |
| 27 | +fun main() { |
| 28 | + val sqsAsyncClient = ElasticMqSqsAsyncClient() |
| 29 | + |
| 30 | + val server = embeddedServer(Netty, 8080) { |
| 31 | + val firstQueueUrl = sqsAsyncClient.createRandomQueue().get().queueUrl() |
| 32 | + val firstContainer = messageListener("core-listener", sqsAsyncClient, firstQueueUrl) { |
| 33 | + processor = lambdaProcessor { |
| 34 | + method { message -> |
| 35 | + log.info("Message: {}", message.body()) |
| 36 | + } |
| 37 | + } |
| 38 | + retriever = batchingMessageRetriever { |
| 39 | + batchSize = { 1 } |
| 40 | + batchingPeriod = { Duration.ofSeconds(30) } |
| 41 | + } |
| 42 | + resolver = batchingResolver { |
| 43 | + batchSize = { 1 } |
| 44 | + batchingPeriod = { Duration.ofSeconds(5) } |
| 45 | + } |
| 46 | + broker = concurrentBroker { |
| 47 | + concurrencyLevel = { 5 } |
| 48 | + concurrencyPollingRate = { Duration.ofMinutes(1) } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + val secondQueueUrl = sqsAsyncClient.createRandomQueue().get().queueUrl() |
| 53 | + val secondContainer = prefetchingMessageListener("prefetching-listener", sqsAsyncClient, secondQueueUrl) { |
| 54 | + concurrencyLevel = { 2 } |
| 55 | + desiredPrefetchedMessages = 5 |
| 56 | + maxPrefetchedMessages = 10 |
| 57 | + |
| 58 | + processor = lambdaProcessor { |
| 59 | + methodWithVisibilityExtender { message, _ -> |
| 60 | + log.info("Message: {}", message.body()) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + val thirdQueueUrl = sqsAsyncClient.createRandomQueue().get().queueUrl() |
| 66 | + val thirdContainer = batchingMessageListener("batching-listener", sqsAsyncClient, thirdQueueUrl) { |
| 67 | + concurrencyLevel = { 2 } |
| 68 | + batchSize = { 5 } |
| 69 | + batchingPeriod = { Duration.ofSeconds(5) } |
| 70 | + |
| 71 | + processor = asyncLambdaProcessor { |
| 72 | + method { message -> |
| 73 | + log.info("Processing message: ${message.body()}") |
| 74 | + future { |
| 75 | + something(message) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + routing { |
| 82 | + get("/message/{queue}") { |
| 83 | + val queueIdentifier = call.parameters["queue"] |
| 84 | + val queueUrl: String? = when (queueIdentifier) { |
| 85 | + "one" -> firstQueueUrl |
| 86 | + "two" -> secondQueueUrl |
| 87 | + "three" -> thirdQueueUrl |
| 88 | + else -> null |
| 89 | + } |
| 90 | + |
| 91 | + if (queueUrl == null) { |
| 92 | + call.respond(HttpStatusCode.NotFound, "Unknown queue: $queueIdentifier") |
| 93 | + return@get |
| 94 | + } |
| 95 | + |
| 96 | + val response = sqsAsyncClient.sendMessage { |
| 97 | + it.queueUrl(queueUrl).messageBody("body") |
| 98 | + }.await() |
| 99 | + |
| 100 | + call.respondText("Hello, world! ${response.messageId()}", ContentType.Text.Html) |
| 101 | + } |
| 102 | + |
| 103 | + get("/start/{queue}") { |
| 104 | + val queueIdentifier = call.parameters["queue"] |
| 105 | + val container: MessageListenerContainer? = when (queueIdentifier) { |
| 106 | + "one" -> firstContainer |
| 107 | + "two" -> secondContainer |
| 108 | + "three" -> thirdContainer |
| 109 | + else -> null |
| 110 | + } |
| 111 | + |
| 112 | + if (container == null) { |
| 113 | + call.respond(HttpStatusCode.NotFound, "Unknown queue: $queueIdentifier") |
| 114 | + return@get |
| 115 | + } |
| 116 | + |
| 117 | + container.start() |
| 118 | + |
| 119 | + call.respondText("Container ${queueIdentifier} started", ContentType.Text.Html) |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + get("/stop/{queue}") { |
| 124 | + val queueIdentifier = call.parameters["queue"] |
| 125 | + val container: MessageListenerContainer? = when (queueIdentifier) { |
| 126 | + "one" -> firstContainer |
| 127 | + "two" -> secondContainer |
| 128 | + "three" -> thirdContainer |
| 129 | + else -> null |
| 130 | + } |
| 131 | + |
| 132 | + if (container == null) { |
| 133 | + call.respond(HttpStatusCode.NotFound, "Unknown queue: $queueIdentifier") |
| 134 | + return@get |
| 135 | + } |
| 136 | + |
| 137 | + container.stop() |
| 138 | + |
| 139 | + call.respondText("Container ${queueIdentifier} stopped", ContentType.Text.Html) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + server.start() |
| 144 | + Runtime.getRuntime().addShutdownHook(Thread { |
| 145 | + server.stop(1, 30_000) |
| 146 | + }) |
| 147 | + Thread.currentThread().join() |
| 148 | +} |
| 149 | + |
| 150 | +suspend fun something(@Suppress("UNUSED_PARAMETER") message: Message) = GlobalScope.launch { |
| 151 | + delay(5000) |
| 152 | +} |
0 commit comments