Skip to content

Commit cd3ffbf

Browse files
Added support for anonymous consumer queues (just specify an empty string as the queue). (Minor bug fix.)
1 parent 8d6de8f commit cd3ffbf

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

logback-amqp-appender/src/main/java/ch/qos/logback/amqp/AmqpPublisher.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ protected final void loop ()
2929
synchronized (this) {
3030
if (this.shouldStopLoop ())
3131
break loop;
32-
if (!this.shouldReconnect ())
33-
break;
3432
if (this.reconnect ())
3533
break;
3634
}

logback-amqp-appender/src/test/java/ch/qos/logback/amqp/tests/AmqpAppenderTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
import java.util.UUID;
66

7-
import ch.qos.logback.amqp.tools.DefaultMutator;
8-
9-
import org.slf4j.MDC;
10-
117
import ch.qos.logback.amqp.AmqpAppender;
8+
import ch.qos.logback.amqp.tools.DefaultMutator;
129
import ch.qos.logback.classic.Logger;
1310
import org.slf4j.LoggerFactory;
11+
import org.slf4j.MDC;
1412

1513
import org.junit.Assert;
1614
import org.junit.Test;

logback-amqp-consumer/src/main/java/ch/qos/logback/amqp/AmqpConsumer.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public AmqpConsumer (
2424
super (host, port, virtualHost, username, password, callbacks);
2525
this.exchanges = exchanges;
2626
this.queue = queue;
27+
this.queue1 = null;
2728
this.sink = sink;
2829
}
2930

@@ -34,9 +35,7 @@ protected final void loop ()
3435
synchronized (this) {
3536
if (this.shouldStopLoop ())
3637
break loop;
37-
if (!this.shouldReconnect ())
38-
break;
39-
if (this.connect ())
38+
if (this.reconnect ())
4039
break;
4140
}
4241
this.sleep ();
@@ -111,27 +110,38 @@ private final boolean declare ()
111110
}
112111
}
113112
{
114-
this.callbacks.handleLogEvent (Level.INFO, null, "amqp consumer declaring the queue `%s`", this.queue);
113+
final String queue;
114+
final boolean unique;
115+
if ((this.queue == null) || this.queue.isEmpty ()) {
116+
if (this.queue1 != null)
117+
queue = this.queue1;
118+
else
119+
queue = "";
120+
unique = true;
121+
} else {
122+
queue = this.queue;
123+
unique = false;
124+
}
125+
this.callbacks.handleLogEvent (Level.INFO, null, "amqp consumer declaring the queue `%s`", queue);
115126
try {
116-
channel.queueDeclare (this.queue, true, false, false, null);
127+
this.queue1 = channel.queueDeclare (queue, true, unique, unique, null).getQueue ();
117128
} catch (final Throwable exception) {
118-
this.callbacks
119-
.handleException (
120-
exception, "amqp consumer encountered an error while declaring the queue `%s`; aborting!",
121-
this.queue);
129+
this.queue1 = null;
130+
this.callbacks.handleException (
131+
exception, "amqp consumer encountered an error while declaring the queue `%s`; aborting!", queue);
122132
return (false);
123133
}
124134
}
125135
for (final String exchange : this.exchanges) {
126136
this.callbacks.handleLogEvent (
127-
Level.INFO, null, "amqp consumer binding the queue `%s` to exchange `%s`", this.queue, exchange);
137+
Level.INFO, null, "amqp consumer binding the queue `%s` to exchange `%s`", this.queue1, exchange);
128138
try {
129-
channel.queueBind (this.queue, exchange, "#", null);
139+
channel.queueBind (this.queue1, exchange, "#", null);
130140
} catch (final Throwable exception) {
131141
this.callbacks.handleException (
132142
exception,
133143
"amqp consumer encountered an error while binding the queue `%s` to exchange `%s`; aborting!",
134-
this.queue, exchange);
144+
this.queue1, exchange);
135145
return (false);
136146
}
137147
}
@@ -143,7 +153,7 @@ private final boolean register ()
143153
this.callbacks.handleLogEvent (Level.INFO, null, "amqp consumer registering the consumer");
144154
final Channel channel = this.getChannel ();
145155
try {
146-
channel.basicConsume (this.queue, true, this.queue, true, true, null, new ConsumerCallback ());
156+
channel.basicConsume (this.queue1, true, this.queue1, true, true, null, new ConsumerCallback ());
147157
return (true);
148158
} catch (final Throwable exception) {
149159
this.callbacks.handleException (
@@ -154,6 +164,7 @@ private final boolean register ()
154164

155165
private final String[] exchanges;
156166
private final String queue;
167+
private String queue1;
157168
private final LinkedBlockingQueue<AmqpMessage> sink;
158169

159170
private final class ConsumerCallback

logback-amqp-consumer/src/main/java/ch/qos/logback/amqp/AmqpConsumerAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,6 @@ private final void loop ()
234234
private String virtualHost;
235235

236236
public static final String defaultExchange = "logback";
237-
public static final String defaultQueue = "logback.agent";
237+
public static final String defaultQueue = "";
238238
public static final int waitTimeout = 1000;
239239
}

logback-amqp-consumer/src/test/config/logback-consumer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<username>guest</username>
1212
<password>guest</password>
1313
<exchange>logback</exchange>
14-
<queue>logback.agent</queue>
14+
<queue></queue>
1515
</amqpConsumerAgent>
1616

1717
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">

0 commit comments

Comments
 (0)