@@ -30,6 +30,7 @@ import org.springframework.amqp.rabbit.connection.CachingConnectionFactory
30
30
import org.springframework.amqp.rabbit.core.RabbitTemplate
31
31
import org.springframework.amqp.rabbit.junit.RabbitAvailable
32
32
import org.springframework.amqp.rabbit.junit.RabbitAvailableCondition
33
+ import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer
33
34
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry
34
35
import org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler
35
36
import org.springframework.amqp.utils.test.TestUtils
@@ -44,7 +45,6 @@ import org.springframework.test.annotation.DirtiesContext
44
45
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
45
46
import java.util.concurrent.CountDownLatch
46
47
import java.util.concurrent.TimeUnit
47
- import java.util.concurrent.atomic.AtomicBoolean
48
48
49
49
/* *
50
50
* Kotlin Annotated listener tests.
@@ -63,17 +63,24 @@ class EnableRabbitKotlinTests {
63
63
@Autowired
64
64
private lateinit var config: Config
65
65
66
+ @Autowired
67
+ private lateinit var registry: RabbitListenerEndpointRegistry
68
+
66
69
@Test
67
- fun `send and wait for consume` (@Autowired registry : RabbitListenerEndpointRegistry ) {
70
+ fun `send and wait for consume` () {
68
71
val template = RabbitTemplate (this .config.cf())
69
72
template.setReplyTimeout(10_000 )
70
73
val result = template.convertSendAndReceive(" kotlinQueue" , " test" )
71
74
assertThat(result).isEqualTo(" TEST" )
72
- val listener = registry.getListenerContainer(" single" )?.messageListener
75
+ var listenerContainer = registry.getListenerContainer(" single" ) as AbstractMessageListenerContainer
76
+ assertThat(listenerContainer.acknowledgeMode).isEqualTo(AcknowledgeMode .MANUAL )
77
+ val listener = listenerContainer?.messageListener
73
78
assertThat(listener).isNotNull()
74
79
listener?.let { nonNullableListener ->
75
- assertThat(TestUtils .getPropertyValue(nonNullableListener, " messagingMessageConverter.inferredArgumentType" )
76
- .toString())
80
+ assertThat(
81
+ TestUtils .getPropertyValue(nonNullableListener, " messagingMessageConverter.inferredArgumentType" )
82
+ .toString()
83
+ )
77
84
.isEqualTo(" class java.lang.String" )
78
85
}
79
86
}
@@ -86,6 +93,9 @@ class EnableRabbitKotlinTests {
86
93
assertThat(this .config.batchReceived.await(10 , TimeUnit .SECONDS )).isTrue()
87
94
assertThat(this .config.batch[0 ]).isInstanceOf(Message ::class .java)
88
95
assertThat(this .config.batch.map { m -> String (m.body) }).containsOnly(" test1" , " test2" )
96
+
97
+ var listenerContainer = registry.getListenerContainer(" batch" ) as AbstractMessageListenerContainer
98
+ assertThat(listenerContainer.acknowledgeMode).isEqualTo(AcknowledgeMode .MANUAL )
89
99
}
90
100
91
101
@Test
@@ -95,45 +105,48 @@ class EnableRabbitKotlinTests {
95
105
assertThat(this .config.ehLatch.await(10 , TimeUnit .SECONDS )).isTrue()
96
106
val reply = template.receiveAndConvert(" kotlinReplyQueue" , 10_000 )
97
107
assertThat(reply).isEqualTo(" error processed" )
108
+
109
+ var listenerContainer = registry.getListenerContainer(" multi" ) as AbstractMessageListenerContainer
110
+ assertThat(listenerContainer.acknowledgeMode).isEqualTo(AcknowledgeMode .AUTO )
98
111
}
99
112
100
113
@Configuration
101
114
@EnableRabbit
102
115
class Config {
103
116
104
117
@RabbitListener(id = " single" , queues = [" kotlinQueue" ])
105
- suspend fun handle (@Suppress(" UNUSED_PARAMETER" ) data : String ) : String? {
118
+ suspend fun handle (@Suppress(" UNUSED_PARAMETER" ) data : String ): String? {
106
119
return data.uppercase()
107
120
}
108
121
109
122
val batchReceived = CountDownLatch (1 )
110
123
111
124
lateinit var batch: List <Message >
112
125
113
- @RabbitListener(id = " batch" , queues = [" kotlinBatchQueue" ],
114
- containerFactory = " batchRabbitListenerContainerFactory" )
126
+ @RabbitListener(
127
+ id = " batch" , queues = [" kotlinBatchQueue" ],
128
+ containerFactory = " batchRabbitListenerContainerFactory"
129
+ )
115
130
suspend fun receiveBatch (messages : List <Message >) {
116
131
batch = messages
117
132
batchReceived.countDown()
118
133
}
119
134
120
135
@Bean
121
136
fun rabbitListenerContainerFactory (cf : CachingConnectionFactory ) =
122
- SimpleRabbitListenerContainerFactory ().also {
123
- it.setAcknowledgeMode(AcknowledgeMode .MANUAL )
124
- it.setReceiveTimeout(10 )
125
- it.setConnectionFactory(cf)
126
- }
137
+ SimpleRabbitListenerContainerFactory ().also {
138
+ it.setReceiveTimeout(10 )
139
+ it.setConnectionFactory(cf)
140
+ }
127
141
128
142
@Bean
129
143
fun batchRabbitListenerContainerFactory (cf : CachingConnectionFactory ) =
130
- SimpleRabbitListenerContainerFactory ().also {
131
- it.setAcknowledgeMode(AcknowledgeMode .MANUAL )
132
- it.setConsumerBatchEnabled(true )
133
- it.setDeBatchingEnabled(true )
134
- it.setBatchSize(3 )
135
- it.setConnectionFactory(cf)
136
- }
144
+ SimpleRabbitListenerContainerFactory ().also {
145
+ it.setConsumerBatchEnabled(true )
146
+ it.setDeBatchingEnabled(true )
147
+ it.setBatchSize(3 )
148
+ it.setConnectionFactory(cf)
149
+ }
137
150
138
151
@Bean
139
152
fun cf () = CachingConnectionFactory (RabbitAvailableCondition .getBrokerRunning().connectionFactory)
0 commit comments