|
27 | 27 | import static org.mockito.Mockito.spy; |
28 | 28 | import static org.mockito.Mockito.when; |
29 | 29 |
|
| 30 | +import java.util.List; |
30 | 31 | import java.util.Map; |
31 | 32 | import java.util.concurrent.CountDownLatch; |
32 | 33 | import java.util.concurrent.TimeUnit; |
|
36 | 37 |
|
37 | 38 | import org.springframework.amqp.core.AcknowledgeMode; |
38 | 39 | import org.springframework.amqp.core.MessageProperties; |
| 40 | +import org.springframework.amqp.rabbit.batch.MessageBatch; |
| 41 | +import org.springframework.amqp.rabbit.batch.SimpleBatchingStrategy; |
39 | 42 | import org.springframework.amqp.rabbit.connection.Connection; |
40 | 43 | import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
41 | 44 | import org.springframework.amqp.rabbit.core.RabbitTemplate; |
@@ -377,6 +380,54 @@ public void testRetryWithinOnMessageGateway() throws Exception { |
377 | 380 | assertThat(errors.receive(0)).isNull(); |
378 | 381 | } |
379 | 382 |
|
| 383 | + @SuppressWarnings({ "unchecked" }) |
| 384 | + @Test |
| 385 | + public void testBatchdAdapter() throws Exception { |
| 386 | + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(mock(ConnectionFactory.class)); |
| 387 | + container.setDeBatchingEnabled(false); |
| 388 | + AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container); |
| 389 | + QueueChannel out = new QueueChannel(); |
| 390 | + adapter.setOutputChannel(out); |
| 391 | + adapter.afterPropertiesSet(); |
| 392 | + ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener(); |
| 393 | + SimpleBatchingStrategy bs = new SimpleBatchingStrategy(2, 10_000, 10_000L); |
| 394 | + MessageProperties messageProperties = new MessageProperties(); |
| 395 | + messageProperties.setContentType("text/plain"); |
| 396 | + org.springframework.amqp.core.Message message = |
| 397 | + new org.springframework.amqp.core.Message("test1".getBytes(), messageProperties); |
| 398 | + bs.addToBatch("foo", "bar", message); |
| 399 | + message = new org.springframework.amqp.core.Message("test2".getBytes(), messageProperties); |
| 400 | + MessageBatch batched = bs.addToBatch("foo", "bar", message); |
| 401 | + listener.onMessage(batched.getMessage(), null); |
| 402 | + Message<?> received = out.receive(); |
| 403 | + assertThat(received).isNotNull(); |
| 404 | + assertThat(((List<String>) received.getPayload())).contains("test1", "test2"); |
| 405 | + } |
| 406 | + |
| 407 | + @SuppressWarnings({ "unchecked" }) |
| 408 | + @Test |
| 409 | + public void testBatchGateway() throws Exception { |
| 410 | + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(mock(ConnectionFactory.class)); |
| 411 | + container.setDeBatchingEnabled(false); |
| 412 | + AmqpInboundGateway adapter = new AmqpInboundGateway(container); |
| 413 | + QueueChannel out = new QueueChannel(); |
| 414 | + adapter.setRequestChannel(out); |
| 415 | + adapter.afterPropertiesSet(); |
| 416 | + ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener(); |
| 417 | + SimpleBatchingStrategy bs = new SimpleBatchingStrategy(2, 10_000, 10_000L); |
| 418 | + MessageProperties messageProperties = new MessageProperties(); |
| 419 | + messageProperties.setContentType("text/plain"); |
| 420 | + org.springframework.amqp.core.Message message = |
| 421 | + new org.springframework.amqp.core.Message("test1".getBytes(), messageProperties); |
| 422 | + bs.addToBatch("foo", "bar", message); |
| 423 | + message = new org.springframework.amqp.core.Message("test2".getBytes(), messageProperties); |
| 424 | + MessageBatch batched = bs.addToBatch("foo", "bar", message); |
| 425 | + listener.onMessage(batched.getMessage(), null); |
| 426 | + Message<?> received = out.receive(); |
| 427 | + assertThat(received).isNotNull(); |
| 428 | + assertThat(((List<String>) received.getPayload())).contains("test1", "test2"); |
| 429 | + } |
| 430 | + |
380 | 431 | public static class Foo { |
381 | 432 |
|
382 | 433 | private String bar; |
|
0 commit comments