Search in sources :

Example 1 with XASession

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) {
            }
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) TransactionManagerAdapter(com.sun.messaging.bridge.service.jms.tx.TransactionManagerAdapter) XASession(jakarta.jms.XASession) XAConnectionFactory(jakarta.jms.XAConnectionFactory) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JMSException(jakarta.jms.JMSException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) InvalidClientIDException(jakarta.jms.InvalidClientIDException) JMSSecurityException(jakarta.jms.JMSSecurityException) XAConnection(jakarta.jms.XAConnection)

Example 2 with XASession

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();
    }
}
Also used : Destination(jakarta.jms.Destination) JMSException(jakarta.jms.JMSException) BridgeException(com.sun.messaging.bridge.api.BridgeException) JMSException(jakarta.jms.JMSException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) RollbackException(jakarta.transaction.RollbackException) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) XAResource(javax.transaction.xa.XAResource) ConnectionMetaData(jakarta.jms.ConnectionMetaData) XASession(jakarta.jms.XASession) ExceptionListener(jakarta.jms.ExceptionListener) XAConnection(jakarta.jms.XAConnection)

Example 3 with XASession

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);
    }
}
Also used : JMSException(jakarta.jms.JMSException) BridgeException(com.sun.messaging.bridge.api.BridgeException) JMSException(jakarta.jms.JMSException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) RollbackException(jakarta.transaction.RollbackException) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) XAResource(javax.transaction.xa.XAResource) ConnectionMetaData(jakarta.jms.ConnectionMetaData) XASession(jakarta.jms.XASession) ExceptionListener(jakarta.jms.ExceptionListener) Topic(jakarta.jms.Topic) Queue(jakarta.jms.Queue) XAConnection(jakarta.jms.XAConnection)

Aggregations

JMSException (jakarta.jms.JMSException)3 XAConnection (jakarta.jms.XAConnection)3 XASession (jakarta.jms.XASession)3 XAResource (javax.transaction.xa.XAResource)3 BridgeException (com.sun.messaging.bridge.api.BridgeException)2 ConnectionMetaData (jakarta.jms.ConnectionMetaData)2 ExceptionListener (jakarta.jms.ExceptionListener)2 HeuristicMixedException (jakarta.transaction.HeuristicMixedException)2 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)2 RollbackException (jakarta.transaction.RollbackException)2 TransactionManagerAdapter (com.sun.messaging.bridge.service.jms.tx.TransactionManagerAdapter)1 Destination (jakarta.jms.Destination)1 InvalidClientIDException (jakarta.jms.InvalidClientIDException)1 JMSSecurityException (jakarta.jms.JMSSecurityException)1 Queue (jakarta.jms.Queue)1 Topic (jakarta.jms.Topic)1 XAConnectionFactory (jakarta.jms.XAConnectionFactory)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1