Skip to content

Commit 67d2f44

Browse files
committed
Made exchange declaration fashionably late
1 parent b6ecf55 commit 67d2f44

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

test/src/com/rabbitmq/examples/ProducerMain.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,22 @@ private void runIt() throws IOException {
148148

149149
String queueName = "test queue";
150150
_channel.queueDeclare(_ticket, queueName, shouldPersist());
151-
152-
String exchangeName = "test completion";
153-
_channel.exchangeDeclare(_ticket, exchangeName, "fanout", false, false, true, null);
154-
151+
155152
if (shouldCommit()) {
156153
_channel.txSelect();
157154
}
158155
sendBatch(queueName);
159156

160157
if (_sendCompletion) {
158+
// Declaring this exchange as auto-delete is a bit dodgy because of a
159+
// race condition with the consumer declaring the same exchange to be
160+
// auto-delete and hence pulling the rug out from underneath the producer's
161+
// feet.
162+
// Hence we're delaying a possible re-declaration until as late as possible.
163+
// Ideally you would use a global lock around both critical sections,
164+
// but thread safety has gone out of fashion these days.
165+
String exchangeName = "test completion";
166+
_channel.exchangeDeclare(_ticket, exchangeName, "fanout", false, false, true, null);
161167
_channel.basicPublish(_ticket, exchangeName, "", MessageProperties.BASIC, new byte[0]);
162168
if (shouldCommit())
163169
_channel.txCommit();

0 commit comments

Comments
 (0)