-
Notifications
You must be signed in to change notification settings - Fork 33
/
EndpointConsumer.java
891 lines (800 loc) · 39.2 KB
/
EndpointConsumer.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
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022 Contributors to Eclipse Foundation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jms.ra;
import jakarta.jms.*;
import javax.naming.NamingException;
import jakarta.resource.NotSupportedException;
import jakarta.resource.ResourceException;
import jakarta.resource.spi.InvalidPropertyException;
import jakarta.resource.spi.endpoint.MessageEndpointFactory;
import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.jmq.ClientConstants;
import com.sun.messaging.jmq.jmsservice.JMSService;
import com.sun.messaging.jms.ra.util.CustomTokenizer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Logger;
import java.lang.reflect.Method;
import com.sun.messaging.jms.ra.api.JMSRAEndpointConsumer;
/**
* Encapsulates a message consumer for the Oracle GlassFish(tm) Server Message Queue J2EE Resource Adapter.
*/
public class EndpointConsumer implements jakarta.jms.ExceptionListener, com.sun.messaging.jms.notification.EventListener, JMSRAEndpointConsumer {
private static final String QUEUE = "jakarta.jms.Queue";
private static final String TOPIC = "jakarta.jms.Topic";
/** Resource Adapter holding this epConsumer */
protected com.sun.messaging.jms.ra.ResourceAdapter ra = null;
/** XAConnectionFactory for this endpoint (not used for RADirect) */
private com.sun.messaging.XAConnectionFactory xacf = null;
/** DirectConnectionFactory for this endpoint (used for RADirect) */
protected DirectConnectionFactory dcf = null;
/** The Consumer ID associated with this epConsumer */
private int cID = 0;
/** The MessageEndpointFactory ID associated with this epConsumer */
private int fID = 0;
/** The destination type associated with this epConsumer */
private int destinationType = ClientConstants.DESTINATION_TYPE_UNKNOWN;
/**
* onMessage delivery related parameters
*/
protected boolean isDeliveryTransacted = false;
protected boolean noAckDelivery = false;
protected Method onMessageMethod = null;
/** The subscription durability associated with this epConsumer */
protected boolean isDurable = false;
protected String clientId = null;
protected String mName = null;
/** Flags whether this epConsumer is transcated */
//////// private boolean transactedDelivery = false;
/** Flags Deactivation */
protected boolean deactivated = false;
/** Connection and Session held by this epConsumer */
protected com.sun.messaging.jmq.jmsclient.XAConnectionImpl xac = null;
protected com.sun.messaging.jmq.jmsclient.XASessionImpl xas = null;
private DirectConnection dc = null;
private DirectSession ds = null;
/** MessageListener for this epConsumer */
protected MessageListener msgListener = null;
/** MessageConsumer for this epConsumer */
protected MessageConsumer msgConsumer = null;
protected MessageConsumer msgConsumer2 = null;
/** Destination for this epConsumer */
protected Destination destination = null;
/** whether this epConsumer is operating in RA direct mode or not */
protected boolean useRADirect = false;
protected ActivationSpec aSpec = null;
protected MessageEndpointFactory endpointFactory = null;
protected String username = null; // username only used for direct mode
protected String password = null; // password only used for direct mode
protected String selector = null;
protected String subscriptionName = null;
protected int exRedeliveryAttempts = 0;
protected int exRedeliveryInterval = 0;
private boolean stopping = false;
private boolean logRCFailures = true;
private int maxLoopDelay = 120000;
/* Loggers */
private static final String _className = "com.sun.messaging.jms.ra.EndpointConsumer";
protected static final String _lgrNameInboundMessage = "javax.resourceadapter.mqjmsra.inbound.message";
protected static final Logger _loggerIM = Logger.getLogger(_lgrNameInboundMessage);
protected static final String _lgrMIDPrefix = "MQJMSRA_EC";
protected static final String _lgrMID_EET = _lgrMIDPrefix + "1001: ";
protected static final String _lgrMID_INF = _lgrMIDPrefix + "1101: ";
protected static final String _lgrMID_WRN = _lgrMIDPrefix + "2001: ";
protected static final String _lgrMID_ERR = _lgrMIDPrefix + "3001: ";
protected static final String _lgrMID_EXC = _lgrMIDPrefix + "4001: ";
/** Create an EndpointConsumer for Direct mode */
public EndpointConsumer(com.sun.messaging.jms.ra.ResourceAdapter ra, MessageEndpointFactory endpointFactory, jakarta.resource.spi.ActivationSpec spec)
throws ResourceException {
if (ra == null || endpointFactory == null || spec == null) {
throw new NotSupportedException("MQRA:EC:const:null RA||EPF||AS");
}
if (!(spec instanceof com.sun.messaging.jms.ra.ActivationSpec)) {
throw new NotSupportedException("MQRA:EC:const:" + "Unsupported ActivationSpec Class-" + spec.getClass());
}
this.aSpec = (ActivationSpec) spec;
this.endpointFactory = endpointFactory;
this.ra = ra;
_init();
}
protected void _init() throws ResourceException {
if (!this.ra.getInAppClientContainer()) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
System.setProperty("imq.DaemonThreads", "true");
return null;
}
});
// System.setProperty("imq.DaemonThreads", "true");
}
// ask the activation spec whether this endpoint should use RADirect
// it will return true if the RA is configured to use RADirect and we haven't overridden addressList in the activation
// spec
useRADirect = this.aSpec.useRADirect();
Object cfObj = null;
String connectionFactoryLookup = aSpec.getConnectionFactoryLookup();
if (connectionFactoryLookup != null) {
try {
cfObj = Util.jndiLookup(connectionFactoryLookup);
} catch (NamingException e) {
String errorMessage = "MQRA:EC:Invalid connectionFactoryLookup " + connectionFactoryLookup
+ " configured in ActivationSpec of MDB for no JNDI name found";
throw new ResourceException(errorMessage, e);
}
}
// Configure connection factory
if (useRADirect) {
JMSService jmsservice = this.ra._getJMSService();
this.dcf = new com.sun.messaging.jms.ra.DirectConnectionFactory(jmsservice, null);
if (cfObj != null) {
DirectConnectionFactory cfd = (DirectConnectionFactory) cfObj;
ManagedConnectionFactory mcf = cfd.getMCF();
cfd.setMCF(mcf);
aSpec.setMCF(mcf);
}
this.username = this.aSpec.getUserName();
this.password = this.aSpec.getPassword();
} else {
xacf = new com.sun.messaging.XAConnectionFactory();
try {
if (cfObj != null) {
ConnectionFactoryAdapter cfa = (ConnectionFactoryAdapter) cfObj;
ManagedConnectionFactory mcf = cfa.getMCF();
aSpec.setMCF(mcf);
}
// get addressList from activation spec or (suitably adjusted) from the resource adapter
this.xacf.setProperty(ConnectionConfiguration.imqAddressList, aSpec._AddressList());
// get addressListBehavior from activation spec or resource adapter
xacf.setProperty(ConnectionConfiguration.imqAddressListBehavior, aSpec.getAddressListBehavior());
// get username from activation spec or resource adapter
this.xacf.setProperty(ConnectionConfiguration.imqDefaultUsername, aSpec.getUserName());
// get password from activation spec or resource adapter
this.xacf.setProperty(ConnectionConfiguration.imqDefaultPassword, aSpec.getPassword());
// get addressListIterations from activation spec or resource adapter
xacf.setProperty(ConnectionConfiguration.imqAddressListIterations, Integer.toString(aSpec.getAddressListIterations()));
// get reconnectAttempts from activation spec or resource adapter
this.xacf.setProperty(ConnectionConfiguration.imqReconnectAttempts, Integer.toString(aSpec.getReconnectAttempts()));
// get reconnectEnabled from activation spec
this.xacf.setProperty(ConnectionConfiguration.imqReconnectEnabled, Boolean.toString(aSpec.getReconnectEnabled()));
// get reconnectInterval from activation spec or resource adapter
this.xacf.setProperty(ConnectionConfiguration.imqReconnectInterval, Integer.toString(aSpec.getReconnectInterval()));
// configure xacf with any additional connection factory properties defined in the activation spec
setAdditionalConnectionFactoryProperties(aSpec.getOptions());
} catch (JMSException jmse) {
System.err.println("MQRA:EC:constr:Exception setting connection factory properties: " + jmse.getMessage());
}
}
this.onMessageMethod = this.ra._getOnMessageMethod();
this.exRedeliveryAttempts = this.aSpec.getEndpointExceptionRedeliveryAttempts();
this.exRedeliveryInterval = this.aSpec.getEndpointExceptionRedeliveryInterval();
this.mName = this.aSpec.getMdbName();
this.selector = this.aSpec.getMessageSelector();
this.subscriptionName = this.aSpec.getSubscriptionName();
String cId = this.aSpec.getClientId();
if ((cId != null) && !("".equals(cId)) && (cId.length() > 0)) {
this.clientId = cId;
} else {
this.clientId = null;
}
try {
this.isDeliveryTransacted = this.endpointFactory.isDeliveryTransacted(this.onMessageMethod);
} catch (NoSuchMethodException ex) {
// Assume delivery is non-transacted
// Fix to throw NotSupportedException on activation
// ex.printStackTrace();
}
setDestinationType();
if (this.destinationType == ClientConstants.DESTINATION_TYPE_TOPIC) {
// Will throw NotSupportedException if clientId not set properly
setIsDurable();
}
checkSubscriptionScopeAndClientId();
if (this.isDurable) {
if (this.clientId == null && aSpec.getSubscriptionScope() == null) {
// automatically generate client identifier
if (aSpec._getGroupName() != null) {
this.clientId = aSpec._getGroupName() + "{m:" + mName + "}";
} else {
this.clientId = "{m:" + mName + "}";
}
}
} else {
if ((aSpec._isNoAckDeliverySet()) && (this.destination instanceof com.sun.messaging.Topic) && (!this.isDeliveryTransacted)) {
this.noAckDelivery = true;
}
if (aSpec._isInClusteredContainerSet()) {
if (this.clientId == null && aSpec.getSubscriptionScope() == null) {
if ((mName == null) || ("".equals(mName))) {
throw new NotSupportedException("MQRA:EC:Error:Clustered Message Consumer requires"
+ " non-null clientID OR mdbName:clientID="
+ this.clientId + ":mdbName=" + mName);
} else {
// set effective clientId from mName
if (aSpec._getGroupName() != null) {
this.clientId = aSpec._getGroupName() + "{m:" + mName + "}";
} else {
this.clientId = "{m:" + mName + "}";
}
}
}
}
}
if (this.useRADirect) {
this.createDirectMessageConsumer();
} else {
this.createRemoteMessageConsumer();
}
}
// jakarta.jms.ExceptionListener interface method
@Override
public void onException(JMSException exception) {
_loggerIM.severe(_lgrMID_EXC + "onException:" + exception.getMessage());
// System.err.println("MQRA:EC:EL:Got Connection Exception:"+exception.getMessage());
logRCFailures = true;
if (msgListener != null) {
// System.err.println("MQRA:EC:EL:invalidatingOMRs");
msgListener.invalidateOnMessageRunners();
}
int loopDelay = aSpec.getReconnectInterval();
int loopCount = 0;
while (!stopping) {
// wait till initial interval expires
try {
Thread.sleep(loopDelay);
} catch (Exception ie) {
}
try {
loopCount += 1;
if (logRCFailures) {
_loggerIM.severe(_lgrMID_EXC + "onException:" + aSpec.toString());
// System.err.println("MQRA:EC:EL:"+aSpec.toString());
}
_loggerIM.severe(_lgrMID_EXC + "onException:reconnect attempt loop# " + loopCount + " :Delayed " + loopDelay + " milliseconds.");
// System.err.println("MQRA:EC:EL:addressList reconnect attempt_loop#"+loopCount+":Delayed "+loopDelay+"
// milliseconds.");
synchronized (this) {
if (!stopping) {
createRemoteMessageConsumer();
_loggerIM.severe(_lgrMID_EXC + "onException:reconnect success on loop# " + loopCount + " for " + aSpec.toString());
// System.err.println("MQRA:EC:EL:RE-CONNECTED consumer:on loop#"+loopCount+" for "+aSpec.toString());
}
}
break;
} catch (Exception e) {
if (logRCFailures) {
_loggerIM.severe(_lgrMID_EXC + "onException:Unable to re-establish connection for " + aSpec.toString() + "\nin " + ra.toString());
// System.err.println("MQRA:EC:EL:Exception SEVERE:Unable to re-establish connection for
// "+aSpec.toString()+"\n"+ra.toString());
} else {
logRCFailures = false;
}
if (loopDelay < maxLoopDelay) {
loopDelay *= 3;
if (loopDelay > maxLoopDelay) {
loopDelay = maxLoopDelay;
}
}
}
}
}
// com.sun.messaging.jms.notification.EventListener interface method
@Override
public void onEvent(com.sun.messaging.jms.notification.Event evnt) {
_loggerIM.entering(_className, "onEvent()", evnt);
_loggerIM.info(_lgrMID_INF + "onEvent:Connection Event:" + evnt.toString());
}
// Public Methods
//
/**
* Returns the ResourceAdapter associated with this EndpointConsumer
*
* @return The ResourceAdapter
*/
public com.sun.messaging.jms.ra.ResourceAdapter getResourceAdapter() {
return ra;
}
/**
* Returns the consumerID associated with this EndpointConsumer
*
* @return The consumerID
*/
public int getConsumerID() {
return cID;
}
/*
* Returns the factoryID for the MessageFactory associated with this EndpointConsumer
*
* @return The factoryID
*/
public int getFactoryID() {
return fID;
}
/**
* Returns the MessageEndpointFactory associated with this EndpointConsumer
*
* @return The MessageEndpointFactory
*/
public MessageEndpointFactory getMessageEndpointFactory() {
// System.out.println("MQRA:EC:getMsgEpFctry:fID="+fID);
MessageEndpointFactory epf = ra._getMessageFactory(fID);
return epf;
}
/**
* Returns the XASession associated with this EndpointConsumer
*
* @return The XASession
*/
@Override
public jakarta.jms.XASession getXASession() {
return xas;
}
public DirectSession getDirectSession() {
return this.ds;
}
/**
* Sets this EndpointConsumer to Deactivated
*
*/
public void setDeactivated() {
deactivated = true;
}
/**
* Creates a MessageConsumer associated with this EndpointConsumer after validating the passed in ActivationSpec
* parameter. The MessageConsumer delivers the messages to a MessageEndpoint manufactured using the
* MessageEndpointFactory passed in.
*
*/
private void createRemoteMessageConsumer() throws ResourceException {
try {
xac = (com.sun.messaging.jmq.jmsclient.XAConnectionImpl) xacf.createXAConnection();
if (xac == null) { // This should never happen
throw new ResourceException("MQRA:EC:Error:createRemoteMessageConsumer failed: cannot create XAConnection");
}
if ((aSpec._isInClusteredContainerSet())) {
xac.setRANamespaceUID(aSpec._getRAUID());
}
_loggerIM.fine("MQRA:EC:createRemoteMessageConsumer setting clientID to " + this.clientId);
if (this.clientId != null) {
xac.setClientID(this.clientId);
}
xac.setExceptionListener(this);
xac.setEventListener(this);
} catch (JMSException jmse) {
if (xac != null) {
try {
xac.close();
} catch (JMSException jmsecc) {
}
xac = null;
}
if (logRCFailures) {
jmse.printStackTrace();
}
throw new NotSupportedException("MQRA:EC:Error:createRemoteMessageConsumer failed:aborting due to:" + jmse.getMessage(), jmse);
}
// success
try {
if (this.isDurable) {
this.xas = (com.sun.messaging.jmq.jmsclient.XASessionImpl) xac.createSession(false, Session.CLIENT_ACKNOWLEDGE);
} else {
if (noAckDelivery) {
this.xas = (com.sun.messaging.jmq.jmsclient.XASessionImpl) xac.createSession(com.sun.messaging.jms.Session.NO_ACKNOWLEDGE);
} else {
this.xas = (com.sun.messaging.jmq.jmsclient.XASessionImpl) xac.createSession(false, Session.CLIENT_ACKNOWLEDGE);
}
}
xas._setRAEndpointSession();
if (aSpec.getSubscriptionScope() != null) {
subscriptionName = getSubscriptionName();
if (this.isDurable) {
msgConsumer = xas.createSharedDurableConsumer((Topic) destination, subscriptionName, aSpec.getMessageSelector());
} else {
msgConsumer = xas.createSharedConsumer((Topic) destination, subscriptionName, aSpec.getMessageSelector());
}
} else {
if (this.isDurable) {
msgConsumer = xas.createDurableSubscriber((Topic) destination, aSpec.getSubscriptionName(), aSpec.getMessageSelector(), false);
} else {
msgConsumer = xas.createConsumer(destination, aSpec.getMessageSelector());
// test to see if Queue is enabled for more than one consumer when InClustered true
if (destination instanceof jakarta.jms.Queue && aSpec._isInClusteredContainerSet()) {
// Fail activation if it is not
try {
msgConsumer2 = xas.createConsumer(destination, aSpec.getMessageSelector());
msgConsumer2.close();
msgConsumer2 = null;
} catch (JMSException jmse) {
try {
xac.close();
} catch (JMSException jmsecc) {
// System.out.println("MQRA:EC:closed xac on conn creation exception-"+jmsecc.getMessage());
}
xac = null;
throw new NotSupportedException("MQRA:EC:Error clustering multiple consumers on Queue:\n" + jmse.getMessage(), jmse);
}
}
}
}
msgListener = new MessageListener(this, this.endpointFactory, aSpec);
// System.out.println("MQRA:EC:Created msgListener");
// msgConsumer.setMessageListener(new MessageListener(this, epFactory, spec));
msgConsumer.setMessageListener(msgListener);
// System.out.println("MQRA:EC:Set msgListener");
// System.out.println("MQRA:EC:Starting Connection");
xac.start();
updateFactoryConsumerTables(this.endpointFactory);
} catch (JMSException jmse) {
if (xac != null) {
try {
xac.close();
} catch (JMSException jmsecc) {
// System.out.println("MQRA:EC:closed xac on conn creation exception-"+jmsecc.getMessage());
}
xac = null;
}
throw new NotSupportedException("MQRA:EC:Error creating Remote Message Consumer:\n" + jmse.getMessage(), jmse);
}
}
/**
* Set additional arbitrary connection factory properties
*
* The properties must be specified as a String containing a comma-separated list of name=value pairs e.g.
* prop1=value1,prop2=value2 If a value contains a = or , you can either place the whole value between quotes
* (prop1="val=ue") or use \ as an escape character (prop1=val\,ue)
*
* This method cannot be used to set properties which are configured internally or which have their own setter methods.
* These are: imqReconnectEnabled, imqReconnectAttempts, imqReconnectInterval, imqDefaultUsername, imqDefaultPassword,
* imqAddressList, imqAddressListIterations, imqAddressListBehavior
*
* Any values specified for those properties will be ignored
*
* @param stringProps connection factory properties as a comma-separated list of name=value pairs
*/
private void setAdditionalConnectionFactoryProperties(String stringProps) {
if (stringProps == null) {
return;
}
Hashtable<String, String> props = null;
try {
props = CustomTokenizer.parseToProperties(stringProps);
} catch (InvalidPropertyException ipe) {
// syntax error in properties
IllegalArgumentException iae = new IllegalArgumentException(_lgrMID_EXC + "Invalid value for activation spec property options: " + stringProps);
iae.initCause(ipe);
throw iae;
}
for (Enumeration<String> keysEnum = props.keys(); keysEnum.hasMoreElements();) {
String thisPropertyName = keysEnum.nextElement();
// don't allow properties that are, or can be, set elsewhere to be overridden here as this might have unexpected results
if (thisPropertyName.equals(ConnectionConfiguration.imqReconnectEnabled) || thisPropertyName.equals(ConnectionConfiguration.imqReconnectInterval)
|| thisPropertyName.equals(ConnectionConfiguration.imqDefaultUsername)
|| thisPropertyName.equals(ConnectionConfiguration.imqDefaultPassword) || thisPropertyName.equals(ConnectionConfiguration.imqAddressList)
|| thisPropertyName.equals(ConnectionConfiguration.imqAddressListIterations)
|| thisPropertyName.equals(ConnectionConfiguration.imqAddressListBehavior)
|| thisPropertyName.equals(ConnectionConfiguration.imqReconnectAttempts)) {
_loggerIM.warning(_lgrMID_WRN + "Cannot use activation spec property options to set property " + thisPropertyName + ": ignoring");
continue;
}
try {
xacf.setProperty(thisPropertyName, props.get(thisPropertyName));
} catch (JMSException e) {
IllegalArgumentException iae = new IllegalArgumentException(_lgrMID_EXC + "Error setting connection factory property " + thisPropertyName
+ " (defined in activation spec property options) to " + props.get(thisPropertyName));
iae.initCause(e);
throw iae;
}
}
}
protected void startMessageConsumer() throws Exception {
}
/**
* Stop a consumer and connections associated with this EndpointConsumer
*/
public void stopMessageConsumer() throws Exception {
if (this.useRADirect) {
stopDirectMessageConsumer();
} else {
stopRemoteMessageConsumer();
}
}
/**
* Stops a consumer and connections associated with this EndpointConsumer
*/
public void stopRemoteMessageConsumer() throws Exception {
stopping = true;
synchronized (this) {
com.sun.messaging.jmq.jmsclient.SessionImpl mqsess;
// System.out.println("MQRA:EC:stopMessageConsumer()");
if (msgConsumer != null) {
try {
if (msgListener != null) {
mqsess = xas;
// System.out.println("MQRA:EC:stopMessageConsumer:_stopFromRA:sessionId="+mqsess.getBrokerSessionID());
mqsess._stopFromRA();
// System.out.println("MQRA:EC:stopMessageConsumer:_stopFromRA-done/wfOMRs");
msgListener.waitForAllOnMessageRunners();
// System.out.println("MQRA:EC:stopMessageConsumer:wfOMRs-done/releaseOMRs");
msgListener.releaseOnMessageRunners();
// System.out.println("MQRA:EC:stopMessageConsumer:Done releasing OMRs/session.close()");
xas.close();
// System.out.println("MQRA:EC:stopMessageConsumer:Done session.close()");
}
// System.out.println("MQRA:EC:stopMessageConsumer:_stopFromRA");
//////// <---((com.sun.messaging.jmq.jmsclient.SessionImpl)xas)._stopFromRA();
// System.out.println("MQRA:EC:stopMessageConsumer:done _stopFromRA");
// System.out.println("MQRA:EC:stopMessageConsumer:closing msgConsumer");
/////////////// msgConsumer.close();
// System.out.println("MQRA:EC:stopMessageConsumer:closed msgConsumer...........................");
} catch (JMSException jmse) {
ResourceException re = new ResourceException("MQRA:EC:Error on closing MessageConsumer");
re.initCause(jmse);
throw re;
}
}
if (xac != null) {
try {
xac.close();
} catch (JMSException jmse) {
ResourceException re = new ResourceException("MQRA:EC:Error closing JMS Connection");
re.initCause(jmse);
throw re;
}
}
}
}
/**
* Updates the factory and consumer tables held by the resource adapter
*
*/
private void updateFactoryConsumerTables(MessageEndpointFactory endpointFactory) {
cID = ra.addEndpointConsumer(this);
fID = ra.addMessageFactory(endpointFactory);
ra.addFactorytoConsumerLink(fID, cID);
// System.out.println("MQRA:EC:updateFactoryConsumerTables:fID="+fID+" cID="+cID+":"+spec.toString());
}
/**
* Sets destinationType from the ActivationSpec instance passed in and validates related configs
*/
private void setDestinationType() throws ResourceException {
String destName = aSpec.getDestination();
String destinationLookup = aSpec.getDestinationLookup();
Object destObj = null;
if (destinationLookup != null) {
try {
destObj = Util.jndiLookup(destinationLookup);
} catch (NamingException e) {
String errorMessage = "MQRA:EC:Invalid destinationLookup " + destinationLookup + " configured in ActivationSpec of MDB for no JNDI name found";
throw new ResourceException(errorMessage, e);
}
if (destObj != null) {
if (destObj instanceof com.sun.messaging.Destination) {
destName = ((com.sun.messaging.Destination) destObj).getName();
} else {
String errorMessage = "MQRA:EC:Invalid destinationLookup " + destinationLookup
+ " configured in ActivationSpec of MDB, The JNDI object is required to be a Destination";
throw new NotSupportedException(errorMessage);
}
} else {
String errorMessage = "MQRA:EC:Invalid destinationLookup " + destinationLookup + " configured in ActivationSpec of MDB for JNDI object is null";
throw new NotSupportedException(errorMessage);
}
}
try {
if (destObj != null) {
if (destObj instanceof com.sun.messaging.Queue) {
if (aSpec._isDestTypeTopicSet()) {
throw new InvalidPropertyException("MQRA:EC:Inconsistent destinationType is set for destinationLookup " + destinationLookup);
}
this.destination = new com.sun.messaging.Queue(destName);
this.destinationType = ClientConstants.DESTINATION_TYPE_QUEUE;
} else {
if (destObj instanceof com.sun.messaging.Topic) {
if (aSpec._isDestTypeQueueSet()) {
throw new InvalidPropertyException("MQRA:EC:Inconsistent destinationType is set for destinationLookup " + destinationLookup);
}
this.destination = new com.sun.messaging.Topic(destName);
this.destinationType = ClientConstants.DESTINATION_TYPE_TOPIC;
}
}
} else if (aSpec._isDestTypeQueueSet()) {
this.destination = new com.sun.messaging.Queue(destName);
this.destinationType = ClientConstants.DESTINATION_TYPE_QUEUE;
} else {
// destinationType is TOPIC by default
this.destination = new com.sun.messaging.Topic(destName);
this.destinationType = ClientConstants.DESTINATION_TYPE_TOPIC;
}
} catch (JMSException jmse) {
String errorMessage;
if (destName == null) {
errorMessage = "MQRA:EC:No destination configured in ActivationSpec of MDB";
} else {
errorMessage = "MQRA:EC:Invalid destination " + destName + " configured in ActivationSpec of MDB";
}
NotSupportedException nse = new NotSupportedException(errorMessage);
nse.initCause(jmse);
throw nse;
}
// XXX:Does MDB Deployment need physical dest to exist?
// If so need Admin API to check
// XXX:Can MDB depoyment handle destname as resource-ref vs. physical dest name?
// If so, need to handle in ActivationSpec (how will AS handle this?)
}
/**
* Sets isDurable from the ActivationSpec instance passed in and validates related configs
*/
private void setIsDurable() throws NotSupportedException {
// If durable subscription, validate subscriptionName
if (aSpec._isDurableSet()) {
if (aSpec.getSubscriptionScope() == null) {
String sName = this.subscriptionName;
if ((sName == null) || (sName.length() <= 0)) {
throw new NotSupportedException("MQRA:EC:Need Valid SubscriptionName-" + sName);
}
}
// Setting this indicates everything is valid
this.isDurable = true;
}
}
/**
* Create a direct mode message consumer
*/
protected void createDirectMessageConsumer() throws NotSupportedException {
try {
// Use method that avoids allocation via the ConnectionManager
this.dc = (DirectConnection) dcf._createConnection(username, password);
if (this.clientId != null) {
this.dc._setClientID(this.clientId);
}
if (ResourceAdapter._isFixCR6760301()) {
this.ds = (DirectSession) this.dc.createSessionForRAEndpoint();
} else {
this.ds = (DirectSession) this.dc.createSession(false, Session.CLIENT_ACKNOWLEDGE);
}
// Set the Session to be an MDB Session
this.ds._setMDBSession(true);
if (aSpec.getSubscriptionScope() != null) {
subscriptionName = getSubscriptionName();
if (this.isDurable) {
this.msgConsumer = this.ds.createSharedDurableConsumer((Topic) this.destination, subscriptionName, this.selector);
} else {
this.msgConsumer = this.ds.createSharedConsumer((Topic) this.destination, subscriptionName, this.selector);
}
} else {
if (this.isDurable) {
this.msgConsumer = this.ds.createDurableSubscriber((Topic) this.destination, this.subscriptionName, this.selector, false);
} else {
this.msgConsumer = this.ds.createConsumer(this.destination, this.selector);
}
}
this.msgListener = new MessageListener(this, this.endpointFactory, this.aSpec, this.noAckDelivery, this.useRADirect);
this.msgConsumer.setMessageListener(this.msgListener);
this.dc.start();
updateFactoryConsumerTables(this.endpointFactory);
} catch (JMSException jmse) {
if (this.dc != null) {
try {
this.dc.close();
} catch (JMSException jmsecc) {
// System.out.println("MQRA:EC:closed xac on conn creation exception-"+jmsecc.getMessage());
}
this.dc = null;
}
NotSupportedException nse = new NotSupportedException("MQRA:EC:Error creating Direct Message Consumer:\n" + jmse.getMessage());
nse.initCause(jmse);
throw nse;
}
}
/**
* Start the Direct MessageConsumer
*/
protected void startDirectConsumer() throws NotSupportedException {
}
/**
* Stop the Direct MessageConsumer
*/
protected void stopDirectMessageConsumer() throws Exception {
stopping = true;
synchronized (this) {
if (this.msgConsumer != null) {
try {
if (this.msgListener != null) {
this.ds._stop();
msgListener.waitForAllOnMessageRunners();
msgListener.releaseOnMessageRunners();
// this.ds._close(); //Will be done by dc.close
}
} catch (JMSException jmse) {
ResourceException re = new ResourceException("MQRA:EC:Error on closing Direct MessageConsumer");
re.initCause(jmse);
throw re;
}
}
if (this.dc != null) {
try {
this.dc.close();
} catch (JMSException jmse) {
ResourceException re = new ResourceException("MQRA:EC:Error closing DircetConnection");
re.initCause(jmse);
throw re;
}
}
}
}
/**
* Get or generate the subscription name
*/
protected String getSubscriptionName() {
if (aSpec.getSubscriptionScope() == null) {
return aSpec.getSubscriptionName();
}
String subscriptionName = null;
if (aSpec.getSubscriptionName() == null) {
// user doesn't specify activationSpec property subscriptionName,
// so we generate a name according to chapter 12.1 of jms20 spec
String activationName = this.endpointFactory.getActivationName();
if (aSpec.getSubscriptionScope().equals("Cluster")) {
// Cluster scope
subscriptionName = activationName;
} else {
// Instance scope
if (aSpec._isInClusteredContainerSet()) {
// it is in a glassfish cluster
// String instanceName = this.ra.getBootstrapContext().getInstanceName();
// subscriptionName = instanceName + "_" + activationName;
} else {
// it is standalone glassfish instance
subscriptionName = activationName;
}
}
} else {
// user specifies activationSpec property subscriptionName
if (aSpec.getSubscriptionScope().equals("Instance")) {
// Instance scope
if (aSpec._isInClusteredContainerSet()) {
// it is in a glassfish cluster
// String instanceName = this.ra.getBootstrapContext().getInstanceName();
// subscriptionName = instanceName + "_" + aSpec.getSubscriptionName();
} else {
// it is standalone glassfish instance
subscriptionName = aSpec.getSubscriptionName();
}
} else {
// Cluster scope
subscriptionName = aSpec.getSubscriptionName();
}
}
_loggerIM.fine("MQRA:EC:Use subscription name '" + subscriptionName + "' for endpoint activation");
return subscriptionName;
}
protected void checkSubscriptionScopeAndClientId() throws NotSupportedException {
if (aSpec.getSubscriptionScope() != null) {
if (QUEUE.equals(aSpec.getDestinationType())) {
NotSupportedException nse = new NotSupportedException("MQRA:EC:Error:Bad parameter");
nse.initCause(new InvalidPropertyException(_lgrMID_EXC + "subscriptionScope must not be set if destinationType is " + QUEUE));
throw nse;
} else if (TOPIC.equals(aSpec.getDestinationType()) && clientId != null) {
NotSupportedException nse = new NotSupportedException("MQRA:EC:Error:Bad parameter");
nse.initCause(new InvalidPropertyException(_lgrMID_EXC + "clientId must not be set if subscriptionScope is set"));
throw nse;
}
}
}
}