Example #1
0
 public void testConnectIntoNonBackup() throws Exception {
   setupServer(false);
   try {
     ClientSessionFactory sf = createSessionFactory(locator);
     manager = new ReplicationManager(sf.getConnection(), factory);
     addHornetQComponent(manager);
     manager.start();
     Assert.fail("Exception was expected");
   } catch (NotConnectedException nce) {
     // ok
   } catch (HornetQException expected) {
     fail("Invalid Exception type:" + expected.getType());
   }
 }
    public ClientSessionFactory connect() throws HornetQException {
      assertOpen();

      initialise();

      ClientSessionFactory csf = null;

      createConnectors();

      try {

        int retryNumber = 0;
        while (csf == null && !isClosed()) {
          retryNumber++;
          for (Connector conn : connectors) {
            if (HornetQLogger.LOGGER.isDebugEnabled()) {
              HornetQLogger.LOGGER.debug(this + "::Submitting connect towards " + conn);
            }

            csf = conn.tryConnect();

            if (csf != null) {
              csf.getConnection()
                  .addFailureListener(
                      new FailureListener() {
                        // Case the node where the cluster connection was connected is gone, we need
                        // to restart the
                        // connection
                        public void connectionFailed(
                            HornetQException exception, boolean failedOver) {
                          if (clusterConnection
                              && exception.getType() == HornetQExceptionType.DISCONNECTED) {
                            try {
                              ServerLocatorImpl.this.start(startExecutor);
                            } catch (Exception e) {
                              // There isn't much to be done if this happens here
                              HornetQLogger.LOGGER.errorStartingLocator(e);
                            }
                          }
                        }

                        @Override
                        public String toString() {
                          return "FailureListener('restarts cluster connections')";
                        }
                      });

              if (HornetQLogger.LOGGER.isDebugEnabled()) {
                HornetQLogger.LOGGER.debug(
                    "Returning "
                        + csf
                        + " after "
                        + retryNumber
                        + " retries on StaticConnector "
                        + ServerLocatorImpl.this);
              }

              return csf;
            }
          }

          if (initialConnectAttempts >= 0 && retryNumber > initialConnectAttempts) {
            break;
          }

          if (!isClosed()) {
            Thread.sleep(retryInterval);
          }
        }

      } catch (RejectedExecutionException e) {
        HornetQLogger.LOGGER.debug("Rejected execution", e);
        throw e;
      } catch (Exception e) {
        HornetQLogger.LOGGER.errorConnectingToNodes(e);
        throw HornetQMessageBundle.BUNDLE.cannotConnectToStaticConnectors(e);
      }

      if (!isClosed()) {
        HornetQLogger.LOGGER.errorConnectingToNodes(e);
        throw HornetQMessageBundle.BUNDLE.cannotConnectToStaticConnectors2();
      }
      return null;
    }