use of jakarta.jms.XASession in project openmq by eclipse-ee4j.
the class JMSBridge method registerXAResources.
private void registerXAResources(Map<String, Refable> cfs) throws Exception {
TransactionManagerAdapter tma = getTransactionManagerAdapter();
if (!tma.registerRM()) {
return;
}
String cfref = null;
Refable cf = null;
XAConnection conn = null;
for (Map.Entry<String, Refable> pair : cfs.entrySet()) {
cfref = pair.getKey();
cf = pair.getValue();
if (!(cf instanceof XAConnectionFactory)) {
continue;
}
if (cf.isMultiRM()) {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_SKIP_REGISTER_MULTIRM_CF, JMSBridgeXMLConstant.CF.MULTIRM, cfref));
continue;
}
XAResource xar = null;
EventListener l = new EventListener(this);
try {
String username = _jmsbridge.getCF(cf.getRef()).getUsername();
String password = _jmsbridge.getCF(cf.getRef()).getPassword();
_notifier.addEventListener(EventListener.EventType.BRIDGE_STOP, l);
conn = (XAConnection) openConnection(cf, 1, 0, username, password, "", this, l, _logger);
XASession ss = conn.createXASession();
xar = ss.getXAResource();
_logger.log(Level.INFO, _jbr.getString(_jbr.I_REGISTER_RM, cfref, xar.toString()));
tma.registerRM(cfref, xar);
} catch (Throwable t) {
_logger.log(Level.WARNING, _jbr.getKString(_jbr.W_REGISTER_RM_ATTEMPT_FAILED, cfref, (xar == null ? "" : xar.toString())), t);
} finally {
_notifier.removeEventListener(l);
try {
conn.close();
} catch (Exception e) {
}
}
}
}
use of jakarta.jms.XASession in project openmq by eclipse-ee4j.
the class Link method initTarget.
private synchronized void initTarget(boolean start, boolean doReconnect, boolean checkState) throws Exception {
if (checkState) {
if (_state == LinkState.STOPPING || _state == LinkState.STOPPED) {
throw new JMSException("Link " + this + " is stopped");
}
}
_targetConnException = false;
String val = _tgtAttrs.getProperty(JMSBridgeXMLConstant.Target.STAYCONNECTED, JMSBridgeXMLConstant.Target.STAYCONNECTED_DEFAULT);
_targetStayConnected = Boolean.parseBoolean(val);
val = _tgtAttrs.getProperty(JMSBridgeXMLConstant.Target.CLIENTID);
if (val != null || _targetStayConnected) {
_targetConnType = "D";
_logger.log(Level.INFO, _jbr.getString(_jbr.I_CREATE_DEDICATED_TARGET_CONN, (val == null ? "" : "[ClientID=" + val + "]"), this.toString()));
EventListener l = new EventListener(this);
try {
_notifier.addEventListener(EventListener.EventType.LINK_STOP, l);
_notifier.addEventListener(EventListener.EventType.BRIDGE_STOP, l);
_targetConn = JMSBridge.openConnection(_targetCF, _parent.getCFAttributes(_targetCF), _jbr.getString(_jbr.M_TARGET), this, l, _logger, doReconnect);
} finally {
_notifier.removeEventListener(l);
}
if (val != null) {
try {
_targetConn.setClientID(val);
} catch (JMSException e) {
_logger.log(Level.WARNING, "Set client id " + val + " to target connection failed in " + this + ", try again ...", e);
if (Thread.currentThread().isInterrupted()) {
throw e;
}
Thread.sleep(_targetAttemptInterval);
_targetConn.setClientID(val);
}
}
} else if (!start) {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_GET_TARGET_CONN, this.toString()));
_targetConn = _parent.obtainConnection(_targetCF, _jbr.getString(_jbr.M_TARGET), this, start);
} else {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_DEFER_GET_TARGET_CONN, this.toString()));
return;
}
if (_targetConn instanceof PooledConnection) {
_targetConnType = "P";
} else if (_targetConn instanceof SharedConnection) {
_targetConnType = "S";
}
_targetConn.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
_logger.log(Level.WARNING, _jbr.getKString(_jbr.W_CONN_EXCEPTION_OCCURRED, _jbr.getString(_jbr.M_TARGET_1), this.toString()), exception);
if (_targetConn instanceof PooledConnection) {
((PooledConnection) _targetConn).invalid();
} else if (_targetConn instanceof SharedConnection) {
((SharedConnection) _targetConn).invalid();
}
_targetConnException = true;
}
});
try {
ConnectionMetaData md = _targetConn.getMetaData();
_targetProviderName = md.getJMSProviderName();
} catch (Exception e) {
_targetProviderName = null;
_logger.log(Level.WARNING, "Unable to get target JMSProvider from conn " + _targetConn + " in " + this + ": " + e.getMessage());
}
if (_targetConn instanceof XAConnection) {
_targetSession = ((XAConnection) _targetConn).createXASession();
XAResource xar = ((XASession) _targetSession).getXAResource();
String rm = _targetCF.getRef();
try {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_REGISTER_RM, rm, xar.toString()));
_parent.getTransactionManagerAdapter().registerRM(rm, xar);
} catch (Exception e) {
String[] eparam = { xar.toString(), rm, this.toString() };
_logger.log(Level.WARNING, _jbr.getKString(_jbr.W_REGISTER_TARGET_XARESOURCE_FAILED, eparam), e);
throw e;
}
} else {
_targetSession = _targetConn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
}
if (_targetDest instanceof Destination) {
_producer = _targetSession.createProducer((Destination) _targetDest);
} else if (_targetDest instanceof AutoDestination) {
AutoDestination ad = (AutoDestination) _targetDest;
if (ad.isQueue()) {
_producer = _targetSession.createProducer(_targetSession.createQueue(ad.getName()));
} else {
_producer = _targetSession.createProducer(_targetSession.createTopic(ad.getName()));
}
} else if (_targetDest.equals(JMSBridgeXMLConstant.Target.DESTINATIONREF_AS_SOURCE)) {
_unidentifiedProducer = true;
_producer = null;
} else {
throw new IllegalArgumentException("Unknown target destination type: " + _targetDest.getClass().getName() + " in " + this);
}
if (_producer != null && _producer instanceof com.sun.messaging.jmq.jmsclient.MessageProducerImpl) {
((com.sun.messaging.jmq.jmsclient.MessageProducerImpl) _producer)._setForJMSBridge();
}
}
use of jakarta.jms.XASession in project openmq by eclipse-ee4j.
the class Link method initSource.
private synchronized void initSource(boolean doReconnect, boolean checkState) throws Exception {
if (checkState) {
if (_state == LinkState.STOPPING || _state == LinkState.STOPPED) {
throw new JMSException(_jbr.getKString(_jbr.X_LINK_IS_STOPPED, this.toString()));
}
}
_sourceConnException = false;
String val = _srcAttrs.getProperty(JMSBridgeXMLConstant.Source.CLIENTID);
_sourceConnType = "D";
_logger.log(Level.INFO, _jbr.getString(_jbr.I_CREATE_DEDICATED_SOURCE_CONN, (val == null ? "" : "[ClientID=" + val + "]"), this.toString()));
EventListener l = new EventListener(this);
try {
_notifier.addEventListener(EventListener.EventType.LINK_STOP, l);
_notifier.addEventListener(EventListener.EventType.BRIDGE_STOP, l);
_sourceConn = JMSBridge.openConnection(_sourceCF, _parent.getCFAttributes(_sourceCF), _jbr.getString(_jbr.M_SOURCE), this, l, _logger, doReconnect);
} finally {
_notifier.removeEventListener(l);
}
if (val != null) {
try {
_sourceConn.setClientID(val);
} catch (JMSException e) {
_logger.log(Level.WARNING, "Set client id " + val + " to source connection failed in " + this + ", try again ...", e);
if (Thread.currentThread().isInterrupted()) {
throw e;
}
Thread.sleep(_sourceAttemptInterval);
_sourceConn.setClientID(val);
}
}
_sourceConn.stop();
_sourceConn.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException exception) {
_logger.log(Level.WARNING, _jbr.getKString(_jbr.W_CONN_EXCEPTION_OCCURRED, _jbr.getString(_jbr.M_SOURCE_1), this.toString()), exception);
if (_targetConn instanceof PooledConnection) {
((PooledConnection) _targetConn).invalid();
} else if (_targetConn instanceof SharedConnection) {
((SharedConnection) _targetConn).invalid();
}
_sourceConnException = true;
}
});
try {
ConnectionMetaData md = _sourceConn.getMetaData();
_sourceProviderName = md.getJMSProviderName();
} catch (Exception e) {
_sourceProviderName = null;
_logger.log(Level.WARNING, "Unable to get source JMSProvider from conn " + _sourceConn + " in " + this + ": " + e.getMessage());
}
if (_sourceConn instanceof XAConnection) {
_sourceSession = ((XAConnection) _sourceConn).createXASession();
XAResource xar = ((XASession) _sourceSession).getXAResource();
String rm = _sourceCF.getRef();
try {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_REGISTER_RM, rm, xar.toString()));
_parent.getTransactionManagerAdapter().registerRM(rm, xar);
} catch (Exception e) {
String[] eparam = { xar.toString(), rm, this.toString() };
_logger.log(Level.WARNING, _jbr.getKString(_jbr.W_REGISTER_SOURCE_XARESOURCE_FAILED, eparam), e);
throw e;
}
} else {
_sourceSession = _sourceConn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
}
String selector = _srcAttrs.getProperty(JMSBridgeXMLConstant.Source.SELECTOR);
Object dest = _sourceDest;
if (_sourceDest instanceof AutoDestination) {
AutoDestination ad = (AutoDestination) _sourceDest;
if (ad.isQueue()) {
dest = _sourceSession.createQueue(ad.getName());
} else {
dest = _sourceSession.createTopic(ad.getName());
}
}
if (dest instanceof Topic) {
String duraname = _srcAttrs.getProperty(JMSBridgeXMLConstant.Source.DURABLESUB);
if (duraname != null) {
_consumer = _sourceSession.createDurableSubscriber((Topic) dest, duraname, selector, true);
} else {
_consumer = _sourceSession.createConsumer((Topic) dest, selector, true);
}
} else if (dest instanceof Queue) {
_consumer = _sourceSession.createConsumer((Queue) dest, selector);
} else {
throw new IllegalArgumentException("Unknown source destination type: " + dest.getClass().getName() + " in " + this);
}
}
Aggregations