Skip to content

Commit a178001

Browse files
Added diagnostic messages. Fixed some bugs.
1 parent dbec787 commit a178001

File tree

11 files changed

+191
-100
lines changed

11 files changed

+191
-100
lines changed

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
package ch.qos.logback.amqp;
33

44

5-
import ch.qos.logback.amqp.tools.ExceptionHandler;
5+
import ch.qos.logback.amqp.tools.Callbacks;
6+
import ch.qos.logback.classic.Level;
67
import com.rabbitmq.client.Channel;
78
import com.rabbitmq.client.Connection;
89
import com.rabbitmq.client.ConnectionFactory;
@@ -14,15 +15,15 @@ public abstract class AmqpAccessor
1415
{
1516
protected AmqpAccessor (
1617
final String host, final Integer port, final String virtualHost, final String username, final String password,
17-
final ExceptionHandler exceptionHandler)
18+
final Callbacks callbacks)
1819
{
1920
super ();
2021
this.host = ((host != null) && !host.isEmpty ()) ? host : "127.0.0.1";
2122
this.port = ((port != null) && (port != 0)) ? port : 5672;
2223
this.virtualHost = ((virtualHost != null) && !virtualHost.isEmpty ()) ? virtualHost : "/";
2324
this.username = ((username != null) && !username.isEmpty ()) ? username : "guest";
2425
this.password = ((password != null) && !password.isEmpty ()) ? password : "guest";
25-
this.exceptionHandler = exceptionHandler;
26+
this.callbacks = callbacks;
2627
this.thread = null;
2728
this.shutdownHook = null;
2829
this.shouldStopLoop = true;
@@ -38,7 +39,7 @@ public final boolean isConnected ()
3839
}
3940
}
4041

41-
public final boolean isStarted ()
42+
public final boolean isRunning ()
4243
{
4344
synchronized (this) {
4445
return ((this.thread != null) && (this.thread.isAlive ()));
@@ -50,12 +51,15 @@ public final void start ()
5051
synchronized (this) {
5152
if (this.thread != null)
5253
throw (new IllegalStateException ("amqp accessor is already started"));
54+
this.callbacks.handleLogEvent (Level.INFO, null, "amqp accessor starting");
5355
this.thread = new Thread (new Runnable () {
5456
public final void run ()
5557
{
58+
AmqpAccessor.this.callbacks.handleLogEvent (Level.INFO, null, "amqp accessor started");
5659
AmqpAccessor.this.loop ();
5760
if (AmqpAccessor.this.shutdownHook != null)
5861
Runtime.getRuntime ().removeShutdownHook (AmqpAccessor.this.shutdownHook);
62+
AmqpAccessor.this.callbacks.handleLogEvent (Level.INFO, null, "amqp accessor stopped");
5963
}
6064
});
6165
this.thread.setName (String.format ("%s@%x", this.getClass ().getName (), System.identityHashCode (this)));
@@ -64,9 +68,10 @@ public final void run ()
6468
public final void run ()
6569
{
6670
AmqpAccessor.this.shutdownHook = null;
67-
if (AmqpAccessor.this.isStarted ()) {
68-
AmqpAccessor.this.stop ();
69-
while (AmqpAccessor.this.isStarted ())
71+
if (AmqpAccessor.this.isRunning ()) {
72+
if (!AmqpAccessor.this.shouldStopLoop)
73+
AmqpAccessor.this.stop ();
74+
while (AmqpAccessor.this.isRunning ())
7075
try {
7176
Thread.sleep (AmqpAccessor.waitTimeout);
7277
} catch (final InterruptedException exception) {
@@ -86,6 +91,7 @@ public final void stop ()
8691
synchronized (this) {
8792
if (this.thread == null)
8893
throw (new IllegalStateException ("amqp accessor is not started"));
94+
this.callbacks.handleLogEvent (Level.INFO, null, "amqp accessor stopping");
8995
this.shouldStopLoop = true;
9096
}
9197
}
@@ -95,6 +101,9 @@ protected final boolean connect ()
95101
synchronized (this) {
96102
if (this.connection != null)
97103
throw (new IllegalStateException ("amqp accessor is already connected"));
104+
this.callbacks.handleLogEvent (
105+
Level.INFO, null, "amqp accessor connecting to `%s@%s:%s:%s`", this.username, this.host, this.port,
106+
this.virtualHost);
98107
this.shouldReconnect = true;
99108
final ConnectionFactory connectionFactory = new ConnectionFactory ();
100109
connectionFactory.setHost (this.host);
@@ -106,16 +115,15 @@ protected final boolean connect ()
106115
this.connection = connectionFactory.newConnection ();
107116
} catch (final Throwable exception) {
108117
this.connection = null;
109-
this.exceptionHandler.handleException (
110-
"amqp accessor encountered an error while connecting; aborting!", exception);
118+
this.callbacks.handleException (exception, "amqp accessor encountered an error while connecting; aborting!");
111119
}
112120
if (this.connection != null)
113121
try {
114122
this.channel = this.connection.createChannel ();
115123
} catch (final Throwable exception) {
116124
this.channel = null;
117-
this.exceptionHandler.handleException (
118-
"amqp accessor encountered an error while opening a channel; aborting!", exception);
125+
this.callbacks.handleException (
126+
exception, "amqp accessor encountered an error while opening the channel; aborting!");
119127
}
120128
if ((this.connection == null) || (this.channel == null)) {
121129
if (this.connection != null)
@@ -126,12 +134,15 @@ protected final boolean connect ()
126134
public void shutdownCompleted (final ShutdownSignalException exception)
127135
{
128136
AmqpAccessor.this.shouldReconnect = true;
129-
if (!exception.isInitiatedByApplication ())
130-
AmqpAccessor.this.exceptionHandler.handleException (
131-
"amqp consumer encountered an shutdown error; ignoring!", exception);
137+
if (!exception.isInitiatedByApplication ()) {
138+
AmqpAccessor.this.callbacks.handleException (
139+
exception, "amqp consumer encountered an shutdown error; ignoring!");
140+
AmqpAccessor.this.disconnect ();
141+
}
132142
}
133143
});
134144
this.shouldReconnect = false;
145+
this.callbacks.handleLogEvent (Level.INFO, null, "amqp accessor connected");
135146
}
136147
return (this.connection != null);
137148
}
@@ -141,6 +152,7 @@ protected final void disconnect ()
141152
{
142153
if (this.connection == null)
143154
throw (new IllegalStateException ("amqp accessor is not connected"));
155+
this.callbacks.handleLogEvent (Level.INFO, null, "amqp accessor disconnecting");
144156
this.shouldReconnect = true;
145157
try {
146158
try {
@@ -150,8 +162,7 @@ protected final void disconnect ()
150162
this.connection.close ();
151163
}
152164
} catch (final Throwable exception) {
153-
this.exceptionHandler.handleException (
154-
"amqp accessor encountered an error while disconnecting; ignoring!", exception);
165+
this.callbacks.handleException (exception, "amqp accessor encountered an error while disconnecting; ignoring!");
155166
} finally {
156167
this.connection = null;
157168
this.channel = null;
@@ -191,7 +202,7 @@ protected final void sleep ()
191202
} catch (final InterruptedException exception) {}
192203
}
193204

194-
protected final ExceptionHandler exceptionHandler;
205+
protected final Callbacks callbacks;
195206
protected final String host;
196207
protected final String password;
197208
protected final int port;

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.concurrent.LinkedBlockingDeque;
77

88
import ch.qos.logback.amqp.tools.DefaultBinarySerializer;
9-
import ch.qos.logback.amqp.tools.ExceptionHandler;
9+
import ch.qos.logback.amqp.tools.DefaultContextAwareCallbacks;
1010
import ch.qos.logback.amqp.tools.Serializer;
1111
import ch.qos.logback.classic.PatternLayout;
1212
import ch.qos.logback.classic.net.LoggingEventPreSerializationTransformer;
@@ -18,8 +18,6 @@
1818

1919
public final class AmqpAppender
2020
extends UnsynchronizedAppenderBase<ILoggingEvent>
21-
implements
22-
ExceptionHandler
2321
{
2422
public AmqpAppender ()
2523
{
@@ -34,11 +32,6 @@ public AmqpAppender ()
3432
this.publisher = null;
3533
}
3634

37-
public final void handleException (final String message, final Throwable exception)
38-
{
39-
this.addError (message, exception);
40-
}
41-
4235
public final boolean isDrained ()
4336
{
4437
return (this.buffer.isEmpty ());
@@ -47,7 +40,7 @@ public final boolean isDrained ()
4740
public final boolean isRunning ()
4841
{
4942
final AmqpPublisher publisher = this.publisher;
50-
return (((publisher != null) && publisher.isStarted ()) || super.isStarted ());
43+
return (((publisher != null) && publisher.isRunning ()) || super.isStarted ());
5144
}
5245

5346
public final void setContext (final Context context)
@@ -59,6 +52,13 @@ public final void setContext (final Context context)
5952
this.routingKeyLayout.setContext (context);
6053
}
6154

55+
public final void setExchangePattern (final String pattern)
56+
{
57+
if (this.isStarted ())
58+
throw (new IllegalStateException ("amqp appender is already started"));
59+
this.exchangeLayout.setPattern (pattern);
60+
}
61+
6262
public final void setHost (final String host)
6363
{
6464
if (this.isStarted ())
@@ -87,13 +87,6 @@ public final void setRoutingKeyPattern (final String pattern)
8787
this.routingKeyLayout.setPattern (pattern);
8888
}
8989

90-
public final void setExchangePattern (final String pattern)
91-
{
92-
if (this.isStarted ())
93-
throw (new IllegalStateException ("amqp appender is already started"));
94-
this.exchangeLayout.setPattern (pattern);
95-
}
96-
9790
public final void setUsername (final String username)
9891
{
9992
if (this.isStarted ())
@@ -117,7 +110,9 @@ public final void start ()
117110
this.exchangeLayout.start ();
118111
this.routingKeyLayout.start ();
119112
this.publisher =
120-
new AmqpPublisher (this.host, this.port, this.virtualHost, this.username, this.password, this, this.buffer);
113+
new AmqpPublisher (
114+
this.host, this.port, this.virtualHost, this.username, this.password,
115+
new DefaultContextAwareCallbacks (this), this.buffer);
121116
this.publisher.start ();
122117
super.start ();
123118
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import java.util.concurrent.LinkedBlockingDeque;
66
import java.util.concurrent.TimeUnit;
77

8-
import ch.qos.logback.amqp.tools.ExceptionHandler;
8+
import ch.qos.logback.amqp.tools.Callbacks;
9+
import ch.qos.logback.classic.Level;
910
import com.rabbitmq.client.AMQP;
1011
import com.rabbitmq.client.Channel;
1112

@@ -15,9 +16,9 @@ public final class AmqpPublisher
1516
{
1617
public AmqpPublisher (
1718
final String host, final Integer port, final String virtualHost, final String username, final String password,
18-
final ExceptionHandler exceptionHandler, final LinkedBlockingDeque<AmqpMessage> source)
19+
final Callbacks callbacks, final LinkedBlockingDeque<AmqpMessage> source)
1920
{
20-
super (host, port, virtualHost, username, password, exceptionHandler);
21+
super (host, port, virtualHost, username, password, callbacks);
2122
this.source = source;
2223
}
2324

@@ -35,6 +36,7 @@ protected final void loop ()
3536
}
3637
this.sleep ();
3738
}
39+
this.callbacks.handleLogEvent (Level.INFO, null, "amqp publisher showeling outbound messages");
3840
while (true) {
3941
synchronized (this) {
4042
if (this.shouldStopLoop ())
@@ -71,8 +73,8 @@ private final boolean publish (final AmqpMessage message)
7173
channel.basicPublish (message.exchange, message.routingKey, false, false, properties, message.content);
7274
return (true);
7375
} catch (final Throwable exception) {
74-
this.exceptionHandler.handleException (
75-
"amqp publisher encountered an error while publishing the message; requeueing!", exception);
76+
this.callbacks.handleException (
77+
exception, "amqp publisher encountered an error while publishing the message; requeueing!");
7678
return (false);
7779
}
7880
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
package ch.qos.logback.amqp.tools;
3+
4+
5+
import ch.qos.logback.classic.Level;
6+
7+
8+
public interface Callbacks
9+
{
10+
public abstract void handleException (
11+
final Throwable exception, final String messageFormat, final Object ... messageArguments);
12+
13+
public abstract void handleLogEvent (
14+
final Level level, final Throwable exception, final String messageFormat, final Object ... messageArguments);
15+
}

logback-amqp-appender/src/main/java/ch/qos/logback/amqp/tools/DefaultBinarySerializer.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ public final Serializable deserialize (final byte[] data)
2222
return (object);
2323
}
2424

25-
public final byte[] serialize (final Serializable object)
26-
throws Throwable
25+
public final String getContentEncoding ()
2726
{
28-
final ByteArrayOutputStream stream = new ByteArrayOutputStream (this.defaultBufferSize);
29-
final ObjectOutputStream encoder = new ObjectOutputStream (stream);
30-
encoder.writeObject (object);
31-
encoder.close ();
32-
return (stream.toByteArray ());
27+
return (this.contentEncoding);
3328
}
3429

3530
public final String getContentType ()
3631
{
3732
return (this.contentType);
3833
}
3934

40-
public final String getContentEncoding ()
35+
public final byte[] serialize (final Serializable object)
36+
throws Throwable
4137
{
42-
return (this.contentEncoding);
38+
final ByteArrayOutputStream stream = new ByteArrayOutputStream (this.defaultBufferSize);
39+
final ObjectOutputStream encoder = new ObjectOutputStream (stream);
40+
encoder.writeObject (object);
41+
encoder.close ();
42+
return (stream.toByteArray ());
4343
}
4444

45-
public final int defaultBufferSize = 2048;
46-
public final String contentType = "application/x-java-serialized-object";
4745
public final String contentEncoding = "binary";
46+
public final String contentType = "application/x-java-serialized-object";
47+
public final int defaultBufferSize = 2048;
4848
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
package ch.qos.logback.amqp.tools;
3+
4+
5+
import ch.qos.logback.classic.Level;
6+
import ch.qos.logback.core.spi.ContextAware;
7+
8+
9+
public final class DefaultContextAwareCallbacks
10+
implements
11+
Callbacks
12+
{
13+
public DefaultContextAwareCallbacks (final ContextAware delegate)
14+
{
15+
super ();
16+
this.delegate = delegate;
17+
}
18+
19+
public final void handleException (
20+
final Throwable exception, final String messageFormat, final Object ... messageArguments)
21+
{
22+
this.handleLogEvent (Level.ERROR, exception, messageFormat, messageArguments);
23+
}
24+
25+
public final void handleLogEvent (
26+
final Level level, final Throwable exception, final String messageFormat, final Object ... messageArguments)
27+
{
28+
final String message = String.format (messageFormat, messageArguments);
29+
switch (level.levelInt) {
30+
case Level.ERROR_INT :
31+
if (exception != null)
32+
this.delegate.addError (message, exception);
33+
else
34+
this.delegate.addError (message);
35+
break;
36+
case Level.WARN_INT :
37+
if (exception != null)
38+
this.delegate.addWarn (message, exception);
39+
else
40+
this.delegate.addWarn (message);
41+
break;
42+
case Level.INFO_INT :
43+
if (exception != null)
44+
this.delegate.addInfo (message, exception);
45+
else
46+
this.delegate.addInfo (message);
47+
break;
48+
default:
49+
if (exception != null)
50+
this.delegate.addInfo (message, exception);
51+
else
52+
this.delegate.addInfo (message);
53+
break;
54+
}
55+
}
56+
57+
private final ContextAware delegate;
58+
}

logback-amqp-appender/src/main/java/ch/qos/logback/amqp/tools/ExceptionHandler.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)