/
JMSBridgeImpl.java
2031 lines (1611 loc) · 62.9 KB
/
JMSBridgeImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.jms.bridge.impl;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.XAConnection;
import javax.jms.XAConnectionFactory;
import javax.jms.XASession;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.StandardMBean;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.TransactionRolledbackException;
import javax.transaction.xa.XAResource;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ActiveMQInterruptedException;
import org.apache.activemq.artemis.api.core.client.FailoverEventListener;
import org.apache.activemq.artemis.api.core.client.FailoverEventType;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSConstants;
import org.apache.activemq.artemis.core.client.impl.ClientSessionInternal;
import org.apache.activemq.artemis.jms.bridge.ActiveMQJMSBridgeLogger;
import org.apache.activemq.artemis.jms.bridge.ConnectionFactoryFactory;
import org.apache.activemq.artemis.jms.bridge.DestinationFactory;
import org.apache.activemq.artemis.jms.bridge.JMSBridge;
import org.apache.activemq.artemis.jms.bridge.JMSBridgeControl;
import org.apache.activemq.artemis.jms.bridge.QualityOfServiceMode;
import org.apache.activemq.artemis.jms.client.ActiveMQConnection;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQMessage;
import org.apache.activemq.artemis.jms.server.ActiveMQJMSServerBundle;
import org.apache.activemq.artemis.service.extensions.ServiceUtils;
import org.apache.activemq.artemis.service.extensions.xa.recovery.ActiveMQRegistry;
import org.apache.activemq.artemis.service.extensions.xa.recovery.XARecoveryConfig;
import org.apache.activemq.artemis.utils.PasswordMaskingUtil;
public final class JMSBridgeImpl implements JMSBridge {
private static final String[] RESOURCE_RECOVERY_CLASS_NAMES = new String[]{"org.jboss.as.messaging.jms.AS7RecoveryRegistry"};
private static boolean trace = ActiveMQJMSBridgeLogger.LOGGER.isTraceEnabled();
private static final int TEN_YEARS = 60 * 60 * 24 * 365 * 10; // in ms
private static final long DEFAULT_FAILOVER_TIMEOUT = 60 * 1000;
private final Object lock = new Object();
private String bridgeName = "N/A";
private String sourceUsername;
private String sourcePassword;
private String targetUsername;
private String targetPassword;
private TransactionManager tm;
private String selector;
private long failureRetryInterval;
private int maxRetries;
private QualityOfServiceMode qualityOfServiceMode;
private int maxBatchSize;
private long maxBatchTime;
private String subName;
private String clientID;
private volatile boolean addMessageIDInHeader;
private boolean started;
private final Object stoppingGuard = new Object();
private boolean stopping = false;
private final LinkedList<Message> messages;
private ConnectionFactoryFactory sourceCff;
private ConnectionFactoryFactory targetCff;
private DestinationFactory sourceDestinationFactory;
private DestinationFactory targetDestinationFactory;
private Connection sourceConn;
private Connection targetConn;
private Destination sourceDestination;
private Destination targetDestination;
private Session sourceSession;
private Session targetSession;
private MessageConsumer sourceConsumer;
private MessageProducer targetProducer;
private BatchTimeChecker timeChecker;
private ExecutorService executor;
private long batchExpiryTime;
private boolean paused;
private Transaction tx;
private boolean failed;
private boolean connectedSource = false;
private boolean connectedTarget = false;
private int forwardMode;
private MBeanServer mbeanServer;
private ObjectName objectName;
private Boolean useMaskedPassword;
private String passwordCodec;
private long failoverTimeout;
private static final int FORWARD_MODE_XA = 0;
private static final int FORWARD_MODE_LOCALTX = 1;
private static final int FORWARD_MODE_NONTX = 2;
private ActiveMQRegistry registry;
private ClassLoader moduleTccl;
private long messageCount = 0;
private long abortedMessageCount = 0;
/*
* Constructor for MBean
*/
public JMSBridgeImpl() {
messages = new LinkedList<>();
executor = createExecutor();
}
public JMSBridgeImpl(final ConnectionFactoryFactory sourceCff,
final ConnectionFactoryFactory targetCff,
final DestinationFactory sourceDestinationFactory,
final DestinationFactory targetDestinationFactory,
final String sourceUsername,
final String sourcePassword,
final String targetUsername,
final String targetPassword,
final String selector,
final long failureRetryInterval,
final int maxRetries,
final QualityOfServiceMode qosMode,
final int maxBatchSize,
final long maxBatchTime,
final String subName,
final String clientID,
final boolean addMessageIDInHeader) {
this(sourceCff, targetCff, sourceDestinationFactory, targetDestinationFactory, sourceUsername, sourcePassword, targetUsername, targetPassword, selector, failureRetryInterval, maxRetries, qosMode, maxBatchSize, maxBatchTime, subName, clientID, addMessageIDInHeader, null, null);
}
public JMSBridgeImpl(final ConnectionFactoryFactory sourceCff,
final ConnectionFactoryFactory targetCff,
final DestinationFactory sourceDestinationFactory,
final DestinationFactory targetDestinationFactory,
final String sourceUsername,
final String sourcePassword,
final String targetUsername,
final String targetPassword,
final String selector,
final long failureRetryInterval,
final int maxRetries,
final QualityOfServiceMode qosMode,
final int maxBatchSize,
final long maxBatchTime,
final String subName,
final String clientID,
final boolean addMessageIDInHeader,
final MBeanServer mbeanServer,
final String objectName) {
this(sourceCff, targetCff, sourceDestinationFactory, targetDestinationFactory, sourceUsername, sourcePassword, targetUsername, targetPassword, selector, failureRetryInterval, maxRetries, qosMode, maxBatchSize, maxBatchTime, subName, clientID, addMessageIDInHeader, mbeanServer, objectName, DEFAULT_FAILOVER_TIMEOUT);
}
public JMSBridgeImpl(final ConnectionFactoryFactory sourceCff,
final ConnectionFactoryFactory targetCff,
final DestinationFactory sourceDestinationFactory,
final DestinationFactory targetDestinationFactory,
final String sourceUsername,
final String sourcePassword,
final String targetUsername,
final String targetPassword,
final String selector,
final long failureRetryInterval,
final int maxRetries,
final QualityOfServiceMode qosMode,
final int maxBatchSize,
final long maxBatchTime,
final String subName,
final String clientID,
final boolean addMessageIDInHeader,
final MBeanServer mbeanServer,
final String objectName,
final long failoverTimeout) {
this();
this.sourceCff = sourceCff;
this.targetCff = targetCff;
this.sourceDestinationFactory = sourceDestinationFactory;
this.targetDestinationFactory = targetDestinationFactory;
this.sourceUsername = sourceUsername;
this.sourcePassword = sourcePassword;
this.targetUsername = targetUsername;
this.targetPassword = targetPassword;
this.selector = selector;
this.failureRetryInterval = failureRetryInterval;
this.maxRetries = maxRetries;
qualityOfServiceMode = qosMode;
this.maxBatchSize = maxBatchSize;
this.maxBatchTime = maxBatchTime;
this.subName = subName;
this.clientID = clientID;
this.addMessageIDInHeader = addMessageIDInHeader;
this.failoverTimeout = failoverTimeout;
checkParams();
if (mbeanServer != null) {
if (objectName != null) {
this.mbeanServer = mbeanServer;
try {
JMSBridgeControlImpl controlBean = new JMSBridgeControlImpl(this);
this.objectName = ObjectName.getInstance(objectName);
StandardMBean mbean = new StandardMBean(controlBean, JMSBridgeControl.class);
mbeanServer.registerMBean(mbean, this.objectName);
ActiveMQJMSBridgeLogger.LOGGER.debug("Registered JMSBridge instance as: " + this.objectName.getCanonicalName());
} catch (Exception e) {
throw new IllegalStateException("Failed to register JMSBridge MBean", e);
}
} else {
throw new IllegalArgumentException("objectName is required when specifying an MBeanServer");
}
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Created " + this);
}
}
// ActiveMQComponent overrides --------------------------------------------------
@Override
public JMSBridgeImpl setBridgeName(String name) {
this.bridgeName = name;
return this;
}
@Override
public String getBridgeName() {
return bridgeName;
}
@Override
public synchronized void start() throws Exception {
synchronized (stoppingGuard) {
stopping = false;
}
moduleTccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
});
locateRecoveryRegistry();
if (started) {
ActiveMQJMSBridgeLogger.LOGGER.errorBridgeAlreadyStarted(bridgeName);
return;
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Starting " + this);
}
// bridge has been stopped and is restarted
if (executor.isShutdown()) {
executor = createExecutor();
}
initPasswords();
checkParams();
// There may already be a JTA transaction associated to the thread
boolean ok;
// Check to see if the QoSMode requires a TM
if (qualityOfServiceMode.equals(QualityOfServiceMode.ONCE_AND_ONLY_ONCE) && sourceCff != targetCff) {
if (tm == null) {
tm = ServiceUtils.getTransactionManager();
}
if (tm == null) {
ActiveMQJMSBridgeLogger.LOGGER.jmsBridgeTransactionManagerMissing(qualityOfServiceMode, bridgeName);
throw new RuntimeException();
}
// There may already be a JTA transaction associated to the thread
Transaction toResume = null;
try {
toResume = tm.suspend();
ok = setupJMSObjects();
} finally {
if (toResume != null) {
tm.resume(toResume);
}
}
} else {
ok = setupJMSObjects();
}
if (ok) {
connectedSource = true;
connectedTarget = true;
startSource();
} else {
ActiveMQJMSBridgeLogger.LOGGER.errorStartingBridge(bridgeName);
handleFailureOnStartup();
}
}
private void startSource() throws JMSException {
// start the source connection
sourceConn.start();
started = true;
if (maxBatchTime != -1) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Starting time checker thread");
}
timeChecker = new BatchTimeChecker();
executor.execute(timeChecker);
batchExpiryTime = System.currentTimeMillis() + maxBatchTime;
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Started time checker thread");
}
}
executor.execute(new SourceReceiver());
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Started " + this);
}
}
private void initPasswords() throws ActiveMQException {
try {
if (this.sourcePassword != null) {
sourcePassword = PasswordMaskingUtil.resolveMask(useMaskedPassword, sourcePassword, passwordCodec);
}
if (this.targetPassword != null) {
targetPassword = PasswordMaskingUtil.resolveMask(useMaskedPassword, targetPassword, passwordCodec);
}
} catch (Exception e) {
throw ActiveMQJMSServerBundle.BUNDLE.errorDecodingPassword(e);
}
}
@Override
public void stop() throws Exception {
synchronized (stoppingGuard) {
if (stopping)
return;
stopping = true;
}
synchronized (this) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Stopping " + this);
}
if (!connectedSource && sourceConn != null) {
sourceConn.close();
}
if (!connectedTarget && targetConn != null) {
targetConn.close();
}
synchronized (lock) {
started = false;
executor.shutdownNow();
}
boolean ok = executor.awaitTermination(60, TimeUnit.SECONDS);
if (!ok) {
throw new Exception("fail to stop JMS Bridge");
}
if (tx != null) {
// Terminate any transaction
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Rolling back remaining tx");
}
stopSessionFailover();
try {
tx.rollback();
abortedMessageCount += messages.size();
} catch (Exception ignore) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Failed to rollback", ignore);
}
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Rolled back remaining tx");
}
}
try {
sourceConn.close();
} catch (Exception ignore) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Failed to close source conn", ignore);
}
}
if (targetConn != null) {
try {
targetConn.close();
} catch (Exception ignore) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Failed to close target conn", ignore);
}
}
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Stopped " + this);
}
}
}
private void stopSessionFailover() {
XASession xaSource = (XASession) sourceSession;
XASession xaTarget = (XASession) targetSession;
((ClientSessionInternal) xaSource.getXAResource()).getSessionContext().releaseCommunications();
((ClientSessionInternal) xaTarget.getXAResource()).getSessionContext().releaseCommunications();
}
@Override
public synchronized boolean isStarted() {
return started;
}
public void destroy() {
if (mbeanServer != null && objectName != null) {
try {
mbeanServer.unregisterMBean(objectName);
} catch (Exception e) {
ActiveMQJMSBridgeLogger.LOGGER.errorUnregisteringBridge(objectName, bridgeName);
}
}
}
// JMSBridge implementation ------------------------------------------------------------
@Override
public synchronized void pause() throws Exception {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Pausing " + this);
}
synchronized (lock) {
paused = true;
sourceConn.stop();
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Paused " + this);
}
}
@Override
public synchronized void resume() throws Exception {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Resuming " + this);
}
synchronized (lock) {
paused = false;
sourceConn.start();
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Resumed " + this);
}
}
@Override
public DestinationFactory getSourceDestinationFactory() {
return sourceDestinationFactory;
}
@Override
public void setSourceDestinationFactory(final DestinationFactory dest) {
checkBridgeNotStarted();
JMSBridgeImpl.checkNotNull(dest, "TargetDestinationFactory");
sourceDestinationFactory = dest;
}
@Override
public DestinationFactory getTargetDestinationFactory() {
return targetDestinationFactory;
}
@Override
public void setTargetDestinationFactory(final DestinationFactory dest) {
checkBridgeNotStarted();
JMSBridgeImpl.checkNotNull(dest, "TargetDestinationFactory");
targetDestinationFactory = dest;
}
@Override
public synchronized String getSourceUsername() {
return sourceUsername;
}
@Override
public synchronized void setSourceUsername(final String name) {
checkBridgeNotStarted();
sourceUsername = name;
}
@Override
public synchronized String getSourcePassword() {
return sourcePassword;
}
@Override
public synchronized void setSourcePassword(final String pwd) {
checkBridgeNotStarted();
sourcePassword = pwd;
}
@Override
public synchronized String getTargetUsername() {
return targetUsername;
}
@Override
public synchronized void setTargetUsername(final String name) {
checkBridgeNotStarted();
targetUsername = name;
}
@Override
public synchronized String getTargetPassword() {
return targetPassword;
}
@Override
public synchronized void setTargetPassword(final String pwd) {
checkBridgeNotStarted();
targetPassword = pwd;
}
@Override
public synchronized String getSelector() {
return selector;
}
@Override
public synchronized void setSelector(final String selector) {
checkBridgeNotStarted();
this.selector = selector;
}
@Override
public synchronized long getFailureRetryInterval() {
return failureRetryInterval;
}
@Override
public synchronized void setFailureRetryInterval(final long interval) {
checkBridgeNotStarted();
if (interval < 1) {
throw new IllegalArgumentException("FailureRetryInterval must be >= 1");
}
failureRetryInterval = interval;
}
@Override
public synchronized int getMaxRetries() {
return maxRetries;
}
@Override
public synchronized void setMaxRetries(final int retries) {
checkBridgeNotStarted();
JMSBridgeImpl.checkValidValue(retries, "MaxRetries");
maxRetries = retries;
}
@Override
public synchronized QualityOfServiceMode getQualityOfServiceMode() {
return qualityOfServiceMode;
}
@Override
public synchronized void setQualityOfServiceMode(final QualityOfServiceMode mode) {
checkBridgeNotStarted();
JMSBridgeImpl.checkNotNull(mode, "QualityOfServiceMode");
qualityOfServiceMode = mode;
}
@Override
public synchronized int getMaxBatchSize() {
return maxBatchSize;
}
@Override
public synchronized void setMaxBatchSize(final int size) {
checkBridgeNotStarted();
JMSBridgeImpl.checkMaxBatchSize(size);
maxBatchSize = size;
}
@Override
public synchronized long getMaxBatchTime() {
return maxBatchTime;
}
@Override
public synchronized void setMaxBatchTime(final long time) {
checkBridgeNotStarted();
JMSBridgeImpl.checkValidValue(time, "MaxBatchTime");
maxBatchTime = time;
}
@Override
public synchronized String getSubscriptionName() {
return subName;
}
@Override
public synchronized void setSubscriptionName(final String subname) {
checkBridgeNotStarted();
subName = subname;
}
@Override
public synchronized String getClientID() {
return clientID;
}
@Override
public synchronized void setClientID(final String clientID) {
checkBridgeNotStarted();
this.clientID = clientID;
}
@Override
public boolean isAddMessageIDInHeader() {
return addMessageIDInHeader;
}
@Override
public void setAddMessageIDInHeader(final boolean value) {
addMessageIDInHeader = value;
}
@Override
public synchronized boolean isPaused() {
return paused;
}
@Override
public synchronized boolean isFailed() {
return failed;
}
@Override
public synchronized long getMessageCount() {
return messageCount;
}
@Override
public synchronized long getAbortedMessageCount() {
return abortedMessageCount;
}
@Override
public synchronized void setSourceConnectionFactoryFactory(final ConnectionFactoryFactory cff) {
checkBridgeNotStarted();
JMSBridgeImpl.checkNotNull(cff, "SourceConnectionFactoryFactory");
sourceCff = cff;
}
@Override
public synchronized void setTargetConnectionFactoryFactory(final ConnectionFactoryFactory cff) {
checkBridgeNotStarted();
JMSBridgeImpl.checkNotNull(cff, "TargetConnectionFactoryFactory");
targetCff = cff;
}
@Override
public void setTransactionManager(final TransactionManager tm) {
this.tm = tm;
}
// Public ---------------------------------------------------------------------------
// Private -------------------------------------------------------------------
private synchronized void checkParams() {
checkNotNull(sourceCff, "sourceCff");
checkNotNull(targetCff, "targetCff");
checkNotNull(sourceDestinationFactory, "sourceDestinationFactory");
checkNotNull(targetDestinationFactory, "targetDestinationFactory");
checkValidValue(failureRetryInterval, "failureRetryInterval");
checkValidValue(maxRetries, "maxRetries");
if (failureRetryInterval == -1 && maxRetries > 0) {
throw new IllegalArgumentException("If failureRetryInterval == -1 maxRetries must be set to -1");
}
checkMaxBatchSize(maxBatchSize);
checkValidValue(maxBatchTime, "maxBatchTime");
checkNotNull(qualityOfServiceMode, "qualityOfServiceMode");
}
/**
* Check the object is not null
*
* @throws IllegalArgumentException if the object is null
*/
private static void checkNotNull(final Object obj, final String name) {
if (obj == null) {
throw new IllegalArgumentException(name + " cannot be null");
}
}
/**
* Check the bridge is not started
*
* @throws IllegalStateException if the bridge is started
*/
private void checkBridgeNotStarted() {
if (started) {
throw new IllegalStateException("Cannot set bridge attributes while it is started");
}
}
/**
* Check that value is either equals to -1 or greater than 0
*
* @throws IllegalArgumentException if the value is not valid
*/
private static void checkValidValue(final long value, final String name) {
if (!(value == -1 || value > 0)) {
throw new IllegalArgumentException(name + " must be > 0 or -1");
}
}
private static void checkMaxBatchSize(final int size) {
if (!(size >= 1)) {
throw new IllegalArgumentException("maxBatchSize must be >= 1");
}
}
private void enlistResources(final Transaction tx) throws Exception {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Enlisting resources in tx");
}
XAResource resSource = ((XASession) sourceSession).getXAResource();
tx.enlistResource(resSource);
XAResource resDest = ((XASession) targetSession).getXAResource();
tx.enlistResource(resDest);
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Enlisted resources in tx");
}
}
private void delistResources(final Transaction tx) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Delisting resources from tx");
}
XAResource resSource = ((XASession) sourceSession).getXAResource();
try {
tx.delistResource(resSource, XAResource.TMSUCCESS);
} catch (Exception e) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Failed to delist source resource", e);
}
}
XAResource resDest = ((XASession) targetSession).getXAResource();
try {
tx.delistResource(resDest, XAResource.TMSUCCESS);
} catch (Exception e) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Failed to delist target resource", e);
}
}
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Delisted resources from tx");
}
}
private Transaction startTx() throws Exception {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Starting JTA transaction");
}
if (tm == null) {
tm = ServiceUtils.getTransactionManager();
}
// Set timeout to a large value since we do not want to time out while waiting for messages
// to arrive - 10 years should be enough
tm.setTransactionTimeout(JMSBridgeImpl.TEN_YEARS);
tm.begin();
Transaction tx = tm.getTransaction();
// Remove the association between current thread - we don't want it
// we will be committing /rolling back directly on the transaction object
tm.suspend();
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Started JTA transaction");
}
return tx;
}
private Connection createConnection(final String username,
final String password,
final ConnectionFactoryFactory cff,
final String clientID,
final boolean isXA,
boolean isSource) throws Exception {
Connection conn = null;
try {
Object cf = cff.createConnectionFactory();
if (cf instanceof ActiveMQConnectionFactory && registry != null) {
registry.register(XARecoveryConfig.newConfig((ActiveMQConnectionFactory) cf, username, password, null));
}
if (qualityOfServiceMode == QualityOfServiceMode.ONCE_AND_ONLY_ONCE && !(cf instanceof XAConnectionFactory)) {
throw new IllegalArgumentException("Connection factory must be XAConnectionFactory");
}
if (username == null) {
if (isXA) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
}
conn = ((XAConnectionFactory) cf).createXAConnection();
} else {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
}
conn = ((ConnectionFactory) cf).createConnection();
}
} else {
if (isXA) {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
}
conn = ((XAConnectionFactory) cf).createXAConnection(username, password);
} else {
if (JMSBridgeImpl.trace) {
ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
}
conn = ((ConnectionFactory) cf).createConnection(username, password);
}
}
if (clientID != null) {
conn.setClientID(clientID);
}