|
| 1 | +/* |
| 2 | + * Copyright 2020 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.annotation; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.amqp.core.AnonymousQueue; |
| 27 | +import org.springframework.amqp.core.Message; |
| 28 | +import org.springframework.amqp.core.MessageProperties; |
| 29 | +import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; |
| 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.amqp.support.AmqpHeaders; |
| 37 | +import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter; |
| 38 | +import org.springframework.amqp.support.converter.MessageConversionException; |
| 39 | +import org.springframework.amqp.support.converter.MessageConverter; |
| 40 | +import org.springframework.amqp.support.converter.SimpleMessageConverter; |
| 41 | +import org.springframework.beans.factory.annotation.Autowired; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.messaging.MessageHeaders; |
| 45 | +import org.springframework.messaging.handler.annotation.SendTo; |
| 46 | +import org.springframework.messaging.support.MessageBuilder; |
| 47 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Gary Russell |
| 51 | + * @since 2.5 |
| 52 | + * |
| 53 | + */ |
| 54 | +@RabbitAvailable |
| 55 | +@SpringJUnitConfig |
| 56 | +public class ContentTypeDelegatingMessageConverterIntegrationTests { |
| 57 | + |
| 58 | + @Autowired |
| 59 | + private Config config; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + private RabbitTemplate template; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private AnonymousQueue queue1; |
| 66 | + |
| 67 | + @Test |
| 68 | + void testReplyContentType() throws InterruptedException { |
| 69 | + this.template.convertAndSend(this.queue1.getName(), "foo", msg -> { |
| 70 | + msg.getMessageProperties().setContentType("foo/bar"); |
| 71 | + return msg; |
| 72 | + }); |
| 73 | + assertThat(this.config.latch1.await(10, TimeUnit.SECONDS)).isTrue(); |
| 74 | + assertThat(this.config.replyContentType).isEqualTo("baz/qux"); |
| 75 | + assertThat(this.config.receivedReplyContentType).isEqualTo("baz/qux"); |
| 76 | + |
| 77 | + this.template.convertAndSend(this.queue1.getName(), "bar", msg -> { |
| 78 | + msg.getMessageProperties().setContentType("foo/bar"); |
| 79 | + return msg; |
| 80 | + }); |
| 81 | + assertThat(this.config.latch2.await(10, TimeUnit.SECONDS)).isTrue(); |
| 82 | + assertThat(this.config.replyContentType).isEqualTo("baz/qux"); |
| 83 | + assertThat(this.config.receivedReplyContentType).isEqualTo("foo/bar"); |
| 84 | + } |
| 85 | + |
| 86 | + @Configuration |
| 87 | + @EnableRabbit |
| 88 | + public static class Config { |
| 89 | + |
| 90 | + final CountDownLatch latch1 = new CountDownLatch(1); |
| 91 | + |
| 92 | + final CountDownLatch latch2 = new CountDownLatch(2); |
| 93 | + |
| 94 | + volatile String replyContentType; |
| 95 | + |
| 96 | + volatile String receivedReplyContentType; |
| 97 | + |
| 98 | + @Bean |
| 99 | + public ConnectionFactory cf() { |
| 100 | + return new CachingConnectionFactory(RabbitAvailableCondition.getBrokerRunning().getConnectionFactory()); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() { |
| 105 | + SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); |
| 106 | + factory.setConnectionFactory(cf()); |
| 107 | + ContentTypeDelegatingMessageConverter converter = |
| 108 | + new ContentTypeDelegatingMessageConverter(new SimpleMessageConverter()); |
| 109 | + converter.addDelegate("foo/bar", new MessageConverter() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public Message toMessage(Object object, MessageProperties messageProperties) |
| 113 | + throws MessageConversionException { |
| 114 | + |
| 115 | + return new Message("foo".getBytes(), messageProperties); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Object fromMessage(Message message) throws MessageConversionException { |
| 120 | + return new String(message.getBody()); |
| 121 | + } |
| 122 | + |
| 123 | + }); |
| 124 | + converter.addDelegate("baz/qux", new MessageConverter() { |
| 125 | + |
| 126 | + @Override |
| 127 | + public Message toMessage(Object object, MessageProperties messageProperties) |
| 128 | + throws MessageConversionException { |
| 129 | + |
| 130 | + Config.this.replyContentType = messageProperties.getContentType(); |
| 131 | + messageProperties.setContentType("foo/bar"); |
| 132 | + return new Message("foo".getBytes(), messageProperties); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Object fromMessage(Message message) throws MessageConversionException { |
| 137 | + return new String(message.getBody()); |
| 138 | + } |
| 139 | + |
| 140 | + }); |
| 141 | + factory.setMessageConverter(converter); |
| 142 | + return factory; |
| 143 | + } |
| 144 | + |
| 145 | + @Bean |
| 146 | + public RabbitTemplate template() { |
| 147 | + return new RabbitTemplate(cf()); |
| 148 | + } |
| 149 | + |
| 150 | + @Bean |
| 151 | + public RabbitAdmin admin() { |
| 152 | + return new RabbitAdmin(cf()); |
| 153 | + } |
| 154 | + |
| 155 | + @Bean |
| 156 | + public AnonymousQueue queue1() { |
| 157 | + return new AnonymousQueue(); |
| 158 | + } |
| 159 | + |
| 160 | + @Bean |
| 161 | + public AnonymousQueue queue2() { |
| 162 | + return new AnonymousQueue(); |
| 163 | + } |
| 164 | + |
| 165 | + @RabbitListener(queues = "#{@queue1.name}") |
| 166 | + @SendTo("#{@queue2.name}") |
| 167 | + public org.springframework.messaging.Message<String> listen1(String in) { |
| 168 | + MessageBuilder<String> builder = MessageBuilder.withPayload(in) |
| 169 | + .setHeader(MessageHeaders.CONTENT_TYPE, "baz/qux"); |
| 170 | + if ("bar".equals(in)) { |
| 171 | + builder.setHeader(AmqpHeaders.CONTENT_TYPE_CONVERTER_WINS, true); |
| 172 | + } |
| 173 | + return builder.build(); |
| 174 | + } |
| 175 | + |
| 176 | + @RabbitListener(queues = "#{@queue2.name}") |
| 177 | + public void listen2(Message in) { |
| 178 | + this.receivedReplyContentType = in.getMessageProperties().getContentType(); |
| 179 | + this.latch1.countDown(); |
| 180 | + this.latch2.countDown(); |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments