Skip to content

Commit 6a19044

Browse files
refs #216: Adds a Kotlin DSL for managing the core library. (#225)
This allows you to more easily create a MessageListenerContainer using a simpler Kotlin DSL format. An example is something like: ``` val container = coreMessageListener("identifier", sqsAsyncClient, queueUrl) { retriever = prefetchingMessageRetriever { desiredPrefetchedMessages = 10 maxPrefetchedMessages = 20 } processor = coreProcessor { argumentResolverService = coreArgumentResolverService(objectMapper) bean = MessageListener() method = MessageListener::class.java.getMethod("listen", String::class.java) } broker = concurrentBroker { concurrencyLevel = { 10 } concurrencyPollingRate = { Duration.ofSeconds(30) } } resolver = batchingResolver { bufferingSizeLimit = { 5 } bufferingTime = { Duration.ofSeconds(2) } } } ```
1 parent fa7758f commit 6a19044

File tree

39 files changed

+2262
-25
lines changed

39 files changed

+2262
-25
lines changed

.markdownlinkcheck.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
},
66
{
77
"pattern": "mailto:*"
8+
},
9+
{
10+
"pattern": "https://coveralls.io.*"
811
}
912
]
1013
}

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,38 @@ to the maximum specified.
305305
10 above the desiredMinPrefetchedMessages will not provide much value as once it has prefetched more than the desired prefetched messages it will
306306
not prefetch anymore.*
307307
308+
### Using the Core Library with a Kotlin DSL
309+
310+
If you are not using Spring and want a way to more easily configure
311+
a [MessageListenerContainer](api/src/main/java/com/jashmore/sqs/container/MessageListenerContainer.java), you can use the
312+
[Core Kotlin DSL](extensions/core-kotlin-dsl) tool.
313+
314+
```kotlin
315+
val container = coreMessageListener("identifier", sqsAsyncClient, queueUrl) {
316+
retriever = prefetchingMessageRetriever {
317+
desiredPrefetchedMessages = 10
318+
maxPrefetchedMessages = 20
319+
}
320+
processor = coreProcessor {
321+
argumentResolverService = coreArgumentResolverService(objectMapper)
322+
bean = MessageListener()
323+
method = MessageListener::class.java.getMethod("listen", String::class.java)
324+
}
325+
broker = concurrentBroker {
326+
concurrencyLevel = { 10 }
327+
concurrencyPollingRate = { Duration.ofSeconds(30) }
328+
}
329+
resolver = batchingResolver {
330+
bufferingSizeLimit = { 5 }
331+
bufferingTime = { Duration.ofSeconds(2) }
332+
}
333+
}
334+
335+
container.start()
336+
```
337+
338+
For more details, see the [Core - How to use the Kotlin DSL](doc/how-to-guides/core/core-how-to-use-kotlin-dsl.md) guide.
339+
308340
### Adding Brave Tracing
309341
310342
If you are using Brave Tracing in your application, for example using Spring Sleuth, you can hook into this system by including the
@@ -355,6 +387,8 @@ module. This allows you to test the performance and usage of each library for di
355387
extending the visibility of a message in the case of long processing so it does not get put back on the queue while processing
356388
1. [How to create a MessageProcessingDecorator](doc/how-to-guides/core/core-how-to-create-a-message-processing-decorator.md): guide for writing your own
357389
decorator to wrap a message listener's processing of a message
390+
1. [How to use the Core Kotlin DSL](doc/how-to-guides/core/core-how-to-use-kotlin-dsl.md): guide for using the core library easier using a Kotlin
391+
DSL for constructing message listeners
358392
1. Spring How To Guides
359393
1. [How to add a custom ArgumentResolver to a Spring application](doc/how-to-guides/spring/spring-how-to-add-custom-argument-resolver.md): useful for
360394
integrating custom argument resolution code to be included in a Spring Application. See [How to implement a custom ArgumentResolver](doc/how-to-guides/core/core-how-to-implement-a-custom-argument-resolver.md)

