Exemplo n.º 1
0
  /* This is called only when the bridge is activated */
  protected void connect() {
    if (stopping) return;

    synchronized (connectionGuard) {
      if (!keepConnecting) return;

      logger.debug(
          "Connecting  "
              + this
              + " to its destination ["
              + nodeUUID.toString()
              + "], csf="
              + this.csf);

      retryCount++;

      try {
        if (csf == null || csf.isClosed()) {
          if (stopping) return;
          csf = createSessionFactory();
          if (csf == null) {
            // Retrying. This probably means the node is not available (for the cluster connection
            // case)
            scheduleRetryConnect();
            return;
          }
          // Session is pre-acknowledge
          session =
              (ClientSessionInternal) csf.createSession(user, password, false, true, true, true, 1);
          sessionConsumer =
              (ClientSessionInternal) csf.createSession(user, password, false, true, true, true, 1);
        }

        if (forwardingAddress != null) {
          ClientSession.AddressQuery query = null;

          try {
            query = session.addressQuery(forwardingAddress);
          } catch (Throwable e) {
            ActiveMQServerLogger.LOGGER.errorQueryingBridge(e, name);
            // This was an issue during startup, we will not count this retry
            retryCount--;

            scheduleRetryConnectFixedTimeout(100);
            return;
          }

          if (forwardingAddress.startsWith(BridgeImpl.JMS_QUEUE_ADDRESS_PREFIX)
              || forwardingAddress.startsWith(BridgeImpl.JMS_TOPIC_ADDRESS_PREFIX)) {
            if (!query.isExists()) {
              ActiveMQServerLogger.LOGGER.errorQueryingBridge(forwardingAddress, retryCount);
              scheduleRetryConnect();
              return;
            }
          } else {
            if (!query.isExists()) {
              ActiveMQServerLogger.LOGGER.bridgeNoBindings(
                  getName(), getForwardingAddress(), getForwardingAddress());
            }
          }
        }

        producer = session.createProducer();
        session.addFailureListener(BridgeImpl.this);

        session.setSendAcknowledgementHandler(BridgeImpl.this);

        afterConnect();

        active = true;

        queue.addConsumer(BridgeImpl.this);
        queue.deliverAsync();

        ActiveMQServerLogger.LOGGER.bridgeConnected(this);

        // We only do this on plain core bridges
        if (isPlainCoreBridge()) {
          serverLocator.addClusterTopologyListener(new TopologyListener());
        }

        keepConnecting = false;
        return;
      } catch (ActiveMQException e) {
        // the session was created while its server was starting, retry it:
        if (e.getType() == ActiveMQExceptionType.SESSION_CREATION_REJECTED) {
          ActiveMQServerLogger.LOGGER.errorStartingBridge(name);

          // We are not going to count this one as a retry
          retryCount--;

          scheduleRetryConnectFixedTimeout(this.retryInterval);
          return;
        } else {
          if (logger.isDebugEnabled()) {
            logger.debug("Bridge " + this + " is unable to connect to destination. Retrying", e);
          }

          scheduleRetryConnect();
        }
      } catch (ActiveMQInterruptedException | InterruptedException e) {
        ActiveMQServerLogger.LOGGER.errorConnectingBridge(e, this);
      } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.errorConnectingBridge(e, this);
        if (csf != null) {
          try {
            csf.close();
            csf = null;
          } catch (Throwable ignored) {
          }
        }
        fail(false);
        scheduleRetryConnect();
      }
    }
  }