1
1
/**
2
- * Copyright 2017 IBM Corporation
2
+ * Copyright 2017, 2018 IBM Corporation
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -139,8 +139,24 @@ public void configure(Map<String, String> props) {
139
139
* @throws ConnectException Operation failed and connector should stop.
140
140
*/
141
141
public void connect () throws ConnectException , RetriableException {
142
- connectInternal ();
143
- log .info ("Connection to MQ established" );
142
+ try {
143
+ if (userName != null ) {
144
+ jmsCtxt = mqConnFactory .createContext (userName , password , JMSContext .SESSION_TRANSACTED );
145
+ }
146
+ else {
147
+ jmsCtxt = mqConnFactory .createContext (JMSContext .SESSION_TRANSACTED );
148
+ }
149
+
150
+ jmsCons = jmsCtxt .createConsumer (queue );
151
+ connected = true ;
152
+
153
+ log .info ("Connection to MQ established" );
154
+ }
155
+ catch (JMSRuntimeException jmse ) {
156
+ log .info ("Connection to MQ could not be established" );
157
+ log .debug ("JMS exception {}" , jmse );
158
+ handleException (jmse );
159
+ }
144
160
}
145
161
146
162
/**
@@ -149,12 +165,11 @@ public void connect() throws ConnectException, RetriableException {
149
165
* @param wait Whether to wait indefinitely for a message
150
166
*
151
167
* @return The SourceRecord representing the message
152
- *
153
- * @throws RetriableException Operation failed, but connector should continue to retry.
154
- * @throws ConnectException Operation failed and connector should stop.
155
168
*/
156
- public SourceRecord receive (boolean wait ) throws ConnectException , RetriableException {
157
- connectInternal ();
169
+ public SourceRecord receive (boolean wait ) {
170
+ if (!connectInternal ()) {
171
+ return null ;
172
+ }
158
173
159
174
Message m = null ;
160
175
SourceRecord sr = null ;
@@ -197,12 +212,12 @@ public SourceRecord receive(boolean wait) throws ConnectException, RetriableExce
197
212
/**
198
213
* Commits the current transaction. If the current transaction contains a message that could not
199
214
* be processed, the transaction is "in peril" and is rolled back instead to avoid data loss.
200
- *
201
- * @throws RetriableException Operation failed, but connector should continue to retry.
202
- * @throws ConnectException Operation failed and connector should stop.
203
215
*/
204
- public void commit () throws ConnectException , RetriableException {
205
- connectInternal ();
216
+ public void commit () {
217
+ if (!connectInternal ()) {
218
+ return ;
219
+ }
220
+
206
221
try {
207
222
if (inflight ) {
208
223
inflight = false ;
@@ -211,7 +226,6 @@ public void commit() throws ConnectException, RetriableException {
211
226
inperil = false ;
212
227
log .trace ("Rolling back in-flight transaction" );
213
228
jmsCtxt .rollback ();
214
- throw new RetriableException ("Transaction rolled back" );
215
229
}
216
230
else {
217
231
jmsCtxt .commit ();
@@ -241,16 +255,15 @@ public void close() {
241
255
/**
242
256
* Internal method to connect to MQ.
243
257
*
244
- * @throws RetriableException Operation failed, but connector should continue to retry.
245
- * @throws ConnectException Operation failed and connector should stop.
258
+ * @return true if connection can be used, false otherwise
246
259
*/
247
- private void connectInternal () throws ConnectException , RetriableException {
260
+ private boolean connectInternal () {
248
261
if (connected ) {
249
- return ;
262
+ return true ;
250
263
}
251
264
252
265
if (closeNow .get ()) {
253
- throw new ConnectException ( "Connection closing" ) ;
266
+ return false ;
254
267
}
255
268
256
269
try {
@@ -263,11 +276,16 @@ private void connectInternal() throws ConnectException, RetriableException {
263
276
264
277
jmsCons = jmsCtxt .createConsumer (queue );
265
278
connected = true ;
279
+
280
+ log .info ("Connection to MQ established" );
266
281
}
267
282
catch (JMSRuntimeException jmse ) {
268
283
log .debug ("JMS exception {}" , jmse );
269
284
handleException (jmse );
285
+ return false ;
270
286
}
287
+
288
+ return true ;
271
289
}
272
290
273
291
/**
@@ -289,14 +307,15 @@ private void closeInternal() {
289
307
finally
290
308
{
291
309
jmsCtxt = null ;
310
+ log .debug ("Connection to MQ closed" );
292
311
}
293
312
}
294
313
295
314
/**
296
315
* Handles exceptions from MQ. Some JMS exceptions are treated as retriable meaning that the
297
316
* connector can keep running and just trying again is likely to fix things.
298
317
*/
299
- private void handleException (Throwable exc ) throws ConnectException , RetriableException {
318
+ private ConnectException handleException (Throwable exc ) {
300
319
boolean isRetriable = false ;
301
320
boolean mustClose = true ;
302
321
int reason = -1 ;
@@ -330,7 +349,7 @@ private void handleException(Throwable exc) throws ConnectException, RetriableEx
330
349
isRetriable = true ;
331
350
break ;
332
351
333
- // These reason codes indicates that the connect is still OK, but just retrying later
352
+ // These reason codes indicate that the connect is still OK, but just retrying later
334
353
// will probably recover - possibly with administrative action on the queue manager
335
354
case MQConstants .MQRC_GET_INHIBITED :
336
355
isRetriable = true ;
@@ -343,8 +362,9 @@ private void handleException(Throwable exc) throws ConnectException, RetriableEx
343
362
}
344
363
345
364
if (isRetriable ) {
346
- throw new RetriableException (exc );
365
+ return new RetriableException (exc );
347
366
}
348
- throw new ConnectException (exc );
367
+
368
+ return new ConnectException (exc );
349
369
}
350
370
}
0 commit comments