-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MessagingAnnotationsWithBeanAnnotationTests.java
462 lines (378 loc) · 14.9 KB
/
MessagingAnnotationsWithBeanAnnotationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.support.BeanDefinitionValidationException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.MessageRejectedException;
import org.springframework.integration.aggregator.AggregatingMessageHandler;
import org.springframework.integration.aggregator.ExpressionEvaluatingCorrelationStrategy;
import org.springframework.integration.aggregator.ExpressionEvaluatingReleaseStrategy;
import org.springframework.integration.aggregator.SimpleMessageGroupProcessor;
import org.springframework.integration.annotation.BridgeFrom;
import org.springframework.integration.annotation.BridgeTo;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Splitter;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.FluxMessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.config.EnableMessageHistory;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.filter.ExpressionEvaluatingSelector;
import org.springframework.integration.history.MessageHistory;
import org.springframework.integration.splitter.DefaultMessageSplitter;
import org.springframework.integration.transformer.ExpressionEvaluatingTransformer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.ReactiveMessageHandler;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
import reactor.test.StepVerifier;
/**
* @author Artem Bilan
* @author Gary Russell
* @author Oleg Zhurakousky
*
* @since 4.0
*/
@ContextConfiguration(classes = MessagingAnnotationsWithBeanAnnotationTests.ContextConfiguration.class)
@SpringJUnitConfig
@DirtiesContext
public class MessagingAnnotationsWithBeanAnnotationTests {
@Autowired
private SourcePollingChannelAdapter[] sourcePollingChannelAdapters;
@Autowired
private PollableChannel discardChannel;
@Resource(name = "collector")
private List<Message<?>> collector;
@Autowired(required = false)
@Qualifier(
"messagingAnnotationsWithBeanAnnotationTests.ContextConfiguration.skippedMessageHandler.serviceActivator")
private EventDrivenConsumer skippedServiceActivator;
@Autowired(required = false)
@Qualifier("skippedMessageHandler")
private MessageHandler skippedMessageHandler;
@Autowired(required = false)
@Qualifier("skippedChannel")
private MessageChannel skippedChannel;
@Autowired(required = false)
@Qualifier("skippedChannel2")
private MessageChannel skippedChannel2;
@Autowired(required = false)
@Qualifier("skippedMessageSource")
private MessageSource<?> skippedMessageSource;
@Autowired
private PollableChannel counterErrorChannel;
@Autowired
private MessageChannel functionServiceChannel;
@Autowired
private MessageChannel functionMessageServiceChannel;
@Autowired
private MessageChannel consumerServiceChannel;
@Autowired
private List<String> stringCollector;
@Autowired
private MessageChannel messageConsumerServiceChannel;
@Test
public void testMessagingAnnotationsFlow() {
Stream.of(this.sourcePollingChannelAdapters).forEach(AbstractEndpoint::start);
//this.sourcePollingChannelAdapter.start();
for (int i = 0; i < 10; i++) {
Message<?> receive = this.discardChannel.receive(10000);
assertThat(receive).isNotNull();
assertThat(((Integer) receive.getPayload()) % 2).isEqualTo(0);
receive = this.counterErrorChannel.receive(10000);
assertThat(receive).isNotNull();
assertThat(receive).isInstanceOf(ErrorMessage.class);
assertThat(receive.getPayload()).isInstanceOf(MessageRejectedException.class);
MessageRejectedException exception = (MessageRejectedException) receive.getPayload();
assertThat(exception.getMessage())
.contains("message has been rejected in filter: bean " +
"'messagingAnnotationsWithBeanAnnotationTests.ContextConfiguration.filter.filter.handler'");
}
for (Message<?> message : this.collector) {
assertThat(((Integer) message.getPayload()) % 2).isNotEqualTo(0);
MessageHistory messageHistory = MessageHistory.read(message);
assertThat(messageHistory).isNotNull();
String messageHistoryString = messageHistory.toString();
assertThat(messageHistoryString).contains("routerChannel")
.contains("filterChannel")
.contains("aggregatorChannel")
.contains("splitterChannel")
.contains("serviceChannel")
.doesNotContain("discardChannel");
}
assertThat(this.skippedServiceActivator).isNull();
assertThat(this.skippedMessageHandler).isNull();
assertThat(this.skippedChannel).isNull();
assertThat(this.skippedChannel2).isNull();
assertThat(this.skippedMessageSource).isNull();
QueueChannel replyChannel = new QueueChannel();
Message<String> message = MessageBuilder.withPayload("foo")
.setReplyChannel(replyChannel)
.build();
this.functionServiceChannel.send(message);
Message<?> receive = replyChannel.receive(10_000);
assertThat(receive).isNotNull();
assertThat(receive.getPayload()).isEqualTo("FOO");
message = MessageBuilder.withPayload("BAR")
.setReplyChannel(replyChannel)
.build();
this.functionMessageServiceChannel.send(message);
receive = replyChannel.receive(10_000);
assertThat(receive).isNotNull();
assertThat(receive.getPayload()).isEqualTo("bar");
this.consumerServiceChannel.send(new GenericMessage<>("baz"));
assertThat(this.stringCollector.isEmpty()).isFalse();
assertThat(this.stringCollector.iterator().next()).isEqualTo("baz");
this.collector.clear();
this.messageConsumerServiceChannel.send(new GenericMessage<>("123"));
assertThat(this.collector.isEmpty()).isFalse();
Message<?> next = this.collector.iterator().next();
assertThat(next.getPayload()).isEqualTo("123");
}
@Test
public void testInvalidMessagingAnnotationsConfig() {
assertThatExceptionOfType(BeanDefinitionValidationException.class)
.isThrownBy(() -> new AnnotationConfigApplicationContext(InvalidContextConfiguration.class))
.withMessageContaining("The attribute causing the ambiguity is: [applySequence].");
}
@Autowired
private MessageChannel reactiveMessageHandlerChannel;
@Autowired
private ContextConfiguration contextConfiguration;
@Test
public void testReactiveMessageHandler() {
this.reactiveMessageHandlerChannel.send(new GenericMessage<>("test"));
StepVerifier.create(
this.contextConfiguration.messageMono
.asMono()
.map(Message::getPayload)
.cast(String.class))
.expectNext("test")
.verifyComplete();
}
@Configuration
@EnableIntegration
@EnableMessageHistory
public static class ContextConfiguration {
private static final ExpressionParser PARSER = new SpelExpressionParser();
@Bean(IntegrationContextUtils.MESSAGE_HANDLER_FACTORY_BEAN_NAME)
public MessageHandlerMethodFactory messageHandlerMethodFactory() {
return new DefaultMessageHandlerMethodFactory();
}
@Bean
public AtomicInteger counter() {
return new AtomicInteger();
}
@Bean
@InboundChannelAdapter(value = "routerChannel", autoStartup = "false",
poller = @Poller(fixedRate = "10", maxMessagesPerPoll = "1", errorChannel = "counterErrorChannel"))
public MessageSource<Integer> counterMessageSource(final AtomicInteger counter) {
return () -> new GenericMessage<>(counter.incrementAndGet());
}
@Bean
@InboundChannelAdapter(value = "routerChannel", autoStartup = "false",
poller = @Poller(fixedRate = "10", maxMessagesPerPoll = "1", errorChannel = "counterErrorChannel"))
public Supplier<Integer> counterMessageSupplier(final AtomicInteger counter) {
return counter::incrementAndGet;
}
@Bean
public PollableChannel counterErrorChannel() {
return new QueueChannel();
}
@Bean
public MessageChannel routerChannel() {
return new DirectChannel();
}
@Bean
@Router(inputChannel = "routerChannel", channelMappings = {"true=odd", "false=filter"}, suffix = "Channel")
public MessageSelector router() {
return new ExpressionEvaluatingSelector("payload % 2 == 0");
}
@Bean
@Transformer(inputChannel = "oddChannel", outputChannel = "filterChannel")
public ExpressionEvaluatingTransformer oddTransformer() {
return new ExpressionEvaluatingTransformer(PARSER.parseExpression("payload / 2"));
}
@Bean
public MessageChannel filterChannel() {
return new DirectChannel();
}
@Bean
@Filter(inputChannel = "filterChannel",
outputChannel = "aggregatorChannel",
discardChannel = "discardChannel",
throwExceptionOnRejection = "true")
public MessageSelector filter() {
return new ExpressionEvaluatingSelector("payload % 2 != 0");
}
@Bean
public MessageChannel aggregatorChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "aggregatorChannel")
public MessageHandler aggregator() {
AggregatingMessageHandler handler = new AggregatingMessageHandler(new SimpleMessageGroupProcessor());
handler.setCorrelationStrategy(new ExpressionEvaluatingCorrelationStrategy("1"));
handler.setReleaseStrategy(new ExpressionEvaluatingReleaseStrategy("size() == 10"));
handler.setOutputChannelName("splitterChannel");
return handler;
}
@Bean
public MessageChannel splitterChannel() {
return new DirectChannel();
}
@Bean
@Splitter(inputChannel = "splitterChannel")
public MessageHandler splitter() {
DefaultMessageSplitter defaultMessageSplitter = new DefaultMessageSplitter();
defaultMessageSplitter.setOutputChannelName("serviceChannel");
return defaultMessageSplitter;
}
@Bean
public PollableChannel discardChannel() {
return new QueueChannel();
}
@Bean
public List<Message<?>> collector() {
return new ArrayList<>();
}
@Bean
public MessageChannel serviceChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "serviceChannel")
public MessageHandler service() {
return collector()::add;
}
@Bean
@ServiceActivator(inputChannel = "skippedChannel")
@Splitter(inputChannel = "skippedChannel2")
@Router(inputChannel = "skippedChannel3")
@Transformer(inputChannel = "skippedChannel4")
@Filter(inputChannel = "skippedChannel5")
@Profile("foo")
public MessageHandler skippedMessageHandler() {
return m -> {
};
}
@Bean
@BridgeFrom("skippedChannel6")
@Profile("foo")
public MessageChannel skippedChannel1() {
return new DirectChannel();
}
@Bean
@BridgeTo
@Profile("foo")
public MessageChannel skippedChannel2() {
return new DirectChannel();
}
@Bean
@InboundChannelAdapter("serviceChannel")
@Profile("foo")
public MessageSource<?> skippedMessageSource() {
return () -> new GenericMessage<>("foo");
}
@Bean
@Transformer(inputChannel = "functionServiceChannel")
public Function<String, String> functionAsService() {
return String::toUpperCase;
}
@Bean
@ServiceActivator(inputChannel = "functionMessageServiceChannel")
public Function<Message<String>, String> messageFunctionAsService() {
return (message) -> message.getPayload().toLowerCase();
}
@Bean
public List<String> stringCollector() {
return new ArrayList<>();
}
@Bean
@ServiceActivator(inputChannel = "consumerServiceChannel")
public Consumer<String> consumerAsService() {
return stringCollector()::add;
}
@Bean
@ServiceActivator(inputChannel = "messageConsumerServiceChannel")
public Consumer<Message<?>> messageConsumerAsService() {
return collector()::add;
}
Sinks.StandaloneMonoSink<Message<?>> messageMono = Sinks.promise();
@Bean
MessageChannel reactiveMessageHandlerChannel() {
return new FluxMessageChannel();
}
@Bean
@ServiceActivator(inputChannel = "reactiveMessageHandlerChannel")
public ReactiveMessageHandler reactiveMessageHandlerService() {
return (message) -> {
messageMono.success(message);
return Mono.empty();
};
}
}
@Configuration
@EnableIntegration
static class InvalidContextConfiguration {
@Bean
@Splitter(inputChannel = "splitterChannel", applySequence = "false")
public MessageHandler splitter() {
return new DefaultMessageSplitter();
}
}
}