|
| 1 | +/* |
| 2 | + * Copyright 2019 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.integration.amqp.outbound; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.amqp.AmqpException; |
| 27 | +import org.springframework.amqp.core.Queue; |
| 28 | +import org.springframework.amqp.core.QueueBuilder; |
| 29 | +import org.springframework.amqp.core.QueueBuilder.Overflow; |
| 30 | +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; |
| 31 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 32 | +import org.springframework.amqp.rabbit.core.RabbitAdmin; |
| 33 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 34 | +import org.springframework.amqp.rabbit.junit.RabbitAvailable; |
| 35 | +import org.springframework.amqp.rabbit.junit.RabbitAvailableCondition; |
| 36 | +import org.springframework.beans.factory.annotation.Autowired; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.context.annotation.Configuration; |
| 39 | +import org.springframework.integration.amqp.dsl.Amqp; |
| 40 | +import org.springframework.integration.config.EnableIntegration; |
| 41 | +import org.springframework.integration.dsl.IntegrationFlow; |
| 42 | +import org.springframework.messaging.MessageHandlingException; |
| 43 | +import org.springframework.messaging.support.GenericMessage; |
| 44 | +import org.springframework.test.annotation.DirtiesContext; |
| 45 | +import org.springframework.test.context.junit.jupiter.DisabledIf; |
| 46 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Gary Russell |
| 50 | + * @since 5.2 |
| 51 | + * |
| 52 | + */ |
| 53 | +@SpringJUnitConfig |
| 54 | +@RabbitAvailable(queues = "testConfirmOk") |
| 55 | +@DirtiesContext |
| 56 | +public class AmqpOutboundEndpointTests2 { |
| 57 | + |
| 58 | + @Test |
| 59 | + void testConfirmOk(@Autowired IntegrationFlow flow, @Autowired RabbitTemplate template) { |
| 60 | + flow.getInputChannel().send(new GenericMessage<>("test", Collections.singletonMap("rk", "testConfirmOk"))); |
| 61 | + assertThat(template.receive("testConfirmOk")).isNotNull(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testWithReturn(@Autowired IntegrationFlow flow) { |
| 66 | + assertThatThrownBy(() -> flow.getInputChannel() |
| 67 | + .send(new GenericMessage<>("test", Collections.singletonMap("rk", "junkjunk")))) |
| 68 | + .isInstanceOf(MessageHandlingException.class) |
| 69 | + .hasCauseInstanceOf(AmqpException.class) |
| 70 | + .extracting(ex -> ex.getCause()) |
| 71 | + .extracting(ex -> ex.getMessage()) |
| 72 | + .isEqualTo("Message was returned by the broker"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @DisabledIf("#{systemEnvironment['TRAVIS'] ?: false}") // needs RabbitMQ 3.7 |
| 77 | + void testWithReject(@Autowired IntegrationFlow flow, @Autowired RabbitAdmin admin, |
| 78 | + @Autowired RabbitTemplate template) { |
| 79 | + |
| 80 | + Queue queue = QueueBuilder.nonDurable().autoDelete().maxLength(1).overflow(Overflow.rejectPublish).build(); |
| 81 | + admin.declareQueue(queue); |
| 82 | + flow.getInputChannel().send(new GenericMessage<>("test", Collections.singletonMap("rk", queue.getName()))); |
| 83 | + assertThatThrownBy(() -> flow.getInputChannel() |
| 84 | + .send(new GenericMessage<>("test", Collections.singletonMap("rk", queue.getName())))) |
| 85 | + .hasCauseInstanceOf(AmqpException.class) |
| 86 | + .extracting(ex -> ex.getCause()) |
| 87 | + .extracting(ex -> ex.getMessage()) |
| 88 | + .matches(msg -> msg.matches("Negative publisher confirm received: .*")); |
| 89 | + assertThat(template.receive(queue.getName())).isNotNull(); |
| 90 | + admin.deleteQueue(queue.getName()); |
| 91 | + } |
| 92 | + |
| 93 | + @Configuration(proxyBeanMethods = false) |
| 94 | + @EnableIntegration |
| 95 | + public static class Config { |
| 96 | + |
| 97 | + @Bean |
| 98 | + public IntegrationFlow flow(RabbitTemplate template) { |
| 99 | + return f -> f.handle(Amqp.outboundAdapter(template) |
| 100 | + .exchangeName("") |
| 101 | + .routingKeyFunction(msg -> msg.getHeaders().get("rk", String.class)) |
| 102 | + .confirmCorrelationFunction(msg -> msg) |
| 103 | + .waitForConfirm(true)); |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public CachingConnectionFactory cf() { |
| 108 | + CachingConnectionFactory ccf = new CachingConnectionFactory( |
| 109 | + RabbitAvailableCondition.getBrokerRunning().getConnectionFactory()); |
| 110 | + ccf.setPublisherConfirms(true); |
| 111 | + ccf.setPublisherReturns(true); |
| 112 | + return ccf; |
| 113 | + } |
| 114 | + |
| 115 | + @Bean |
| 116 | + public RabbitTemplate template(ConnectionFactory cf) { |
| 117 | + RabbitTemplate rabbitTemplate = new RabbitTemplate(cf); |
| 118 | + rabbitTemplate.setMandatory(true); |
| 119 | + rabbitTemplate.setReceiveTimeout(10_000); |
| 120 | + return rabbitTemplate; |
| 121 | + } |
| 122 | + |
| 123 | + @Bean |
| 124 | + public RabbitAdmin admin(ConnectionFactory cf) { |
| 125 | + return new RabbitAdmin(cf); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments