-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1509: Add Concurrency for Super Streams
Resolves #1509
- Loading branch information
1 parent
52e49cb
commit 506abd5
Showing
5 changed files
with
198 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...c/test/java/org/springframework/rabbit/stream/listener/SuperStreamConcurrentSACTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.rabbit.stream.listener; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.amqp.core.Declarables; | ||
import org.springframework.amqp.core.DirectExchange; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.core.RabbitAdmin; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.rabbit.stream.config.SuperStream; | ||
import org.springframework.rabbit.stream.support.AbstractIntegrationTests; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import com.rabbitmq.stream.Address; | ||
import com.rabbitmq.stream.Environment; | ||
import com.rabbitmq.stream.OffsetSpecification; | ||
|
||
/** | ||
* @author Gary Russell | ||
* @since 3.0 | ||
* | ||
*/ | ||
@SpringJUnitConfig | ||
public class SuperStreamConcurrentSACTests extends AbstractIntegrationTests { | ||
|
||
@Test | ||
void concurrent(@Autowired StreamListenerContainer container, @Autowired RabbitTemplate template, | ||
@Autowired Config config, @Autowired RabbitAdmin admin, | ||
@Autowired Declarables superStream) throws InterruptedException { | ||
|
||
template.getConnectionFactory().createConnection(); | ||
container.start(); | ||
assertThat(config.consumerLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
template.convertAndSend("ss.sac.concurrency.test", "0", "foo"); | ||
template.convertAndSend("ss.sac.concurrency.test", "1", "bar"); | ||
template.convertAndSend("ss.sac.concurrency.test", "2", "baz"); | ||
assertThat(config.messageLatch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
assertThat(config.threads).hasSize(3); | ||
container.stop(); | ||
clean(admin, superStream); | ||
} | ||
|
||
private void clean(RabbitAdmin admin, Declarables declarables) { | ||
declarables.getDeclarablesByType(Queue.class).forEach(queue -> admin.deleteQueue(queue.getName())); | ||
declarables.getDeclarablesByType(DirectExchange.class).forEach(ex -> admin.deleteExchange(ex.getName())); | ||
} | ||
|
||
@Configuration | ||
public static class Config { | ||
|
||
final Set<String> threads = new HashSet<>(); | ||
|
||
final CountDownLatch consumerLatch = new CountDownLatch(3); | ||
|
||
final CountDownLatch messageLatch = new CountDownLatch(3); | ||
|
||
@Bean | ||
CachingConnectionFactory cf() { | ||
return new CachingConnectionFactory("localhost", amqpPort()); | ||
} | ||
|
||
@Bean | ||
RabbitAdmin admin(ConnectionFactory cf) { | ||
return new RabbitAdmin(cf); | ||
} | ||
|
||
@Bean | ||
RabbitTemplate template(ConnectionFactory cf) { | ||
return new RabbitTemplate(cf); | ||
} | ||
|
||
@Bean | ||
SuperStream superStream() { | ||
return new SuperStream("ss.sac.concurrency.test", 3); | ||
} | ||
|
||
@Bean | ||
static Environment environment() { | ||
return Environment.builder() | ||
.addressResolver(add -> new Address("localhost", streamPort())) | ||
.maxConsumersByConnection(1) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
StreamListenerContainer concurrentContainer(Environment env) { | ||
StreamListenerContainer container = new StreamListenerContainer(env); | ||
container.superStream("ss.sac.concurrency.test", "concurrent", 3); | ||
container.setupMessageListener(msg -> { | ||
this.threads.add(Thread.currentThread().getName()); | ||
this.messageLatch.countDown(); | ||
}); | ||
container.setConsumerCustomizer((id, builder) -> { | ||
builder.consumerUpdateListener(context -> { | ||
this.consumerLatch.countDown(); | ||
return OffsetSpecification.last(); | ||
}); | ||
}); | ||
container.setAutoStartup(false); | ||
return container; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters