|
| 1 | +/* |
| 2 | + * Copyright 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.rabbit.stream.listener; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.amqp.core.Declarables; |
| 31 | +import org.springframework.amqp.core.DirectExchange; |
| 32 | +import org.springframework.amqp.core.Message; |
| 33 | +import org.springframework.amqp.core.Queue; |
| 34 | +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; |
| 35 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 36 | +import org.springframework.amqp.rabbit.core.RabbitAdmin; |
| 37 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 38 | +import org.springframework.beans.factory.annotation.Autowired; |
| 39 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 40 | +import org.springframework.context.ApplicationContext; |
| 41 | +import org.springframework.context.annotation.Bean; |
| 42 | +import org.springframework.context.annotation.Configuration; |
| 43 | +import org.springframework.context.annotation.Scope; |
| 44 | +import org.springframework.rabbit.stream.config.SuperStream; |
| 45 | +import org.springframework.rabbit.stream.support.AbstractIntegrationTests; |
| 46 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 47 | + |
| 48 | +import com.rabbitmq.stream.Address; |
| 49 | +import com.rabbitmq.stream.Environment; |
| 50 | +import com.rabbitmq.stream.OffsetSpecification; |
| 51 | + |
| 52 | +/** |
| 53 | + * @author Gary Russell |
| 54 | + * @since 3.0 |
| 55 | + * |
| 56 | + */ |
| 57 | +@SpringJUnitConfig |
| 58 | +public class SuperStreamSACTests extends AbstractIntegrationTests { |
| 59 | + |
| 60 | + @Test |
| 61 | + void superStream(@Autowired ApplicationContext context, @Autowired RabbitTemplate template, |
| 62 | + @Autowired Environment env, @Autowired Config config, @Autowired RabbitAdmin admin, |
| 63 | + @Autowired Declarables declarables) throws InterruptedException { |
| 64 | + |
| 65 | + template.getConnectionFactory().createConnection(); |
| 66 | + StreamListenerContainer container1 = context.getBean(StreamListenerContainer.class, env, "one"); |
| 67 | + container1.start(); |
| 68 | + StreamListenerContainer container2 = context.getBean(StreamListenerContainer.class, env, "two"); |
| 69 | + container2.start(); |
| 70 | + StreamListenerContainer container3 = context.getBean(StreamListenerContainer.class, env, "three"); |
| 71 | + container3.start(); |
| 72 | + template.convertAndSend("ss.sac.test", "0", "foo"); |
| 73 | + template.convertAndSend("ss.sac.test", "1", "bar"); |
| 74 | + template.convertAndSend("ss.sac.test", "2", "baz"); |
| 75 | + assertThat(config.latch.await(10, TimeUnit.SECONDS)).isTrue(); |
| 76 | + assertThat(config.messages.keySet()).contains("one", "two", "three"); |
| 77 | + assertThat(config.info).contains("one:foo", "two:bar", "three:baz"); |
| 78 | + container1.stop(); |
| 79 | + container2.stop(); |
| 80 | + container3.stop(); |
| 81 | + clean(admin, declarables); |
| 82 | + } |
| 83 | + |
| 84 | + private void clean(RabbitAdmin admin, Declarables declarables) { |
| 85 | + declarables.getDeclarablesByType(Queue.class).forEach(queue -> admin.deleteQueue(queue.getName())); |
| 86 | + declarables.getDeclarablesByType(DirectExchange.class).forEach(ex -> admin.deleteExchange(ex.getName())); |
| 87 | + } |
| 88 | + |
| 89 | + @Configuration |
| 90 | + public static class Config { |
| 91 | + |
| 92 | + final List<String> info = new ArrayList<>(); |
| 93 | + |
| 94 | + final Map<String, Message> messages = new ConcurrentHashMap<>(); |
| 95 | + |
| 96 | + final CountDownLatch latch = new CountDownLatch(3); |
| 97 | + |
| 98 | + @Bean |
| 99 | + CachingConnectionFactory cf() { |
| 100 | + return new CachingConnectionFactory("localhost", amqpPort()); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + RabbitAdmin admin(ConnectionFactory cf) { |
| 105 | + return new RabbitAdmin(cf); |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + RabbitTemplate template(ConnectionFactory cf) { |
| 110 | + return new RabbitTemplate(cf); |
| 111 | + } |
| 112 | + |
| 113 | + @Bean |
| 114 | + SuperStream superStream() { |
| 115 | + return new SuperStream("ss.sac.test", 3); |
| 116 | + } |
| 117 | + |
| 118 | + @Bean |
| 119 | + static Environment environment() { |
| 120 | + return Environment.builder() |
| 121 | + .addressResolver(add -> new Address("localhost", streamPort())) |
| 122 | + .build(); |
| 123 | + } |
| 124 | + |
| 125 | + @Bean |
| 126 | + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) |
| 127 | + StreamListenerContainer container(Environment env, String name) { |
| 128 | + StreamListenerContainer container = new StreamListenerContainer(env); |
| 129 | + container.superStream("ss.sac.test", "test"); |
| 130 | + container.setupMessageListener(msg -> { |
| 131 | + this.messages.put(name, msg); |
| 132 | + this.info.add(name + ":" + new String(msg.getBody())); |
| 133 | + this.latch.countDown(); |
| 134 | + }); |
| 135 | + container.setConsumerCustomizer((id, builder) -> builder.offset(OffsetSpecification.last())); |
| 136 | + container.setAutoStartup(false); |
| 137 | + return container; |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments