|
15 | 15 |
|
16 | 16 | package com.rabbitmq.client.impl;
|
17 | 17 |
|
| 18 | +import com.rabbitmq.client.*; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
18 | 21 | import java.io.IOException;
|
19 | 22 | import java.net.ConnectException;
|
20 |
| -import java.util.concurrent.TimeoutException; |
21 |
| - |
22 |
| -import com.rabbitmq.client.AMQP; |
23 |
| -import com.rabbitmq.client.AlreadyClosedException; |
24 |
| -import com.rabbitmq.client.Channel; |
25 |
| -import com.rabbitmq.client.Connection; |
26 |
| -import com.rabbitmq.client.Consumer; |
27 |
| -import com.rabbitmq.client.ExceptionHandler; |
28 |
| -import com.rabbitmq.client.TopologyRecoveryException; |
29 | 23 |
|
30 | 24 | /**
|
31 | 25 | * An implementation of {@link com.rabbitmq.client.ExceptionHandler} that does not
|
|
38 | 32 | */
|
39 | 33 | public class ForgivingExceptionHandler implements ExceptionHandler {
|
40 | 34 | public void handleUnexpectedConnectionDriverException(Connection conn, Throwable exception) {
|
41 |
| - // TODO: Log this somewhere, just in case we have a bug like |
42 |
| - // 16272 where exceptions aren't being propagated properly |
43 |
| - // again. |
44 |
| - |
45 |
| - //System.err.println("DefaultExceptionHandler:"); |
46 |
| - //exception.printStackTrace(); |
| 35 | + log("An unexpected connection driver error occured", exception); |
47 | 36 | }
|
48 | 37 |
|
49 | 38 | public void handleReturnListenerException(Channel channel, Throwable exception) {
|
@@ -82,48 +71,43 @@ public void handleConnectionRecoveryException(Connection conn, Throwable excepti
|
82 | 71 | if (exception instanceof ConnectException) {
|
83 | 72 | // no-op
|
84 | 73 | } else {
|
85 |
| - System.err.println("Caught an exception during connection recovery!"); |
86 |
| - exception.printStackTrace(System.err); |
| 74 | + log("Caught an exception during connection recovery!", exception); |
87 | 75 | }
|
88 | 76 | }
|
89 | 77 |
|
90 | 78 | /**
|
91 | 79 | * @since 3.3.0
|
92 | 80 | */
|
93 | 81 | public void handleChannelRecoveryException(Channel ch, Throwable exception) {
|
94 |
| - System.err.println("Caught an exception when recovering channel " + ch.getChannelNumber()); |
95 |
| - exception.printStackTrace(System.err); |
| 82 | + log("Caught an exception when recovering channel " + ch.getChannelNumber(), exception); |
96 | 83 | }
|
97 | 84 |
|
98 | 85 | /**
|
99 | 86 | * @since 3.3.0
|
100 | 87 | */
|
101 | 88 | public void handleTopologyRecoveryException(Connection conn, Channel ch, TopologyRecoveryException exception) {
|
102 |
| - System.err.println("Caught an exception when recovering topology " + exception.getMessage()); |
103 |
| - exception.printStackTrace(System.err); |
| 89 | + log("Caught an exception when recovering topology " + exception.getMessage(), exception); |
104 | 90 | }
|
105 | 91 |
|
106 | 92 | protected void handleChannelKiller(Channel channel, Throwable exception, String what) {
|
107 |
| - System.err.println(this.getClass().getName() + ": " + what + " threw an exception for channel " |
108 |
| - + channel + ":"); |
109 |
| - exception.printStackTrace(); |
| 93 | + log(what + "threw an exception for channel "+channel, exception); |
110 | 94 | }
|
111 | 95 |
|
112 | 96 | protected void handleConnectionKiller(Connection connection, Throwable exception, String what) {
|
113 |
| - // TODO: log the exception |
114 |
| - System.err.println("DefaultExceptionHandler: " + what + " threw an exception for connection " |
115 |
| - + connection + ":"); |
116 |
| - exception.printStackTrace(); |
| 97 | + log(what + " threw an exception for connection " + connection, exception); |
117 | 98 | try {
|
118 | 99 | connection.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + what);
|
119 | 100 | } catch (AlreadyClosedException ace) {
|
120 | 101 | // noop
|
121 | 102 | } catch (IOException ioe) {
|
122 |
| - // TODO: log the failure |
123 |
| - System.err.println("Failure during close of connection " + connection + " after " + exception |
124 |
| - + ":"); |
125 |
| - ioe.printStackTrace(); |
| 103 | + log("Failure during close of connection " + connection + " after " + exception, ioe); |
126 | 104 | connection.abort(AMQP.INTERNAL_ERROR, "Internal error closing connection for " + what);
|
127 | 105 | }
|
128 | 106 | }
|
| 107 | + |
| 108 | + protected void log(String message, Throwable e) { |
| 109 | + LoggerFactory.getLogger(ForgivingExceptionHandler.class).error( |
| 110 | + message, e |
| 111 | + ); |
| 112 | + } |
129 | 113 | }
|
0 commit comments