use of jakarta.jms.XAConnection in project openmq by eclipse-ee4j.
the class SharedConnectionFactory method obtainConnection.
public Connection obtainConnection(Connection c, String logstr, Object caller, boolean doReconnect) throws Exception {
_lock.lockInterruptibly();
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Obtaining shared connection from shared connection factory " + this);
}
if (_closed) {
if (c == null) {
throw new JMSException(_jbr.getString(_jbr.X_SHARED_CF_CLOSED, this.toString()));
}
try {
c.close();
} catch (Exception e) {
_logger.log(Level.WARNING, "Unable to close conneciton from shared connection factory " + this);
}
throw new JMSException(_jbr.getString(_jbr.X_SHARED_CF_CLOSED, this.toString()));
}
if (c != null) {
if (c instanceof XAConnection) {
_conn = new SharedXAConnectionImpl((XAConnection) c);
} else {
_conn = new SharedConnectionImpl(c);
}
}
if (_conn != null && !_conn.isValid()) {
try {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_CLOSE_INVALID_CONN_IN_SHAREDCF, c.toString(), this.toString()));
((Connection) _conn).close();
} catch (Exception e) {
_logger.log(Level.WARNING, "Unable to close invalid connection " + _conn + " from shared connection factory " + this);
}
_conn = null;
}
try {
if (_conn == null) {
Connection cn = null;
EventListener l = new EventListener(this);
try {
_notifier.addEventListener(EventListener.EventType.CONN_CLOSE, l);
cn = JMSBridge.openConnection(cF, maxRetries, retryInterval, _username, _password, logstr, caller, l, _logger, doReconnect);
} finally {
_notifier.removeEventListener(l);
}
if (cF instanceof XAConnectionFactory) {
_conn = new SharedXAConnectionImpl((XAConnection) cn);
} else {
_conn = new SharedConnectionImpl(cn);
}
}
if (_closed) {
try {
((Connection) _conn).close();
} catch (Exception e) {
_logger.log(Level.FINE, "Exception on closing connection " + _conn + ": " + e.getMessage());
}
_conn = null;
throw new JMSException(_jbr.getString(_jbr.X_SHARED_CF_CLOSED, this.toString()));
}
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Increment refcnt in shared connection factory " + this);
}
refCount++;
if (_future != null) {
_future.cancel(true);
_future = null;
}
} finally {
_lock.unlock();
}
return (Connection) _conn;
}
use of jakarta.jms.XAConnection 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.XAConnection 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.XAConnection 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);
}
}
use of jakarta.jms.XAConnection in project openmq by eclipse-ee4j.
the class PooledConnectionFactory method obtainConnection.
/**
*/
public Connection obtainConnection(Connection c, String logstr, Object caller, boolean doReconnect) throws Exception {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Obtaining pooled connection from pooled connection factory " + this);
}
if (_closed) {
if (c == null) {
throw new JMSException(_jbr.getKString(_jbr.X_POOLED_CF_CLOSED, this.toString()));
}
try {
c.close();
} catch (Exception e) {
_logger.log(Level.WARNING, "Unable to close connection in pooled connection factory " + this);
}
throw new JMSException(_jbr.getKString(_jbr.X_POOLED_CF_CLOSED, this.toString()));
}
PooledConnection pconn = null;
if (c != null) {
if (c instanceof XAConnection) {
pconn = new PooledXAConnectionImpl((XAConnection) c);
} else {
pconn = new PooledConnectionImpl(c);
}
_idleConns.offer(pconn);
}
while (true) {
pconn = _idleConns.poll();
if (pconn == null) {
if (_closed) {
throw new JMSException(_jbr.getKString(_jbr.X_POOLED_CF_CLOSED, this.toString()));
}
Connection cn = null;
EventListener l = new EventListener(this);
try {
_notifier.addEventListener(EventListener.EventType.CONN_CLOSE, l);
cn = JMSBridge.openConnection(_cf, _maxRetries, _retryInterval, _username, _password, logstr, caller, l, _logger, doReconnect);
} finally {
_notifier.removeEventListener(l);
}
if (cn instanceof XAConnection) {
pconn = new PooledXAConnectionImpl((XAConnection) cn);
} else {
pconn = new PooledConnectionImpl(cn);
}
}
if (!_closed && pconn.isValid()) {
pconn.idleEnd();
_outConns.offer(pconn);
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Obtained pooled connection " + pconn + " from pooled connection factory " + this);
}
return (Connection) pconn;
}
try {
if (_closed) {
_logger.log(Level.INFO, "Closing connection " + pconn + " for pooled connection factory " + this + " is closed");
} else if (!pconn.isValid()) {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_CLOSE_INVALID_CONN_IN_POOLCF, pconn.toString(), this.toString()));
}
((Connection) pconn).close();
} catch (Exception e) {
_logger.log(Level.WARNING, "Unable to close connection " + pconn + " in pooled connection factory " + this + ": " + e.getMessage());
}
}
// while
}
Aggregations