@@ -126,49 +126,33 @@ private void connectionLoop() {
126126 }
127127 }
128128 } catch (UnprocessableEventException | UnexpectedEventProcessingException e ) {
129- addContextToException (e );
130- LOGGER .warn ("Unable to process MongoDB change event; reconnecting ({})" , e .getMessage (), e );
131- listener .onDisconnect (e );
129+ disconnect ("Unable to process MongoDB change event" , REMEDY_CONTINUE , e );
132130 // Reconnection will skip this event, so it's safe to try it right away
133131 continue ;
134132 } catch (InterruptedException e ) {
135- addContextToException (e );
136- LOGGER .warn ("Interrupted while processing MongoDB change events; reconnecting" , e );
137- listener .onDisconnect (e );
133+ disconnect ("Interrupted while processing MongoDB change events" , REMEDY_CONTINUE , e );
138134 continue ;
139135 } catch (IOException e ) {
140- addContextToException (e );
141- LOGGER .warn ("Unexpected exception while processing MongoDB change events; will wait and retry" , e );
142- listener .onDisconnect (e );
136+ disconnect ("Unexpected exception while processing MongoDB change events" , REMEDY_RETURN , e );
143137 return ;
144138 } catch (UnrecognizedFormatException e ) {
145- addContextToException (e );
146- LOGGER .warn ("Unrecognized MongoDB database content format; will wait and retry" , e );
147- listener .onDisconnect (e );
139+ disconnect ("Unrecognized MongoDB database content format" , REMEDY_RETURN , e );
148140 return ;
149141 } catch (UninitializedCollectionException e ) {
150- addContextToException (e );
151- LOGGER .warn ("MongoDB collection is not initialized; will wait and retry" , e );
152- listener .onDisconnect (e );
142+ disconnect ("MongoDB collection is not initialized" , REMEDY_RETURN , e );
153143 return ;
154144 } catch (InitialRootActionException e ) {
155- addContextToException (e );
156- LOGGER .warn ("Unable to initialize bosk state; will wait and retry" , e );
157- listener .onDisconnect (e );
145+ disconnect ("Unable to initialize bosk state" , REMEDY_RETURN , e );
158146 return ;
159147 } catch (TimeoutException e ) {
160- addContextToException (e );
161- LOGGER .warn ("Timed out waiting for bosk state to initialize; will wait and retry" , e );
162- listener .onDisconnect (e );
148+ disconnect ("Timed out waiting for bosk state to initialize" , REMEDY_RETURN , e );
163149 return ;
164150 } catch (DisconnectedException e ) {
165151 addContextToException (e );
166152 LOGGER .warn ("Driver is disconnected; will wait and retry" , e );
167153 return ;
168154 } catch (RuntimeException | Error e ) {
169- addContextToException (e );
170- LOGGER .warn ("Unexpected exception after connecting to MongoDB; will wait and retry" , e );
171- listener .onDisconnect (e );
155+ disconnect ("Unexpected exception after connecting to MongoDB" , REMEDY_RETURN , e );
172156 return ;
173157 }
174158 } catch (RuntimeException e ) {
@@ -194,6 +178,16 @@ private void connectionLoop() {
194178 }
195179 }
196180
181+ private void disconnect (String description , String remedy , Throwable e ) {
182+ addContextToException (e );
183+ if (isClosed ) {
184+ LOGGER .debug ("(Driver is already closed) {}" , description , e );
185+ } else {
186+ LOGGER .warn ("{}; {}" , description , remedy , e );
187+ }
188+ listener .onDisconnect (e );
189+ }
190+
197191 private void addContextToException (Throwable x ) {
198192 x .addSuppressed (creationPoint );
199193 }
@@ -275,4 +269,14 @@ private void processEvent(ChangeStreamDocument<BsonDocument> event) throws Unpro
275269
276270 private static final AtomicLong EVENT_COUNTER = new AtomicLong (0 );
277271 private static final Logger LOGGER = LoggerFactory .getLogger (ChangeReceiver .class );
272+
273+ /**
274+ * Suitable for {@code continue} in {@link #connectionLoop()}
275+ */
276+ private static final String REMEDY_CONTINUE = "reconnecting" ;
277+
278+ /**
279+ * Suitable for {@code return} in {@link #connectionLoop()}
280+ */
281+ private static final String REMEDY_RETURN = "will wait and retry" ;
278282}
0 commit comments