@@ -38,64 +38,81 @@ public class Confirm extends ConfirmBase
3838
3939 private static final String TTL_ARG = "x-message-ttl" ;
4040
41+ private DefaultConsumer defaultConsumer = null ;
42+
4143 @ Override
42- protected void setUp () throws IOException {
43- super .setUp ();
44+ protected void createResources () throws IOException {
4445 channel .confirmSelect ();
45- channel .queueDeclare ("confirm-test" , true , true , false , null );
46- channel .basicConsume ("confirm-test" , true ,
47- new DefaultConsumer (channel ));
48- channel .queueDeclare ("confirm-test-nondurable" , false , true ,
49- false , null );
50- channel .basicConsume ("confirm-test-nondurable" , true ,
51- new DefaultConsumer (channel ));
52- channel .queueDeclare ("confirm-test-noconsumer" , true ,
53- true , false , null );
54- channel .queueDeclare ("confirm-test-2" , true , true , false , null );
55- channel .basicConsume ("confirm-test-2" , true ,
56- new DefaultConsumer (channel ));
57- Map <String , Object > argMap =
58- Collections .singletonMap (TTL_ARG , (Object )1 );
59- channel .queueDeclare ("confirm-ttl" , true , true , false , argMap );
60- channel .queueBind ("confirm-test" , "amq.direct" ,
61- "confirm-multiple-queues" );
62- channel .queueBind ("confirm-test-2" , "amq.direct" ,
63- "confirm-multiple-queues" );
46+ defaultConsumer = new DefaultConsumer (channel );
47+ }
48+
49+ @ Override
50+ protected void releaseResources () throws IOException {
51+ defaultConsumer = null ;
52+ }
53+
54+ private void declareQueue (String queueName , boolean durable )
55+ throws IOException {
56+ declareQueue (queueName , durable , null );
57+ }
58+
59+ private void declareQueue (String queueName , boolean durable ,
60+ Map <String , Object > args )
61+ throws IOException {
62+ channel .queueDeclare (queueName , durable , true , false , args );
63+ }
64+
65+ private void declareConsumeQueue (String queueName , boolean durable )
66+ throws IOException {
67+ declareQueue (queueName , durable );
68+ channel .basicConsume (queueName , true , defaultConsumer );
69+ }
70+
71+ private void declareBindQueue (String queueName , boolean durable )
72+ throws IOException {
73+ declareConsumeQueue (queueName , durable );
74+ channel .queueBind (queueName , "amq.direct" , "confirm-multiple-queues" );
6475 }
6576
6677 public void testTransient ()
6778 throws Exception
6879 {
80+ declareConsumeQueue ("confirm-test" , true );
6981 confirmTest ("" , "confirm-test" , false , false , false );
7082 }
7183
7284 public void testPersistentSimple ()
7385 throws Exception
7486 {
87+ declareConsumeQueue ("confirm-test" , true );
7588 confirmTest ("" , "confirm-test" , true , false , false );
7689 }
7790
7891 public void testNonDurable ()
7992 throws Exception
8093 {
94+ declareConsumeQueue ("confirm-test-nondurable" , false );
8195 confirmTest ("" , "confirm-test-nondurable" , true , false , false );
8296 }
8397
8498 public void testPersistentImmediate ()
8599 throws Exception
86100 {
101+ declareConsumeQueue ("confirm-test" , true );
87102 confirmTest ("" , "confirm-test" , true , false , true );
88103 }
89104
90105 public void testPersistentImmediateNoConsumer ()
91106 throws Exception
92107 {
108+ declareQueue ("confirm-test-noconsumer" , true );
93109 confirmTest ("" , "confirm-test-noconsumer" , true , false , true );
94110 }
95111
96112 public void testPersistentMandatory ()
97113 throws Exception
98114 {
115+ declareConsumeQueue ("confirm-test" , true );
99116 confirmTest ("" , "confirm-test" , true , true , false );
100117 }
101118
@@ -108,6 +125,8 @@ public void testPersistentMandatoryReturn()
108125 public void testMultipleQueues ()
109126 throws Exception
110127 {
128+ declareBindQueue ("confirm-test" , true );
129+ declareBindQueue ("confirm-test-2" , true );
111130 confirmTest ("amq.direct" , "confirm-multiple-queues" ,
112131 true , false , false );
113132 }
@@ -120,6 +139,8 @@ public void testMultipleQueues()
120139 public void testQueueDelete ()
121140 throws Exception
122141 {
142+ declareQueue ("confirm-test-noconsumer" , true );
143+
123144 publishN ("" ,"confirm-test-noconsumer" , true , false , false );
124145
125146 channel .queueDelete ("confirm-test-noconsumer" );
@@ -130,6 +151,8 @@ public void testQueueDelete()
130151 public void testQueuePurge ()
131152 throws Exception
132153 {
154+ declareQueue ("confirm-test-noconsumer" , true );
155+
133156 publishN ("" , "confirm-test-noconsumer" , true , false , false );
134157
135158 channel .queuePurge ("confirm-test-noconsumer" );
@@ -140,14 +163,19 @@ public void testQueuePurge()
140163 public void testBasicReject ()
141164 throws Exception
142165 {
143- basicRejectCommon (false );
166+ declareQueue ("confirm-test-noconsumer" , true );
167+
168+ basicRejectCommon ("confirm-test-noconsumer" , false );
144169
145170 waitForConfirms ();
146171 }
147172
148173 public void testQueueTTL ()
149174 throws Exception
150175 {
176+ declareQueue ("confirm-ttl" , true ,
177+ Collections .singletonMap (TTL_ARG , (Object )Long .valueOf (1L )));
178+
151179 publishN ("" , "confirm-ttl" , true , false , false );
152180
153181 waitForConfirms ();
@@ -156,20 +184,24 @@ public void testQueueTTL()
156184 public void testBasicRejectRequeue ()
157185 throws Exception
158186 {
159- basicRejectCommon (true );
187+ declareQueue ("confirm-test-noconsumer" , true );
188+
189+ basicRejectCommon ("confirm-test-noconsumer" , true );
160190
161191 /* wait confirms to go through the broker */
162- Thread .sleep (1000 );
192+ // Thread.sleep(1000);
163193
164194 channel .basicConsume ("confirm-test-noconsumer" , true ,
165- new DefaultConsumer ( channel ) );
195+ defaultConsumer );
166196
167197 waitForConfirms ();
168198 }
169199
170200 public void testBasicRecover ()
171201 throws Exception
172202 {
203+ declareQueue ("confirm-test-noconsumer" , true );
204+
173205 publishN ("" , "confirm-test-noconsumer" , true , false , false );
174206
175207 for (long i = 0 ; i < NUM_MESSAGES ; i ++) {
@@ -181,10 +213,10 @@ public void testBasicRecover()
181213
182214 channel .basicRecover (true );
183215
184- Thread .sleep (1000 );
216+ // Thread.sleep(1000);
185217
186218 channel .basicConsume ("confirm-test-noconsumer" , true ,
187- new DefaultConsumer ( channel ) );
219+ defaultConsumer );
188220
189221 waitForConfirms ();
190222 }
@@ -214,6 +246,8 @@ public void testSelect()
214246 public void testWaitForConfirms ()
215247 throws Exception
216248 {
249+ declareConsumeQueue ("confirm-test" , true );
250+
217251 final SortedSet <Long > unconfirmedSet =
218252 Collections .synchronizedSortedSet (new TreeSet <Long >());
219253 channel .addConfirmListener (new ConfirmListener () {
@@ -247,6 +281,8 @@ public void handleNack(long seqNo, boolean multiple) {
247281 public void testWaitForConfirmsNoOp ()
248282 throws Exception
249283 {
284+ declareConsumeQueue ("confirm-test" , true );
285+
250286 channel = connection .createChannel ();
251287 // Don't enable Confirm mode
252288 publish ("" , "confirm-test" , true , false , false );
@@ -256,6 +292,8 @@ public void testWaitForConfirmsNoOp()
256292 public void testWaitForConfirmsException ()
257293 throws Exception
258294 {
295+ declareConsumeQueue ("confirm-test" , true );
296+
259297 publishN ("" , "confirm-test" , true , false , false );
260298 channel .close ();
261299 try {
@@ -291,14 +329,14 @@ private void publishN(String exchangeName, String queueName,
291329 }
292330 }
293331
294- private void basicRejectCommon (boolean requeue )
332+ private void basicRejectCommon (String queueName , boolean requeue )
295333 throws Exception
296334 {
297- publishN ("" , "confirm-test-noconsumer" , true , false , false );
335+ publishN ("" , queueName , true , false , false );
298336
299337 for (long i = 0 ; i < NUM_MESSAGES ; i ++) {
300338 GetResponse resp =
301- channel .basicGet ("confirm-test-noconsumer" , false );
339+ channel .basicGet (queueName , false );
302340 long dtag = resp .getEnvelope ().getDeliveryTag ();
303341 channel .basicReject (dtag , requeue );
304342 }
0 commit comments