Skip to content

Commit 16e03b8

Browse files
authored
Add nullChannel() to Groovy DSL
The Groovy DSL was missing support for the `nullChannel()`, which is a special channel that discards messages (acts like `/dev/null`). This prevents users from using `nullChannel` in their Groovy integration flows. Add unit tests to verify: - `nullChannel` can be used at the end of a flow - `nullChannel` can be used after transform operations - Messages are properly discarded without exceptions * Refactor `nullChannel` tests to use bean configuration The original implementation had left the beans as dead code after initial tests were deemed to weak. However, the dynamic flow increased the amount of code to maintain. Changes include: - Replace dynamic flow creation with `@Bean` configuration methods - Add shared `nullCheckWireTapChannel` bean for message verification - Simplify test methods by using autowired channels - Add `@since 7.0.1` annotation to `nullChannel()` method documentation * Format NullChannel DSL javadoc NullChannel for groovy DSL `see` and `since` annotation were moved to correct location
1 parent e70153e commit 16e03b8

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

spring-integration-groovy/src/main/groovy/org/springframework/integration/groovy/dsl/GroovyIntegrationFlowDefinition.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ class GroovyIntegrationFlowDefinition {
103103
this
104104
}
105105

106+
/**
107+
* Populate a {@link org.springframework.integration.channel.NullChannel} instance
108+
* at the current {@link IntegrationFlow} chain position.
109+
* The nullChannel acts like "/dev/null".
110+
* @since 7.0.1
111+
* @see org.springframework.integration.channel.NullChannel
112+
*/
113+
GroovyIntegrationFlowDefinition nullChannel() {
114+
this.delegate.nullChannel()
115+
this
116+
}
117+
106118
/**
107119
* Populate a {@link org.springframework.integration.dsl.support.MessageChannelReference} instance
108120
* at the current {@link IntegrationFlow} chain position.

spring-integration-groovy/src/test/groovy/org/springframework/integration/groovy/dsl/test/GroovyDslTests.groovy

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,35 @@ class GroovyDslTests {
220220
assert groovyTestService.result.get() == 'TEST'
221221
}
222222

223+
@Autowired
224+
@Qualifier('nullChannelFlow.input')
225+
private MessageChannel nullChannelFlowInput
226+
227+
@Autowired
228+
private MessageChannel nullCheckWireTapChannel
229+
230+
231+
@Test
232+
void 'nullChannelFlow discards messages'() {
233+
nullChannelFlowInput.send MessageBuilder.withPayload('test').build()
234+
def tappedMessage = nullCheckWireTapChannel.receive(1000)
235+
236+
//verify that a message was sent and assume nullChannel discarded it.
237+
assert tappedMessage?.payload == 'test'
238+
}
239+
240+
@Autowired
241+
@Qualifier('nullChannelWithTransformFlow.input')
242+
private MessageChannel nullChannelWithTransformFlowInput
243+
244+
@Test
245+
void 'nullChannel can be used after transform in a flow'() {
246+
nullChannelWithTransformFlowInput.send MessageBuilder.withPayload('test').build()
247+
def tappedMessage = nullCheckWireTapChannel.receive(1000)
248+
//verify that a message was sent, transformed and assume nullChannel discarded it.
249+
assert tappedMessage?.payload == 'TEST'
250+
}
251+
223252
@Configuration(proxyBeanMethods = false)
224253
@EnableIntegration
225254
static class Config {
@@ -361,6 +390,30 @@ class GroovyDslTests {
361390

362391
}
363392

393+
@Bean
394+
nullChannelFlow(@Qualifier('nullCheckWireTapChannel') MessageChannel nullCheckWireTapChannel) {
395+
integrationFlow {
396+
wireTap(nullCheckWireTapChannel)
397+
nullChannel()
398+
}
399+
}
400+
401+
@Bean
402+
nullChannelWithTransformFlow(@Qualifier('nullCheckWireTapChannel') MessageChannel nullCheckWireTapChannel) {
403+
integrationFlow {
404+
transform {
405+
transformer { it.toUpperCase() }
406+
}
407+
wireTap(nullCheckWireTapChannel)
408+
nullChannel()
409+
}
410+
}
411+
412+
@Bean
413+
MessageChannel nullCheckWireTapChannel() {
414+
new QueueChannel()
415+
}
416+
364417
}
365418

366419
@CompileStatic

0 commit comments

Comments
 (0)