|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.amqp.rabbit.core; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.AfterAll; |
| 29 | +import org.junit.jupiter.api.BeforeAll; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import org.springframework.amqp.core.Message; |
| 33 | +import org.springframework.amqp.core.MessageBuilder; |
| 34 | +import org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory; |
| 35 | +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; |
| 36 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 37 | +import org.springframework.amqp.rabbit.connection.CorrelationData; |
| 38 | +import org.springframework.amqp.rabbit.connection.PooledChannelConnectionFactory; |
| 39 | +import org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory; |
| 40 | +import org.springframework.amqp.rabbit.junit.BrokerTestUtils; |
| 41 | +import org.springframework.amqp.rabbit.junit.RabbitAvailable; |
| 42 | +import org.springframework.expression.Expression; |
| 43 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Leonardo Ferreira |
| 47 | + * @since 2.4.4 |
| 48 | + */ |
| 49 | +@RabbitAvailable(queues = RabbitTemplateRoutingConnectionFactoryIntegrationTests.ROUTE) |
| 50 | +class RabbitTemplateRoutingConnectionFactoryIntegrationTests { |
| 51 | + |
| 52 | + public static final String ROUTE = "test.queue.RabbitTemplateRoutingConnectionFactoryIntegrationTests"; |
| 53 | + |
| 54 | + private static RabbitTemplate rabbitTemplate; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void create() { |
| 58 | + final com.rabbitmq.client.ConnectionFactory cf = new com.rabbitmq.client.ConnectionFactory(); |
| 59 | + cf.setHost("localhost"); |
| 60 | + cf.setPort(BrokerTestUtils.getPort()); |
| 61 | + |
| 62 | + CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(cf); |
| 63 | + |
| 64 | + cachingConnectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED); |
| 65 | + |
| 66 | + PooledChannelConnectionFactory pooledChannelConnectionFactory = new PooledChannelConnectionFactory(cf); |
| 67 | + |
| 68 | + Map<Object, ConnectionFactory> connectionFactoryMap = new HashMap<>(2); |
| 69 | + connectionFactoryMap.put("true", cachingConnectionFactory); |
| 70 | + connectionFactoryMap.put("false", pooledChannelConnectionFactory); |
| 71 | + |
| 72 | + final AbstractRoutingConnectionFactory routingConnectionFactory = new SimpleRoutingConnectionFactory(); |
| 73 | + routingConnectionFactory.setConsistentConfirmsReturns(false); |
| 74 | + routingConnectionFactory.setDefaultTargetConnectionFactory(pooledChannelConnectionFactory); |
| 75 | + routingConnectionFactory.setTargetConnectionFactories(connectionFactoryMap); |
| 76 | + |
| 77 | + rabbitTemplate = new RabbitTemplate(routingConnectionFactory); |
| 78 | + |
| 79 | + final Expression sendExpression = new SpelExpressionParser().parseExpression( |
| 80 | + "messageProperties.headers['x-use-publisher-confirms'] ?: false"); |
| 81 | + rabbitTemplate.setSendConnectionFactorySelectorExpression(sendExpression); |
| 82 | + } |
| 83 | + |
| 84 | + @AfterAll |
| 85 | + static void cleanUp() { |
| 86 | + rabbitTemplate.destroy(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void sendWithoutConfirmsTest() { |
| 91 | + final String payload = UUID.randomUUID().toString(); |
| 92 | + rabbitTemplate.convertAndSend(ROUTE, (Object) payload, new CorrelationData()); |
| 93 | + assertThat(rabbitTemplate.getUnconfirmedCount()).isZero(); |
| 94 | + |
| 95 | + final Message received = rabbitTemplate.receive(ROUTE, Duration.ofSeconds(3).toMillis()); |
| 96 | + assertThat(received).isNotNull(); |
| 97 | + final String receivedPayload = new String(received.getBody()); |
| 98 | + |
| 99 | + assertThat(receivedPayload).isEqualTo(payload); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void sendWithConfirmsTest() throws Exception { |
| 104 | + final String payload = UUID.randomUUID().toString(); |
| 105 | + final Message message = MessageBuilder.withBody(payload.getBytes(StandardCharsets.UTF_8)) |
| 106 | + .setHeader("x-use-publisher-confirms", "true").build(); |
| 107 | + |
| 108 | + final CorrelationData correlationData = new CorrelationData(); |
| 109 | + rabbitTemplate.send(ROUTE, message, correlationData); |
| 110 | + assertThat(rabbitTemplate.getUnconfirmedCount()).isEqualTo(1); |
| 111 | + |
| 112 | + final CorrelationData.Confirm confirm = correlationData.getFuture().get(10, TimeUnit.SECONDS); |
| 113 | + |
| 114 | + assertThat(confirm.isAck()).isTrue(); |
| 115 | + |
| 116 | + final Message received = rabbitTemplate.receive(ROUTE, Duration.ofSeconds(10).toMillis()); |
| 117 | + assertThat(received).isNotNull(); |
| 118 | + final String receivedPayload = new String(received.getBody()); |
| 119 | + |
| 120 | + assertThat(receivedPayload).isEqualTo(payload); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments