Skip to content

Commit c1d76e8

Browse files
Initialize exception handler in ConnectionFactory
1 parent 80dc1e4 commit c1d76e8

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/com/rabbitmq/client/ConnectionFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class ConnectionFactory implements Cloneable {
9191
private SaslConfig saslConfig = DefaultSaslConfig.PLAIN;
9292
private ExecutorService sharedExecutor;
9393
private SocketConfigurator socketConf = new DefaultSocketConfigurator();
94-
private ExceptionHandler exceptionHandler = null;
94+
private ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
9595

9696
private boolean automaticRecovery = false;
9797
private boolean topologyRecovery = true;
@@ -444,6 +444,9 @@ public ExceptionHandler getExceptionHandler() {
444444
*/
445445

446446
public void setExceptionHandler(ExceptionHandler exceptionHandler) {
447+
if (exceptionHandler == null) {
448+
throw new IllegalArgumentException("exception handler cannot be null!");
449+
}
447450
this.exceptionHandler = exceptionHandler;
448451
}
449452

src/com/rabbitmq/client/impl/AMQConnection.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static final Map<String, Object> defaultClientProperties() {
111111
private volatile boolean _running = false;
112112

113113
/** Handler for (uncaught) exceptions that crop up in the {@link MainLoop}. */
114-
private ExceptionHandler _exceptionHandler = new DefaultExceptionHandler();
114+
private ExceptionHandler _exceptionHandler;
115115

116116
/** Object used for blocking main application thread when doing all the necessary
117117
* connection shutdown operations
@@ -207,15 +207,14 @@ public AMQConnection(ConnectionParams params, FrameHandler frameHandler)
207207
this.password = params.getPassword();
208208
this._frameHandler = frameHandler;
209209
this._virtualHost = params.getVirtualHost();
210-
this._exceptionHandler = params.getExceptionHandler();
211210
this._clientProperties = new HashMap<String, Object>(params.getClientProperties());
212211
this.requestedFrameMax = params.getRequestedFrameMax();
213212
this.requestedChannelMax = params.getRequestedChannelMax();
214213
this.requestedHeartbeat = params.getRequestedHeartbeat();
215214
this.saslConfig = params.getSaslConfig();
216215

217216
this._workService = new ConsumerWorkService(params.getExecutor());
218-
this._exceptionHandler = params.getExceptionHandler();
217+
this._exceptionHandler = initExceptionHandler(params);
219218
this._channelManager = null;
220219

221220
this._heartbeatSender = new HeartbeatSender(frameHandler);
@@ -224,7 +223,18 @@ public AMQConnection(ConnectionParams params, FrameHandler frameHandler)
224223
this._inConnectionNegotiation = true; // we start out waiting for the first protocol response
225224
}
226225

227-
/**
226+
private ExceptionHandler initExceptionHandler(ConnectionParams params) {
227+
ExceptionHandler result = params.getExceptionHandler();
228+
// null exception handler causes I/O thread to fail,
229+
// so be extra defensive here
230+
if(result == null) {
231+
return new DefaultExceptionHandler();
232+
} else {
233+
return result;
234+
}
235+
}
236+
237+
/**
228238
* Start up the connection, including the MainLoop thread.
229239
* Sends the protocol
230240
* version negotiation header, and runs through

test/src/com/rabbitmq/client/test/functional/ExceptionHandling.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
4646
wait(latch);
4747
}
4848

49+
public void testNullExceptionHandler() {
50+
ConnectionFactory cf = new ConnectionFactory();
51+
try {
52+
cf.setExceptionHandler(null);
53+
fail("expected setExceptionHandler to throw");
54+
} catch (IllegalArgumentException iae) {
55+
// expected
56+
}
57+
}
58+
4959
private void wait(CountDownLatch latch) throws InterruptedException {
5060
latch.await(30, TimeUnit.MINUTES);
5161
}

0 commit comments

Comments
 (0)