Search in sources :

Example 1 with XAConnectionFactory

use of jakarta.jms.XAConnectionFactory 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;
}
Also used : XAConnection(jakarta.jms.XAConnection) Connection(jakarta.jms.Connection) XAConnectionFactory(jakarta.jms.XAConnectionFactory) JMSException(jakarta.jms.JMSException) JMSException(jakarta.jms.JMSException) XAConnection(jakarta.jms.XAConnection)

Example 2 with XAConnectionFactory

use of jakarta.jms.XAConnectionFactory in project openmq by eclipse-ee4j.

the class JMSBridge method init.

public void init(BridgeContext bc, String name, boolean reset) throws Exception {
    _bc = bc;
    _name = name;
    _reset = reset;
    Properties props = bc.getConfig();
    String domain = props.getProperty(BridgeContext.BRIDGE_PROP_PREFIX);
    _xmlurl = props.getProperty(domain + PROP_XMLURL_SUFFIX);
    if (_xmlurl == null) {
        throw new IllegalArgumentException(_jbr.getKString(_jbr.X_NOT_SPECIFIED, _name, domain + PROP_XMLURL_SUFFIX));
    }
    _logger = Logger.getLogger(domain);
    if (bc.isSilentMode()) {
        _logger.setUseParentHandlers(false);
    }
    String var = bc.getRootDir();
    File dir = new File(var);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    String logfile = var + File.separator + "jms%g.log";
    int limit = 0, count = 1;
    String limits = props.getProperty(domain + PROP_LOGFILE_LIMIT_SUFFIX);
    if (limits != null) {
        limit = Integer.parseInt(limits);
    }
    String counts = props.getProperty(domain + PROP_LOGFILE_COUNT_SUFFIX);
    if (counts != null) {
        count = Integer.parseInt(counts);
    }
    FileHandler h = new FileHandler(logfile, limit, count, true);
    h.setFormatter(new LogSimpleFormatter(_logger));
    _logger.addHandler(h);
    _logger.log(Level.INFO, _jbr.getString(_jbr.I_LOG_DOMAIN, _name, domain));
    _logger.log(Level.INFO, _jbr.getString(_jbr.I_LOG_FILE, _name, logfile) + "[" + limit + "," + count + "]");
    String lib = bc.getLibDir();
    if (lib == null) {
        throw new IllegalArgumentException("JMS bridge " + _name + " lib dir not specified");
    }
    String dtdd = lib + File.separator + "dtd" + File.separator;
    File dtddir = new File(dtdd);
    if (!dtddir.exists()) {
        throw new IllegalArgumentException(_jbr.getKString(_jbr.X_NOT_EXIST, _name, dtdd));
    }
    String sysid = dtddir.toURI().toURL().toString();
    String[] param = { _name, _xmlurl, sysid };
    _logger.log(Level.INFO, _jbr.getString(_jbr.I_INIT_JMSBRIDGE_WITH, param));
    JMSBridgeReader reader = new JMSBridgeReader(_xmlurl, sysid, _logger);
    _jmsbridge = reader.getJMSBridgeElement();
    if (!_name.equals(_jmsbridge.getName())) {
        String[] eparam = { _name, _jmsbridge.getName(), _xmlurl };
        throw new IllegalArgumentException(_jbr.getKString(_jbr.X_JMSBRIDGE_NAME_MISMATCH, eparam));
    }
    createBuiltInDMQ(_jmsbridge);
    Map<String, DMQElement> edmqs = _jmsbridge.getDMQs();
    for (Map.Entry<String, DMQElement> pair : edmqs.entrySet()) {
        if (pair.getKey().equals(DMQElement.BUILTIN_DMQ_NAME)) {
            continue;
        }
        createDMQ(pair.getValue(), _jmsbridge);
    }
    boolean xa = false;
    Map<String, LinkElement> elinks = _jmsbridge.getLinks();
    for (Map.Entry<String, LinkElement> pair : elinks.entrySet()) {
        xa |= createLink(pair.getValue(), _jmsbridge);
    }
    if (xa) {
        TransactionManagerAdapter tma = getTransactionManagerAdapter();
        if (tma.registerRM()) {
            Map<String, Refable> xacfs = new LinkedHashMap<>(_localXACFs);
            for (Map.Entry<String, Refable> pair : _allCF.entrySet()) {
                if (xacfs.get(pair.getKey()) != null) {
                    continue;
                }
                if (pair.getValue() instanceof XAConnectionFactory) {
                    xacfs.put(pair.getKey(), pair.getValue());
                }
            }
            String cfref = null;
            Refable cf = null;
            Map<String, ConnectionFactoryElement> cfs = _jmsbridge.getAllCF();
            for (Map.Entry<String, ConnectionFactoryElement> pair : cfs.entrySet()) {
                cfref = pair.getKey();
                if (xacfs.get(cfref) != null) {
                    continue;
                }
                try {
                    cf = createConnectionFactory(pair.getValue(), true);
                } catch (NotXAConnectionFactoryException e) {
                    _logger.log(Level.INFO, _jbr.getString(_jbr.I_CF_NOT_XA_NO_REGISTER, pair.getKey(), "XAConnectionFactory"));
                    continue;
                }
                if (cf == null) {
                    cf = new XAConnectionFactoryImpl(_bc, _jmsbridge.getCF(cfref).getProperties(), _bc.isEmbeded(), cfref, pair.getValue().isMultiRM());
                }
                xacfs.put(cfref, cf);
            }
            registerXAResources(xacfs);
        } else {
            Link link = null;
            for (Map.Entry<String, Link> pair : _links.entrySet()) {
                link = pair.getValue();
                if (link.isTransacted()) {
                    link.registerXAResources();
                }
            }
        }
    }
    _asyncStartExecutor = Executors.newSingleThreadExecutor();
}
Also used : Properties(java.util.Properties) LinkedHashMap(java.util.LinkedHashMap) LogSimpleFormatter(com.sun.messaging.bridge.api.LogSimpleFormatter) TransactionManagerAdapter(com.sun.messaging.bridge.service.jms.tx.TransactionManagerAdapter) FileHandler(java.util.logging.FileHandler) XAConnectionFactory(jakarta.jms.XAConnectionFactory) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with XAConnectionFactory

