1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .amqp .rabbit .listener ;
18
18
19
+ import static org .hamcrest .Matchers .equalTo ;
19
20
import static org .junit .Assert .assertEquals ;
21
+ import static org .junit .Assert .assertThat ;
20
22
import static org .junit .Assert .fail ;
21
23
22
24
import java .util .Set ;
27
29
import org .junit .Rule ;
28
30
import org .junit .Test ;
29
31
30
- import org .springframework .amqp .core .Message ;
31
32
import org .springframework .amqp .core .MessageListener ;
32
33
import org .springframework .amqp .core .Queue ;
33
34
import org .springframework .amqp .rabbit .connection .SingleConnectionFactory ;
41
42
42
43
/**
43
44
* @author Gary Russell
45
+ * @author Artem Bilan
44
46
*
45
47
* @since 1.2.1
46
48
*
47
49
*/
48
50
public class SimpleMessageListenerContainerLongTests {
49
51
52
+ private static final String QUEUE = "SimpleMessageListenerContainerLongTests.queue" ;
53
+
54
+ private static final String QUEUE2 = "SimpleMessageListenerContainerLongTests.queue2" ;
55
+
56
+ private static final String QUEUE3 = "SimpleMessageListenerContainerLongTests.queue3" ;
57
+
58
+ private static final String QUEUE4 = "SimpleMessageListenerContainerLongTests.queue4" ;
59
+
50
60
private final Log logger = LogFactory .getLog (SimpleMessageListenerContainerLongTests .class );
51
61
52
62
@ Rule
53
63
public LongRunningIntegrationTest longTest = new LongRunningIntegrationTest ();
54
64
55
65
@ Rule
56
- public BrokerRunning brokerRunning = BrokerRunning .isRunningWithEmptyQueues ("foo" );
66
+ public BrokerRunning brokerRunning = BrokerRunning .isRunningWithEmptyQueues (QUEUE , QUEUE2 , QUEUE3 , QUEUE4 );
57
67
58
68
@ After
59
69
public void tearDown () {
@@ -75,7 +85,7 @@ private void testChangeConsumerCountGuts(boolean transacted) throws Exception {
75
85
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (singleConnectionFactory );
76
86
try {
77
87
container .setMessageListener (new MessageListenerAdapter (this ));
78
- container .setQueueNames ("foo" );
88
+ container .setQueueNames (QUEUE );
79
89
container .setAutoStartup (false );
80
90
container .setConcurrentConsumers (2 );
81
91
container .setChannelTransacted (transacted );
@@ -90,7 +100,7 @@ private void testChangeConsumerCountGuts(boolean transacted) throws Exception {
90
100
for (int i = 0 ; i < 20 ; i ++) {
91
101
template .convertAndSend ("foo" , "foo" );
92
102
}
93
- waitForNConsumers (container , 2 ); // increased consumers due to work
103
+ waitForNConsumers (container , 2 ); // increased consumers due to work
94
104
waitForNConsumers (container , 1 , 20000 ); // should stop the extra consumer after 10 seconds idle
95
105
container .setConcurrentConsumers (3 );
96
106
waitForNConsumers (container , 3 );
@@ -107,11 +117,7 @@ private void testChangeConsumerCountGuts(boolean transacted) throws Exception {
107
117
public void testAddQueuesAndStartInCycle () throws Exception {
108
118
final SingleConnectionFactory connectionFactory = new SingleConnectionFactory ("localhost" );
109
119
final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (connectionFactory );
110
- container .setMessageListener (new MessageListener () {
111
-
112
- @ Override
113
- public void onMessage (Message message ) {
114
- }
120
+ container .setMessageListener ((MessageListener ) message -> {
115
121
});
116
122
container .setConcurrentConsumers (2 );
117
123
container .afterPropertiesSet ();
@@ -138,6 +144,93 @@ public void onMessage(Message message) {
138
144
connectionFactory .destroy ();
139
145
}
140
146
147
+ @ Test
148
+ public void testIncreaseMinAtMax () throws Exception {
149
+ final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory ("localhost" );
150
+ SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (singleConnectionFactory );
151
+ container .setStartConsumerMinInterval (100 );
152
+ container .setConsecutiveActiveTrigger (1 );
153
+ container .setMessageListener ((MessageListener ) m -> {
154
+ try {
155
+ Thread .sleep (50 );
156
+ }
157
+ catch (InterruptedException e ) {
158
+ Thread .currentThread ().interrupt ();
159
+ }
160
+ });
161
+ container .setQueueNames (QUEUE2 );
162
+ container .setConcurrentConsumers (2 );
163
+ container .setMaxConcurrentConsumers (5 );
164
+ container .afterPropertiesSet ();
165
+ container .start ();
166
+ RabbitTemplate template = new RabbitTemplate (singleConnectionFactory );
167
+ for (int i = 0 ; i < 20 ; i ++) {
168
+ template .convertAndSend (QUEUE2 , "foo" );
169
+ }
170
+ waitForNConsumers (container , 5 );
171
+ container .setConcurrentConsumers (4 );
172
+ Set <?> consumers = (Set <?>) TestUtils .getPropertyValue (container , "consumers" );
173
+ assertThat (consumers .size (), equalTo (5 ));
174
+ }
175
+
176
+ @ Test
177
+ public void testDecreaseMinAtMax () throws Exception {
178
+ final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory ("localhost" );
179
+ SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (singleConnectionFactory );
180
+ container .setStartConsumerMinInterval (100 );
181
+ container .setConsecutiveActiveTrigger (1 );
182
+ container .setMessageListener ((MessageListener ) m -> {
183
+ try {
184
+ Thread .sleep (50 );
185
+ }
186
+ catch (InterruptedException e ) {
187
+ Thread .currentThread ().interrupt ();
188
+ }
189
+ });
190
+ container .setQueueNames (QUEUE3 );
191
+ container .setConcurrentConsumers (2 );
192
+ container .setMaxConcurrentConsumers (3 );
193
+ container .afterPropertiesSet ();
194
+ container .start ();
195
+ RabbitTemplate template = new RabbitTemplate (singleConnectionFactory );
196
+ for (int i = 0 ; i < 20 ; i ++) {
197
+ template .convertAndSend (QUEUE3 , "foo" );
198
+ }
199
+ waitForNConsumers (container , 3 );
200
+ container .setConcurrentConsumers (1 );
201
+ Set <?> consumers = (Set <?>) TestUtils .getPropertyValue (container , "consumers" );
202
+ assertThat (consumers .size (), equalTo (3 ));
203
+ }
204
+
205
+ @ Test
206
+ public void testDecreaseMaxAtMax () throws Exception {
207
+ final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory ("localhost" );
208
+ SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (singleConnectionFactory );
209
+ container .setStartConsumerMinInterval (100 );
210
+ container .setConsecutiveActiveTrigger (1 );
211
+ container .setMessageListener ((MessageListener ) m -> {
212
+ try {
213
+ Thread .sleep (50 );
214
+ }
215
+ catch (InterruptedException e ) {
216
+ Thread .currentThread ().interrupt ();
217
+ }
218
+ });
219
+ container .setQueueNames (QUEUE4 );
220
+ container .setConcurrentConsumers (2 );
221
+ container .setMaxConcurrentConsumers (3 );
222
+ container .afterPropertiesSet ();
223
+ container .start ();
224
+ RabbitTemplate template = new RabbitTemplate (singleConnectionFactory );
225
+ for (int i = 0 ; i < 20 ; i ++) {
226
+ template .convertAndSend (QUEUE4 , "foo" );
227
+ }
228
+ waitForNConsumers (container , 3 );
229
+ container .setConcurrentConsumers (1 );
230
+ container .setMaxConcurrentConsumers (1 );
231
+ Set <?> consumers = (Set <?>) TestUtils .getPropertyValue (container , "consumers" );
232
+ assertThat (consumers .size (), equalTo (1 ));
233
+ }
141
234
142
235
public void handleMessage (String foo ) {
143
236
logger .info (foo );
0 commit comments