1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 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.
79
79
import org .springframework .amqp .support .converter .SimpleMessageConverter ;
80
80
import org .springframework .amqp .utils .test .TestUtils ;
81
81
import org .springframework .beans .DirectFieldAccessor ;
82
+ import org .springframework .context .ApplicationContext ;
83
+ import org .springframework .context .event .ContextClosedEvent ;
82
84
import org .springframework .expression .Expression ;
83
85
import org .springframework .expression .spel .standard .SpelExpressionParser ;
84
86
@@ -100,6 +102,8 @@ public class RabbitTemplatePublisherCallbacksIntegration1Tests {
100
102
101
103
public static final String ROUTE = "test.queue.RabbitTemplatePublisherCallbacksIntegrationTests" ;
102
104
105
+ private static final ApplicationContext APPLICATION_CONTEXT = mock ();
106
+
103
107
private final ExecutorService executorService = Executors .newSingleThreadExecutor ();
104
108
105
109
private CachingConnectionFactory connectionFactory ;
@@ -122,25 +126,35 @@ public void create() {
122
126
connectionFactory .setHost ("localhost" );
123
127
connectionFactory .setChannelCacheSize (10 );
124
128
connectionFactory .setPort (BrokerTestUtils .getPort ());
129
+ connectionFactory .setApplicationContext (APPLICATION_CONTEXT );
130
+
125
131
connectionFactoryWithConfirmsEnabled = new CachingConnectionFactory ();
126
132
connectionFactoryWithConfirmsEnabled .setHost ("localhost" );
127
133
connectionFactoryWithConfirmsEnabled .setChannelCacheSize (100 );
128
134
connectionFactoryWithConfirmsEnabled .setPort (BrokerTestUtils .getPort ());
129
135
connectionFactoryWithConfirmsEnabled .setPublisherConfirmType (ConfirmType .CORRELATED );
136
+ connectionFactoryWithConfirmsEnabled .setApplicationContext (APPLICATION_CONTEXT );
137
+
130
138
templateWithConfirmsEnabled = new RabbitTemplate (connectionFactoryWithConfirmsEnabled );
139
+
131
140
connectionFactoryWithReturnsEnabled = new CachingConnectionFactory ();
132
141
connectionFactoryWithReturnsEnabled .setHost ("localhost" );
133
142
connectionFactoryWithReturnsEnabled .setChannelCacheSize (1 );
134
143
connectionFactoryWithReturnsEnabled .setPort (BrokerTestUtils .getPort ());
135
144
connectionFactoryWithReturnsEnabled .setPublisherReturns (true );
145
+ connectionFactoryWithReturnsEnabled .setApplicationContext (APPLICATION_CONTEXT );
146
+
136
147
templateWithReturnsEnabled = new RabbitTemplate (connectionFactoryWithReturnsEnabled );
137
148
templateWithReturnsEnabled .setMandatory (true );
149
+
138
150
connectionFactoryWithConfirmsAndReturnsEnabled = new CachingConnectionFactory ();
139
151
connectionFactoryWithConfirmsAndReturnsEnabled .setHost ("localhost" );
140
152
connectionFactoryWithConfirmsAndReturnsEnabled .setChannelCacheSize (100 );
141
153
connectionFactoryWithConfirmsAndReturnsEnabled .setPort (BrokerTestUtils .getPort ());
142
154
connectionFactoryWithConfirmsAndReturnsEnabled .setPublisherConfirmType (ConfirmType .CORRELATED );
143
155
connectionFactoryWithConfirmsAndReturnsEnabled .setPublisherReturns (true );
156
+ connectionFactoryWithConfirmsAndReturnsEnabled .setApplicationContext (APPLICATION_CONTEXT );
157
+
144
158
templateWithConfirmsAndReturnsEnabled = new RabbitTemplate (connectionFactoryWithConfirmsAndReturnsEnabled );
145
159
templateWithConfirmsAndReturnsEnabled .setMandatory (true );
146
160
}
@@ -149,17 +163,27 @@ public void create() {
149
163
public void cleanUp () {
150
164
this .templateWithConfirmsEnabled .stop ();
151
165
this .templateWithReturnsEnabled .stop ();
166
+
167
+ this .connectionFactory .onApplicationEvent (new ContextClosedEvent (APPLICATION_CONTEXT ));
152
168
this .connectionFactory .destroy ();
169
+
170
+ this .connectionFactoryWithConfirmsEnabled .onApplicationEvent (new ContextClosedEvent (APPLICATION_CONTEXT ));
153
171
this .connectionFactoryWithConfirmsEnabled .destroy ();
172
+
173
+ this .connectionFactoryWithReturnsEnabled .onApplicationEvent (new ContextClosedEvent (APPLICATION_CONTEXT ));
154
174
this .connectionFactoryWithReturnsEnabled .destroy ();
175
+
176
+ this .connectionFactoryWithConfirmsAndReturnsEnabled .onApplicationEvent (new ContextClosedEvent (APPLICATION_CONTEXT ));
177
+ this .connectionFactoryWithConfirmsAndReturnsEnabled .destroy ();
178
+
155
179
this .executorService .shutdown ();
156
180
}
157
181
158
182
@ Test
159
183
public void testPublisherConfirmReceived () throws Exception {
160
184
final CountDownLatch latch = new CountDownLatch (10000 );
161
185
final AtomicInteger acks = new AtomicInteger ();
162
- final AtomicReference <CorrelationData > confirmCorrelation = new AtomicReference <CorrelationData >();
186
+ final AtomicReference <CorrelationData > confirmCorrelation = new AtomicReference <>();
163
187
AtomicReference <String > callbackThreadName = new AtomicReference <>();
164
188
this .templateWithConfirmsEnabled .setConfirmCallback ((correlationData , ack , cause ) -> {
165
189
acks .incrementAndGet ();
@@ -208,7 +232,7 @@ public Message postProcessMessage(Message message, Correlation correlation, Stri
208
232
this .templateWithConfirmsEnabled .execute (channel -> {
209
233
Map <?, ?> listenerMap = TestUtils .getPropertyValue (((ChannelProxy ) channel ).getTargetChannel (),
210
234
"listenerForSeq" , Map .class );
211
- await ().until (() -> listenerMap . size () == 0 );
235
+ await ().until (listenerMap :: isEmpty );
212
236
return null ;
213
237
});
214
238
@@ -227,7 +251,8 @@ public void testPublisherConfirmWithSendAndReceive() throws Exception {
227
251
confirmCD .set (correlationData );
228
252
latch .countDown ();
229
253
});
230
- SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (this .connectionFactoryWithConfirmsEnabled );
254
+ SimpleMessageListenerContainer container =
255
+ new SimpleMessageListenerContainer (this .connectionFactoryWithConfirmsEnabled );
231
256
container .setQueueNames (ROUTE );
232
257
container .setReceiveTimeout (10 );
233
258
container .setMessageListener (
@@ -643,7 +668,6 @@ public void testConcurrentConfirms() throws Exception {
643
668
assertThat (waitForAll3AcksLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
644
669
assertThat (acks .get ()).isEqualTo (3 );
645
670
646
-
647
671
channel .basicConsume ("foo" , false , (Map ) null , null );
648
672
verify (mockChannel ).basicConsume ("foo" , false , (Map ) null , null );
649
673
0 commit comments