use of jakarta.jms.XAConnectionFactory 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 4 with XAConnectionFactory

use of jakarta.jms.XAConnectionFactory in project openmq by eclipse-ee4j.

the class JMSBridge method openConnection.

public static Connection openConnection(Object cf, int maximumAttempts, long attemptInterval, String username, String password, String logstr, Object caller, EventListener l, Logger logger, boolean doReconnect) throws Exception {
    if (l.hasEventOccurred()) {
        throw new JMSException("" + l.occurredEvent());
    }
    int maxAttempts = maximumAttempts;
    Connection conn = null;
    int attempts = 0;
    Exception ee = null;
    int invalidClientIDExceptionCnt = 0;
    do {
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedException(_jbr.getKString(_jbr.X_OPENCONNECTION_INTERRUPTED, cf.toString(), caller.toString()));
        }
        if (attempts > 0 && attemptInterval > 0) {
            Thread.sleep(attemptInterval);
        }
        try {
            String[] param = { (username == null ? "" : "[" + username + "]"), cf.toString(), caller.toString() };
            if (cf instanceof XAConnectionFactory) {
                logger.log(Level.INFO, _jbr.getString(_jbr.I_CREATING_XA_CONN, param));
                if (username == null) {
                    conn = ((XAConnectionFactory) cf).createXAConnection();
                } else {
                    conn = ((XAConnectionFactory) cf).createXAConnection(username, password);
                }
            } else {
                logger.log(Level.INFO, _jbr.getString(_jbr.I_CREATING_CONN, param));
                if (username == null) {
                    conn = ((ConnectionFactory) cf).createConnection();
                } else {
                    conn = ((ConnectionFactory) cf).createConnection(username, password);
                }
            }
            return conn;
        } catch (JMSException e) {
            attempts++;
            ee = e;
            Exception le = e.getLinkedException();
            if (e instanceof InvalidClientIDException) {
                invalidClientIDExceptionCnt++;
            }
            if (e instanceof JMSSecurityException || le instanceof JMSSecurityException || (e instanceof InvalidClientIDException && invalidClientIDExceptionCnt > 1)) {
                String[] eparam = { logstr, caller.toString(), attempts + "(" + attemptInterval + ")" };
                _logger.log(Level.SEVERE, _jbr.getKString(_jbr.W_EXCEPTION_CREATING_CONN, eparam), e);
                throw e;
            }
            String[] eparam = { logstr, caller.toString(), attempts + "(" + attemptInterval + "): " + e.getMessage() + (le == null ? "" : " - " + le.getMessage()) };
            _logger.log(Level.WARNING, _jbr.getKString(_jbr.W_EXCEPTION_CREATING_CONN, eparam));
            if (!doReconnect && (maxAttempts > 1 || maxAttempts < 0)) {
                throw new ProviderConnectException("Failed to connect to " + cf + ": " + e.getMessage() + (le == null ? "" : " - " + le.getMessage()));
            }
        }
    } while ((maxAttempts < 0 || attempts < maxAttempts) && !l.hasEventOccurred());
    if (l.hasEventOccurred()) {
        throw new JMSException("" + l.occurredEvent());
    }
    throw ee;
}
Also used : InvalidClientIDException(jakarta.jms.InvalidClientIDException) JMSSecurityException(jakarta.jms.JMSSecurityException) XAConnection(jakarta.jms.XAConnection) Connection(jakarta.jms.Connection) XAConnectionFactory(jakarta.jms.XAConnectionFactory) JMSException(jakarta.jms.JMSException) JMSException(jakarta.jms.JMSException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) InvalidClientIDException(jakarta.jms.InvalidClientIDException) JMSSecurityException(jakarta.jms.JMSSecurityException)