build.gradle.kts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import com.jashmore.JacocoCoverallsPlugin
2-
import com.jashmore.ReleasePlugin
3-
import com.jashmore.release
1+
import com.jashmore.gradle.JacocoCoverallsPlugin
2+
import com.jashmore.gradle.ReleasePlugin
3+
import com.jashmore.gradle.release
44

55
plugins {
66
java
@@ -20,10 +20,13 @@ allprojects {
2020
}
2121

2222
subprojects {
23+
val isKotlinProject = project.name.contains("kotlin")
2324
apply(plugin = "java-library")
24-
apply(plugin = "com.github.spotbugs")
25-
apply(plugin = "checkstyle")
2625
apply(plugin = "jacoco")
26+
if (!isKotlinProject) {
27+
apply(plugin = "com.github.spotbugs")
28+
apply(plugin = "checkstyle")
29+
}
2730

2831
dependencies {
2932
// AWS
@@ -49,8 +52,10 @@ subprojects {
4952
testImplementation("ch.qos.logback:logback-core")
5053
testImplementation("ch.qos.logback:logback-classic")
5154

52-
// SpotBugs
53-
spotbugs("com.github.spotbugs:spotbugs:4.0.6")
55+
if (!isKotlinProject) {
56+
// SpotBugs
57+
spotbugs("com.github.spotbugs:spotbugs:4.0.6")
58+
}
5459

5560
constraints {
5661
// Jackson
@@ -93,20 +98,22 @@ subprojects {
9398
options.compilerArgs.addAll(setOf("-Xlint:all", "-Werror", "-Xlint:-processing", "-Xlint:-serial"))
9499
}
95100

96-
tasks.withType<Checkstyle> {
97-
// Needed because this is generated code and I am not good enough with gradle to properly exclude these source files
98-
// from only the checkstyleTest task
99-
exclude("**com/jashmore/sqs/extensions/registry/model/*")
100-
}
101+
if (!isKotlinProject) {
102+
tasks.withType<Checkstyle> {
103+
// Needed because this is generated code and I am not good enough with gradle to properly exclude these source files
104+
// from only the checkstyleTest task
105+
exclude("**com/jashmore/sqs/extensions/registry/model/*")
106+
}
101107

102-
checkstyle {
103-
configFile = file("${project.rootDir}/configuration/checkstyle/google_6_18_checkstyle.xml")
104-
maxWarnings = 0
105-
maxErrors = 0
106-
}
108+
checkstyle {
109+
configFile = file("${project.rootDir}/configuration/checkstyle/google_6_18_checkstyle.xml")
110+
maxWarnings = 0
111+
maxErrors = 0
112+
}
107113

108-
spotbugs {
109-
excludeFilter.set(file("${project.rootDir}/configuration/spotbugs/bugsExcludeFilter.xml"))
114+
spotbugs {
115+
excludeFilter.set(file("${project.rootDir}/configuration/spotbugs/bugsExcludeFilter.xml"))
116+
}
110117
}
111118

112119
tasks.withType<Test> {

buildSrc/src/main/kotlin/com/jashmore/JacocoCoverallsPlugin.kt renamed to buildSrc/src/main/kotlin/com/jashmore/gradle/JacocoCoverallsPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jashmore
1+
package com.jashmore.gradle
22

33
import org.gradle.api.Plugin
44
import org.gradle.api.Project

buildSrc/src/main/kotlin/com/jashmore/ReleasePlugin.kt renamed to buildSrc/src/main/kotlin/com/jashmore/gradle/ReleasePlugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.jashmore
1+
package com.jashmore.gradle
22

3-
import com.jashmore.utils.isSnapshotVersion
3+
import com.jashmore.gradle.utils.isSnapshotVersion
44
import io.codearte.gradle.nexus.NexusStagingExtension
55
import io.codearte.gradle.nexus.NexusStagingPlugin
66
import org.gradle.api.Plugin

buildSrc/src/main/kotlin/com/jashmore/utils/VersionUtils.kt renamed to buildSrc/src/main/kotlin/com/jashmore/gradle/utils/VersionUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package com.jashmore.utils
1+
package com.jashmore.gradle.utils
22

33
fun isSnapshotVersion(version: String) = version.endsWith("-SNAPSHOT")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=com.jashmore.gradle.JacocoCoverallsPlugin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=com.jashmore.gradle.ReleasePlugin

doc/how-to-guides/core/core-how-to-add-aws-xray-tracing.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This will extract the tracing header from the SQS message and begin a Xray Segme
3939
## Integration with AWS Xray Instrumentor
4040

4141
If you want tracing information to be included when making client calls to AWS, you can add the
42-
[AWS Xray SDK V2 Instrumentor](https://github.com/aws/aws-xray-sdk-java/tree/master/aws-xray-recorder-sdk-aws-sdk-v2-instrumentor) dependency.
42+
[AWS Xray SDK V2 Instrumentor](https://github.com/aws/aws-xray-sdk-java/tree/master/aws-xray-recorder-sdk-aws-sdk-v2-instrumentor) dependency.
4343

4444
```xml
4545
<dependency>
@@ -87,4 +87,3 @@ application, as all requests out to the SQS server will automatically be traced
8787
maintains its own threads and does not start Xray segments, all calls out to SQS will throw exceptions due to a missing segment.
8888
The [XrayWrappedSqsAsyncClient](../../../extensions/aws-xray-extension/core/src/main/java/com/jashmore/sqs/extensions/xray/client/XrayWrappedSqsAsyncClient.java)
8989
protects against these errors by making sure there is always a segment present when making a call with the client by creating one for you.
90-
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Core - Kotlin DSL
2+
3+
As the [core](../../../core) library can be quite verbose to configure
4+
a [MessageListenerContainer](../../../api/src/main/java/com/jashmore/sqs/container/MessageListenerContainer.java),
5+
the [Core Kotlin DSL](../../../extensions/core-kotlin-dsl) tool can be used to easily set up a message listener.
6+
7+
## Steps
8+
9+
1. Depend on the [Core Kotlin DSL](../../../extensions/core-kotlin-dsl) module:
10+
11+
```kotlin
12+
implementation("com.jashmore:core-kotlin-dsl:${version}")
13+
```
14+
15+
or
16+
17+
```xml
18+
<dependency>
19+
<groupId>com.jashmore</groupId>
20+
<artifactId>core-kotlin-dsl</artifactId>
21+
<version>${version}</version>
22+
</dependency>
23+
```
24+
25+
1. Create the [MessageListenerContainer](../../../api/src/main/java/com/jashmore/sqs/container/MessageListenerContainer.java) using the Kotlin DSL
26+
27+
```kotlin
28+
val container = coreMessageListener("identifier", sqsAsyncClient, queueUrl) {
29+
retriever = prefetchingMessageRetriever {
30+
desiredPrefetchedMessages = 10
31+
maxPrefetchedMessages = 20
32+
}
33+
processor = coreProcessor {
34+
argumentResolverService = coreArgumentResolverService(objectMapper)
35+
bean = MessageListener()
36+
method = MessageListener::class.java.getMethod("listen", String::class.java)
37+
}
38+
broker = concurrentBroker {
39+
concurrencyLevel = { 10 }
40+
concurrencyPollingRate = { Duration.ofSeconds(30) }
41+
}
42+
resolver = batchingResolver {
43+
bufferingSizeLimit = { 5 }
44+
bufferingTime = { Duration.ofSeconds(2) }
45+
}
46+
}
47+
```
48+
49+
1. Start the container as normal
50+
51+
```kotlin
52+
container.start()
53+
```
54+
55+
Check out the [Core Kotlin DSL](../../../extensions/core-kotlin-dsl) for more details about the internals of this module and what you can use.
56+
57+
## Example
58+
59+
A full example of using the Kotlin DSL can be found in the [core-kotlin-example](../../../examples/core-kotlin-example/README.md).

0 commit comments

Comments
 (0)