Пример #1
0
 public void disconnect() {
   if (factory != null) {
     factory.causeExit();
     factory.cleanup();
     factory = null;
   }
 }
Пример #2
0
  public ClientSessionFactory createSessionFactory(String nodeID) throws Exception {
    TopologyMember topologyMember = topology.getMember(nodeID);

    if (HornetQLogger.LOGGER.isTraceEnabled()) {
      HornetQLogger.LOGGER.trace(
          "Creating connection factory towards "
              + nodeID
              + " = "
              + topologyMember
              + ", topology="
              + topology.describe());
    }

    if (topologyMember == null) {
      return null;
    } else if (topologyMember.getA() != null) {
      ClientSessionFactoryInternal factory =
          (ClientSessionFactoryInternal) createSessionFactory(topologyMember.getA());
      if (topologyMember.getB() != null) {
        factory.setBackupConnector(topologyMember.getA(), topologyMember.getB());
      }
      return factory;
    } else if (topologyMember.getA() == null && topologyMember.getB() != null) {
      // This shouldn't happen, however I wanted this to consider all possible cases
      ClientSessionFactoryInternal factory =
          (ClientSessionFactoryInternal) createSessionFactory(topologyMember.getB());
      return factory;
    } else {
      // it shouldn't happen
      return null;
    }
  }
Пример #3
0
    private synchronized void createConnectors() {
      if (connectors != null) {
        for (Connector conn : connectors) {
          if (conn != null) {
            conn.disconnect();
          }
        }
      }
      connectors = new ArrayList<Connector>();
      for (TransportConfiguration initialConnector : initialConnectors) {
        ClientSessionFactoryInternal factory =
            new ClientSessionFactoryImpl(
                ServerLocatorImpl.this,
                initialConnector,
                callTimeout,
                callFailoverTimeout,
                clientFailureCheckPeriod,
                connectionTTL,
                retryInterval,
                retryIntervalMultiplier,
                maxRetryInterval,
                reconnectAttempts,
                threadPool,
                scheduledThreadPool,
                interceptors);

        factory.disableFinalizeCheck();

        connectors.add(new Connector(initialConnector, factory));
      }
    }
Пример #4
0
      public ClientSessionFactory tryConnect() throws HornetQException {
        if (HornetQLogger.LOGGER.isDebugEnabled()) {
          HornetQLogger.LOGGER.debug(this + "::Trying to connect to " + factory);
        }
        try {
          ClientSessionFactoryInternal factoryToUse = factory;
          if (factoryToUse != null) {
            addToConnecting(factoryToUse);

            try {
              factoryToUse.connect(1, false);
            } finally {
              removeFromConnecting(factoryToUse);
            }
          }
          return factoryToUse;
        } catch (HornetQException e) {
          HornetQLogger.LOGGER.debug(
              this + "::Exception on establish connector initial connection", e);
          return null;
        }
      }
Пример #5
0
  private void addFactory(ClientSessionFactoryInternal factory) {
    if (factory == null) {
      return;
    }

    if (isClosed()) {
      factory.close();
      return;
    }

    TransportConfiguration backup = null;

    if (ha) {
      backup = topology.getBackupForConnector(factory.getConnectorConfiguration());
    }

    factory.setBackupConnector(factory.getConnectorConfiguration(), backup);

    synchronized (factories) {
      factories.add(factory);
    }
  }
Пример #6
0
  public ClientSessionFactory createSessionFactory(
      final TransportConfiguration transportConfiguration) throws Exception {
    assertOpen();

    initialise();

    ClientSessionFactoryInternal factory =
        new ClientSessionFactoryImpl(
            this,
            transportConfiguration,
            callTimeout,
            callFailoverTimeout,
            clientFailureCheckPeriod,
            connectionTTL,
            retryInterval,
            retryIntervalMultiplier,
            maxRetryInterval,
            reconnectAttempts,
            threadPool,
            scheduledThreadPool,
            interceptors);

    addToConnecting(factory);
    try {
      try {
        factory.connect(reconnectAttempts, failoverOnInitialConnection);
      } catch (HornetQException e1) {
        // we need to make sure is closed just for garbage collection
        factory.close();
        throw e1;
      }
      addFactory(factory);
      return factory;
    } finally {
      removeFromConnecting(factory);
    }
  }
Пример #7
0
  public void notifyNodeUp(
      long uniqueEventID,
      final String nodeID,
      final Pair<TransportConfiguration, TransportConfiguration> connectorPair,
      final boolean last) {
    if (!ha) {
      // there's no topology
      return;
    }

    if (HornetQLogger.LOGGER.isDebugEnabled()) {
      HornetQLogger.LOGGER.debug(
          "NodeUp " + this + "::nodeID=" + nodeID + ", connectorPair=" + connectorPair,
          new Exception("trace"));
    }

    TopologyMember member = new TopologyMember(connectorPair.getA(), connectorPair.getB());

    topology.updateMember(uniqueEventID, nodeID, member);

    TopologyMember actMember = topology.getMember(nodeID);

    if (actMember != null
        && actMember.getConnector().getA() != null
        && actMember.getConnector().getB() != null) {
      HashSet<ClientSessionFactory> clonedFactories = new HashSet<ClientSessionFactory>();
      synchronized (factories) {
        clonedFactories.addAll(factories);
      }

      for (ClientSessionFactory factory : clonedFactories) {
        ((ClientSessionFactoryInternal) factory)
            .setBackupConnector(actMember.getConnector().getA(), actMember.getConnector().getB());
      }
    }

    updateArraysAndPairs();

    if (last) {
      synchronized (this) {
        receivedTopology = true;
        // Notify if waiting on getting topology
        notifyAll();
      }
    }
  }