Example 5 with XAConnectionFactory

use of jakarta.jms.XAConnectionFactory in project openmq by eclipse-ee4j.

the class JMSBridge method list.

public ArrayList<BridgeCmdSharedReplyData> list(String linkName, ResourceBundle rb, boolean all) throws Exception {
    ArrayList<BridgeCmdSharedReplyData> replys = new ArrayList<>();
    BridgeCmdSharedReplyData reply = new BridgeCmdSharedReplyData(5, 3, "-");
    String[] oneRow = new String[5];
    oneRow[0] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_LINK_NAME);
    oneRow[1] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_LINK_STATE);
    oneRow[2] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_SOURCE);
    oneRow[3] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_TARGET);
    oneRow[4] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_TRANSACTED);
    reply.addTitle(oneRow);
    Link l = null;
    if (linkName != null) {
        l = _links.get(linkName);
        if (l == null || !l.isEnabled()) {
            throw new IllegalArgumentException(_jbr.getKString(_jbr.X_LINK_NOT_FOUND, linkName, _name));
        }
        oneRow[0] = l.getName();
        oneRow[1] = l.getState().toString();
        oneRow[2] = l.getSourceString();
        oneRow[3] = l.getTargetString();
        oneRow[4] = String.valueOf(l.isTransacted());
        reply.add(oneRow);
        replys.add(reply);
        if (!all) {
            return replys;
        }
        if (l.isTransacted()) {
            BridgeCmdSharedReplyData rep = new BridgeCmdSharedReplyData(1, 3, "-");
            String[] toneRow = new String[1];
            toneRow[0] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_TRANSACTIONS);
            rep.addTitle(toneRow);
            if (_tma != null) {
                try {
                    String[] txns = _tma.getAllTransactions();
                    if (txns != null) {
                        for (int i = 0; i < txns.length; i++) {
                            toneRow[0] = txns[i];
                            rep.add(toneRow);
                        }
                    }
                } catch (Exception e) {
                    _logger.log(Level.WARNING, _jbr.getKString(_jbr.W_FAILED_GET_ALL_TXNS, _name));
                }
            }
            replys.add(rep);
        }
        return replys;
    }
    Link[] ls = null;
    synchronized (_links) {
        ls = _links.values().toArray(new Link[_links.size()]);
    }
    for (int i = 0; i < ls.length; i++) {
        l = ls[i];
        if (!l.isEnabled()) {
            continue;
        }
        oneRow[0] = l.getName();
        oneRow[1] = l.getState().toString();
        oneRow[2] = l.getSourceString();
        oneRow[3] = l.getTargetString();
        oneRow[4] = String.valueOf(l.isTransacted());
        reply.add(oneRow);
    }
    replys.add(reply);
    if (!all) {
        return replys;
    }
    BridgeCmdSharedReplyData pcfreply = new BridgeCmdSharedReplyData(7, 3, "-", BridgeCmdSharedReplyData.CENTER);
    pcfreply.setTitleAlign(BridgeCmdSharedReplyData.CENTER);
    String[] poneRow = new String[7];
    poneRow[0] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_POOLED);
    poneRow[1] = "XA";
    poneRow[2] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_NUM_INUSE);
    poneRow[3] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_NUM_IDLE);
    poneRow[4] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_IDLE);
    poneRow[5] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_MAX);
    poneRow[6] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_RETRY);
    pcfreply.addTitle(poneRow);
    poneRow[0] = "ConnectionFactory";
    poneRow[1] = "";
    poneRow[2] = "";
    poneRow[3] = "";
    poneRow[4] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_TIMEOUT);
    poneRow[5] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_RETRIES);
    poneRow[6] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_INTERVAL);
    pcfreply.addTitle(poneRow);
    BridgeCmdSharedReplyData scfreply = new BridgeCmdSharedReplyData(6, 3, "-", BridgeCmdSharedReplyData.CENTER);
    scfreply.setTitleAlign(BridgeCmdSharedReplyData.CENTER);
    String[] soneRow = new String[6];
    soneRow[0] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_SHARED);
    soneRow[1] = "XA";
    soneRow[2] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_REF);
    soneRow[3] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_IDLE);
    soneRow[4] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_MAX);
    soneRow[5] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_RETRY);
    scfreply.addTitle(soneRow);
    soneRow[0] = "ConnectionFactory";
    soneRow[1] = "";
    soneRow[2] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_COUNT);
    soneRow[3] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_TIMEOUT);
    soneRow[4] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_RETRIES);
    soneRow[5] = rb.getString(BridgeCmdSharedResources.I_BGMGR_TITLE_INTERVAL);
    scfreply.addTitle(soneRow);
    Object[] spcfs = null;
    synchronized (_spcfs) {
        spcfs = _spcfs.values().toArray();
    }
    PooledConnectionFactory pcf = null;
    SharedConnectionFactory scf = null;
    for (int i = 0; i < spcfs.length; i++) {
        if (spcfs[i] instanceof PooledConnectionFactory) {
            pcf = (PooledConnectionFactory) spcfs[i];
            poneRow[0] = ((Refable) pcf.getCF()).getRef();
            poneRow[1] = String.valueOf(pcf.getCF() instanceof XAConnectionFactory);
            poneRow[2] = String.valueOf(pcf.getNumInUseConns());
            poneRow[3] = String.valueOf(pcf.getNumIdleConns());
            poneRow[4] = String.valueOf(pcf.getIdleTimeout());
            poneRow[5] = String.valueOf(pcf.getMaxRetries());
            poneRow[6] = String.valueOf(pcf.getRetryInterval());
            pcfreply.add(poneRow);
        } else if (spcfs[i] instanceof SharedConnectionFactory) {
            scf = (SharedConnectionFactory) spcfs[i];
            soneRow[0] = ((Refable) scf.getCF()).getRef();
            soneRow[1] = String.valueOf(scf.getCF() instanceof XAConnectionFactory);
            soneRow[2] = String.valueOf(scf.getRefCount());
            soneRow[3] = String.valueOf(scf.getIdleTimeout());
            soneRow[4] = String.valueOf(scf.getMaxRetries());
            soneRow[5] = String.valueOf(scf.getRetryInterval());
            scfreply.add(soneRow);
        }
    }
    replys.add(pcfreply);
    replys.add(scfreply);
    return replys;
}
Also used : ArrayList(java.util.ArrayList) JMSException(jakarta.jms.JMSException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) InvalidClientIDException(jakarta.jms.InvalidClientIDException) JMSSecurityException(jakarta.jms.JMSSecurityException) XAConnectionFactory(jakarta.jms.XAConnectionFactory) BridgeCmdSharedReplyData(com.sun.messaging.bridge.api.BridgeCmdSharedReplyData)

Aggregations

XAConnectionFactory (jakarta.jms.XAConnectionFactory)8 JMSException (jakarta.jms.JMSException)4 InvalidClientIDException (jakarta.jms.InvalidClientIDException)3 JMSSecurityException (jakarta.jms.JMSSecurityException)3 XAConnection (jakarta.jms.XAConnection)3 IOException (java.io.IOException)3 Properties (java.util.Properties)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 TransactionManagerAdapter (com.sun.messaging.bridge.service.jms.tx.TransactionManagerAdapter)2 Connection (jakarta.jms.Connection)2 ConnectionFactory (jakarta.jms.ConnectionFactory)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 BridgeCmdSharedReplyData (com.sun.messaging.bridge.api.BridgeCmdSharedReplyData)1 LogSimpleFormatter (com.sun.messaging.bridge.api.LogSimpleFormatter)1 XASession (jakarta.jms.XASession)1 Transaction (jakarta.transaction.Transaction)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 FileHandler (java.util.logging.FileHandler)1