|
| 1 | +package com.jashmore.sqs.core.kotlin.dsl.container |
| 2 | + |
| 3 | +import com.jashmore.sqs.QueueProperties |
| 4 | +import com.jashmore.sqs.broker.concurrent.ConcurrentMessageBrokerProperties |
| 5 | +import com.jashmore.sqs.container.CoreMessageListenerContainer |
| 6 | +import com.jashmore.sqs.container.CoreMessageListenerContainerProperties |
| 7 | +import com.jashmore.sqs.container.MessageListenerContainer |
| 8 | +import com.jashmore.sqs.core.kotlin.dsl.MessageListenerComponentDslMarker |
| 9 | +import com.jashmore.sqs.core.kotlin.dsl.retriever.BatchingMessageRetrieverDslBuilder |
| 10 | +import com.jashmore.sqs.resolver.batching.BatchingMessageResolverProperties |
| 11 | +import com.jashmore.sqs.retriever.MessageRetriever |
| 12 | +import com.jashmore.sqs.retriever.batching.BatchingMessageRetrieverProperties |
| 13 | +import com.jashmore.sqs.retriever.prefetch.PrefetchingMessageRetrieverProperties |
| 14 | +import software.amazon.awssdk.services.sqs.SqsAsyncClient |
| 15 | +import java.time.Duration |
| 16 | + |
| 17 | +@MessageListenerComponentDslMarker |
| 18 | +class BatchingMessageListenerContainerBuilder(identifier: String, |
| 19 | + sqsAsyncClient: SqsAsyncClient, |
| 20 | + queueProperties: QueueProperties) : MessageListenerContainerBuilder(identifier, sqsAsyncClient, queueProperties) { |
| 21 | + /** |
| 22 | + * Supplier for getting the number of messages that should be processed concurrently. |
| 23 | + * |
| 24 | + * Each time a new message is begun to be processed this supplier will be checked and therefore it should be optimised via caching |
| 25 | + * or another method. This field may return different values each time it is checked and therefore the rate of concurrency can |
| 26 | + * be dynamically changed during runtime. |
| 27 | + * |
| 28 | + * @see [ConcurrentMessageBrokerProperties.getConcurrencyLevel] for in-depth details about this field |
| 29 | + */ |
| 30 | + var concurrencyLevel: (() -> Int)? = null |
| 31 | + /** |
| 32 | + * The batch size for the number of messages to receive at once |
| 33 | + * |
| 34 | + * @see BatchingMessageRetrieverProperties.getBatchSize for more details about this field |
| 35 | + * @see BatchingMessageResolverProperties.getBufferingSizeLimit for more details about this field |
| 36 | + */ |
| 37 | + var batchSize: (() -> Int) = { 5 } |
| 38 | + /** |
| 39 | + * The maximum amount of time to wait for the number of messages requested to reach the [BatchingMessageRetrieverDslBuilder.batchSize]. |
| 40 | + * |
| 41 | + * @see BatchingMessageRetrieverProperties.getBatchingPeriod for more details about this field |
| 42 | + * @see BatchingMessageResolverProperties.getBufferingTime for more details about this field |
| 43 | + */ |
| 44 | + var batchingPeriod: (() -> Duration) = { Duration.ofSeconds(2) } |
| 45 | + /** |
| 46 | + * Function for obtaining the visibility timeout for the message being retrieved. |
| 47 | + * |
| 48 | + * @see PrefetchingMessageRetrieverProperties.getMessageVisibilityTimeout for more details about this field |
| 49 | + */ |
| 50 | + var messageVisibility: (() -> Duration?) = { Duration.ofSeconds(30) } |
| 51 | + /** |
| 52 | + * Set whether any extra messages that may have been internally stored in the [MessageRetriever] should be processed before shutting down. |
| 53 | + * |
| 54 | + * @see [CoreMessageListenerContainerProperties.shouldProcessAnyExtraRetrievedMessagesOnShutdown] for more details about this field |
| 55 | + */ |
| 56 | + var processExtraMessagesOnShutdown: Boolean = true |
| 57 | + /** |
| 58 | + * Set whether the message processing threads should be interrupted when a shutdown is requested. |
| 59 | + * |
| 60 | + * @see [CoreMessageListenerContainerProperties.shouldInterruptThreadsProcessingMessagesOnShutdown] for more details about this field |
| 61 | + */ |
| 62 | + var interruptThreadsProcessingMessagesOnShutdown: Boolean = false |
| 63 | + |
| 64 | + override fun invoke(): MessageListenerContainer { |
| 65 | + return coreMessageListener(identifier, sqsAsyncClient, queueProperties) { |
| 66 | + processor = this@BatchingMessageListenerContainerBuilder.processor |
| 67 | + broker = concurrentBroker { |
| 68 | + concurrencyLevel = this@BatchingMessageListenerContainerBuilder.concurrencyLevel |
| 69 | + } |
| 70 | + retriever = batchingMessageRetriever { |
| 71 | + batchSize = this@BatchingMessageListenerContainerBuilder.batchSize |
| 72 | + batchingPeriod = this@BatchingMessageListenerContainerBuilder.batchingPeriod |
| 73 | + messageVisibility = this@BatchingMessageListenerContainerBuilder.messageVisibility |
| 74 | + } |
| 75 | + resolver = batchingResolver { |
| 76 | + batchSize = this@BatchingMessageListenerContainerBuilder.batchSize |
| 77 | + batchingPeriod = this@BatchingMessageListenerContainerBuilder.batchingPeriod |
| 78 | + } |
| 79 | + shutdown { |
| 80 | + shouldInterruptThreadsProcessingMessages = this@BatchingMessageListenerContainerBuilder.interruptThreadsProcessingMessagesOnShutdown |
| 81 | + shouldProcessAnyExtraRetrievedMessages = this@BatchingMessageListenerContainerBuilder.processExtraMessagesOnShutdown |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Build a [CoreMessageListenerContainer] using a Kotlin DSL that will batch requests to retrieve messages to process. |
| 89 | + * |
| 90 | + * This is equivalent to the @QueueListener annotation in the Spring implementation. |
| 91 | + * |
| 92 | + * Usage: |
| 93 | + * |
| 94 | + * ```kotlin |
| 95 | + * val container = batchingMessageListener("identifier", sqsAsyncClient, "url") { |
| 96 | + * concurrencyLevel = { 2 } |
| 97 | + * batchSize = { 5 } |
| 98 | + * batchingPeriod = { Duration.ofSeconds(2) } |
| 99 | + * |
| 100 | + * // other configurations here... |
| 101 | + * } |
| 102 | + * ``` |
| 103 | + * |
| 104 | + * @param identifier the identifier that uniquely identifies this container |
| 105 | + * @param sqsAsyncClient the client for communicating with the SQS server |
| 106 | + * @param queueUrl the URL of the queue to listen to this |
| 107 | + * @param init the function to configure this container |
| 108 | + * @return the message listener container |
| 109 | + */ |
| 110 | +fun batchingMessageListener(identifier: String, |
| 111 | + sqsAsyncClient: SqsAsyncClient, |
| 112 | + queueUrl: String, |
| 113 | + init: BatchingMessageListenerContainerBuilder.() -> Unit): MessageListenerContainer { |
| 114 | + return batchingMessageListener(identifier, sqsAsyncClient, QueueProperties.builder().queueUrl(queueUrl).build(), init) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Build a [CoreMessageListenerContainer] using a Kotlin DSL that will batch requests to retrieve messages to process. |
| 119 | + * |
| 120 | + * This is equivalent to the @QueueListener annotation in the Spring implementation. |
| 121 | + * |
| 122 | + * Usage: |
| 123 | + * |
| 124 | + * ```kotlin |
| 125 | + * val container = batchingMessageListener("identifier", sqsAsyncClient, QueueProperties.builder().queueUrl("url").build()) { |
| 126 | + * concurrencyLevel = { 2 } |
| 127 | + * batchSize = { 5 } |
| 128 | + * batchingPeriod = { Duration.ofSeconds(2) } |
| 129 | + * |
| 130 | + * // other configurations here... |
| 131 | + * } |
| 132 | + * ``` |
| 133 | + * |
| 134 | + * @param identifier the identifier that uniquely identifies this container |
| 135 | + * @param sqsAsyncClient the client for communicating with the SQS server |
| 136 | + * @param queueProperties details about the queue that is being listened to |
| 137 | + * @param init the function to configure this container |
| 138 | + * @return the message listener container |
| 139 | + */ |
| 140 | +fun batchingMessageListener(identifier: String, |
| 141 | + sqsAsyncClient: SqsAsyncClient, |
| 142 | + queueProperties: QueueProperties, |
| 143 | + init: BatchingMessageListenerContainerBuilder.() -> Unit): MessageListenerContainer { |
| 144 | + |
| 145 | + val listener = BatchingMessageListenerContainerBuilder(identifier, sqsAsyncClient, queueProperties) |
| 146 | + listener.init() |
| 147 | + return listener() |
| 148 | +} |
0 commit comments