Пример #8
0
  public ClientSessionFactory createSessionFactory() throws HornetQException {
    assertOpen();

    initialise();

    if (initialConnectors == null && discoveryGroup != null) {
      // Wait for an initial broadcast to give us at least one node in the cluster
      long timeout =
          clusterConnection ? 0 : discoveryGroupConfiguration.getDiscoveryInitialWaitTimeout();
      boolean ok = discoveryGroup.waitForBroadcast(timeout);

      if (!ok) {
        throw HornetQMessageBundle.BUNDLE.connectionTimedOutInInitialBroadcast();
      }
    }

    ClientSessionFactoryInternal factory = null;

    synchronized (this) {
      boolean retry;
      int attempts = 0;
      do {
        retry = false;

        TransportConfiguration tc = selectConnector();
        if (tc == null) {
          throw HornetQMessageBundle.BUNDLE.noTCForSessionFactory();
        }

        // try each factory in the list until we find one which works

        try {
          factory =
              new ClientSessionFactoryImpl(
                  this,
                  tc,
                  callTimeout,
                  callFailoverTimeout,
                  clientFailureCheckPeriod,
                  connectionTTL,
                  retryInterval,
                  retryIntervalMultiplier,
                  maxRetryInterval,
                  reconnectAttempts,
                  threadPool,
                  scheduledThreadPool,
                  interceptors);
          try {
            addToConnecting(factory);
            factory.connect(initialConnectAttempts, failoverOnInitialConnection);
          } finally {
            removeFromConnecting(factory);
          }
        } catch (HornetQException e) {
          factory.close();
          factory = null;
          if (e.getType() == HornetQExceptionType.NOT_CONNECTED) {
            attempts++;

            if (topologyArray != null && attempts == topologyArray.length) {
              throw HornetQMessageBundle.BUNDLE.cannotConnectToServers();
            }
            if (topologyArray == null
                && initialConnectors != null
                && attempts == initialConnectors.length) {
              throw HornetQMessageBundle.BUNDLE.cannotConnectToServers();
            }
            retry = true;
          } else {
            throw e;
          }
        }
      } while (retry);

      if (ha || clusterConnection) {
        final long timeout = System.currentTimeMillis() + 30000;
        while (!isClosed() && !receivedTopology && timeout > System.currentTimeMillis()) {
          // Now wait for the topology

          try {
            wait(1000);
          } catch (InterruptedException ignore) {
          }
        }

        if (System.currentTimeMillis() > timeout && !receivedTopology) {
          throw HornetQMessageBundle.BUNDLE.connectionTimedOutOnReceiveTopology(discoveryGroup);
        }
      }

      addFactory(factory);

      return factory;
    }
  }
Пример #9
0
  private void doClose(final boolean sendClose) {
    if (state == STATE.CLOSED) {
      if (HornetQLogger.LOGGER.isDebugEnabled()) {
        HornetQLogger.LOGGER.debug(this + " is already closed when calling closed");
      }
      return;
    }

    state = STATE.CLOSING;

    if (discoveryGroup != null) {
      synchronized (this) {
        try {
          discoveryGroup.stop();
        } catch (Exception e) {
          HornetQLogger.LOGGER.failedToStopDiscovery(e);
        }
      }
    } else {
      staticConnector.disconnect();
    }

    synchronized (connectingFactories) {
      for (ClientSessionFactoryInternal csf : connectingFactories) {
        csf.close();
      }
      connectingFactories.clear();
    }

    Set<ClientSessionFactoryInternal> clonedFactory;
    synchronized (factories) {
      clonedFactory = new HashSet<ClientSessionFactoryInternal>(factories);

      factories.clear();
    }

    for (ClientSessionFactory factory : clonedFactory) {
      if (sendClose) {
        factory.close();
      } else {
        factory.cleanup();
      }
    }

    if (shutdownPool) {
      if (threadPool != null) {
        threadPool.shutdown();

        try {
          if (!threadPool.awaitTermination(10000, TimeUnit.MILLISECONDS)) {
            HornetQLogger.LOGGER.timedOutWaitingForTermination();
          }
        } catch (InterruptedException ignore) {
        }
      }

      if (scheduledThreadPool != null) {
        scheduledThreadPool.shutdown();

        try {
          if (!scheduledThreadPool.awaitTermination(10000, TimeUnit.MILLISECONDS)) {
            HornetQLogger.LOGGER.timedOutWaitingForScheduledPoolTermination();
          }
        } catch (InterruptedException ignore) {
        }
      }
    }
    readOnly = false;

    state = STATE.CLOSED;
  }