@Test
  public void testSessionClosedOnRemotingConnectionFailure() throws Exception {
    ClientSession session = addClientSession(sf.createSession());

    session.createQueue("fooaddress", "fooqueue");

    ClientProducer prod = session.createProducer("fooaddress");

    ClientConsumer cons = session.createConsumer("fooqueue");

    session.start();

    prod.send(session.createMessage(false));

    Assert.assertNotNull(cons.receive());

    // Now fail the underlying connection

    RemotingConnection connection = ((ClientSessionInternal) session).getConnection();

    connection.fail(new HornetQNotConnectedException());

    Assert.assertTrue(session.isClosed());

    Assert.assertTrue(prod.isClosed());

    Assert.assertTrue(cons.isClosed());

    // Now try and use the producer

    try {
      prod.send(session.createMessage(false));

      Assert.fail("Should throw exception");
    } catch (HornetQObjectClosedException oce) {
      // ok
    } catch (HornetQException e) {
      fail("Invalid Exception type:" + e.getType());
    }

    try {
      cons.receive();

      Assert.fail("Should throw exception");
    } catch (HornetQObjectClosedException oce) {
      // ok
    } catch (HornetQException e) {
      fail("Invalid Exception type:" + e.getType());
    }

    session.close();
  }
  public void write(
      final long position,
      final long size,
      final ByteBuffer directByteBuffer,
      final AIOCallback aioCallback) {
    if (aioCallback == null) {
      throw new NullPointerException("Null Callback");
    }

    checkOpened();
    if (poller == null) {
      startPoller();
    }

    pendingWrites.countUp();

    if (writeExecutor != null) {
      maxIOSemaphore.acquireUninterruptibly();

      writeExecutor.execute(
          new Runnable() {
            public void run() {
              long sequence = nextWritingSequence.getAndIncrement();

              try {
                write(handler, sequence, position, size, directByteBuffer, aioCallback);
              } catch (HornetQException e) {
                callbackError(
                    aioCallback, sequence, directByteBuffer, e.getType().getCode(), e.getMessage());
              } catch (RuntimeException e) {
                callbackError(
                    aioCallback,
                    sequence,
                    directByteBuffer,
                    HornetQExceptionType.INTERNAL_ERROR.getCode(),
                    e.getMessage());
              }
            }
          });
    } else {
      maxIOSemaphore.acquireUninterruptibly();

      long sequence = nextWritingSequence.getAndIncrement();

      try {
        write(handler, sequence, position, size, directByteBuffer, aioCallback);
      } catch (HornetQException e) {
        callbackError(
            aioCallback, sequence, directByteBuffer, e.getType().getCode(), e.getMessage());
      } catch (RuntimeException e) {
        callbackError(
            aioCallback,
            sequence,
            directByteBuffer,
            HornetQExceptionType.INTERNAL_ERROR.getCode(),
            e.getMessage());
      }
    }
  }
 public void testCreateConsumerNoQ() throws Exception {
   try {
     clientSession.createConsumer(queueName);
     Assert.fail("should throw exception");
   } catch (NonExistentQueueException neqe) {
     // ok
   } catch (HornetQException e) {
     fail("Invalid Exception type:" + e.getType());
   }
 }
 public void testCreateConsumerWithInvalidFilter() throws Exception {
   clientSession.createQueue(queueName, queueName, false);
   try {
     clientSession.createConsumer(queueName, "this is not valid filter");
     Assert.fail("should throw exception");
   } catch (InvalidFilterExpressionException ifee) {
     // ok
   } catch (HornetQException e) {
     fail("Invalid Exception type:" + e.getType());
   }
 }
Exemplo n.º 5
0
 /* (non-Javadoc)
  * @see org.hornetq.core.remoting.FailureListener#connectionFailed(org.hornetq.api.core.HornetQException, boolean)
  */
 public void connectionFailed(HornetQException exception, boolean failedOver) {
   if (exception.getType() == HornetQExceptionType.DISCONNECTED) {
     HornetQJMSLogger.LOGGER.warn("being disconnected for server shutdown", exception);
   } else {
     HornetQJMSLogger.LOGGER.warn(
         "Notified of connection failure in xa discovery, we will retry on the next recovery",
         exception);
   }
   internalStop();
   HornetQRecoveryRegistry.getInstance().failedDiscovery(this);
 }
 public void writeInternal(long positionToWrite, long size, ByteBuffer bytes)
     throws HornetQException {
   try {
     writeInternal(handler, positionToWrite, size, bytes);
   } catch (HornetQException e) {
     fireExceptionListener(e.getType().getCode(), e.getMessage());
     throw e;
   }
   if (bufferCallback != null) {
     bufferCallback.bufferDone(bytes);
   }
 }
  public void open(final String fileName, final int maxIO) throws HornetQException {
    writeLock.lock();

    try {
      if (opened) {
        throw new IllegalStateException("AsynchronousFile is already opened");
      }

      this.maxIO = maxIO;
      maxIOSemaphore = new Semaphore(this.maxIO);

      this.fileName = fileName;

      try {
        handler = AsynchronousFileImpl.init(fileName, this.maxIO, HornetQJournalLogger.LOGGER);
      } catch (HornetQException e) {
        HornetQException ex = null;
        if (e.getType() == HornetQExceptionType.NATIVE_ERROR_CANT_INITIALIZE_AIO) {
          ex =
              new HornetQException(
                  e.getType(),
                  "Can't initialize AIO. Currently AIO in use = "
                      + AsynchronousFileImpl.totalMaxIO.get()
                      + ", trying to allocate more "
                      + maxIO,
                  e);
        } else {
          ex = e;
        }
        throw ex;
      }
      opened = true;
      AsynchronousFileImpl.addMax(this.maxIO);
      nextWritingSequence.set(0);
      nextReadSequence = 0;
    } finally {
      writeLock.unlock();
    }
  }
Exemplo n.º 8
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());
   }
 }
Exemplo n.º 9
0
    @Override
    public void connectionFailed(final HornetQException me, boolean failedOver) {
      if (me.getType() == HornetQExceptionType.DISCONNECTED) {
        // Backup has shut down - no need to log a stack trace
        HornetQServerLogger.LOGGER.replicationStopOnBackupShutdown();
      } else {
        HornetQServerLogger.LOGGER.replicationStopOnBackupFail(me);
      }

      try {
        stop();
      } catch (Exception e) {
        HornetQServerLogger.LOGGER.errorStoppingReplication(e);
      }
    }
  public void testSSLWithIncorrectKeyStorePassword() throws Exception {
    tc.getParams().put(TransportConstants.SSL_ENABLED_PROP_NAME, true);
    tc.getParams()
        .put(TransportConstants.KEYSTORE_PATH_PROP_NAME, TransportConstants.DEFAULT_KEYSTORE_PATH);
    tc.getParams().put(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME, "invalid password");

    ServerLocator locator = addServerLocator(HornetQClient.createServerLocatorWithoutHA(tc));
    try {
      ClientSessionFactory sf = createSessionFactory(locator);
      Assert.fail();
    } catch (HornetQNotConnectedException se) {
      // ok
    } catch (HornetQException e) {
      fail("Invalid Exception type:" + e.getType());
    }
  }
  // see https://jira.jboss.org/jira/browse/HORNETQ-234
  public void testPlainConnectionToSSLEndpoint() throws Exception {
    tc.getParams().put(TransportConstants.SSL_ENABLED_PROP_NAME, false);

    ServerLocator locator = addServerLocator(HornetQClient.createServerLocatorWithoutHA(tc));
    locator.setCallTimeout(2000);
    try {
      createSessionFactory(locator);
      fail("expecting exception");
    } catch (HornetQNotConnectedException se) {
      // ok
    } catch (HornetQConnectionTimedOutException ctoe) {
      // ok
    } catch (HornetQException e) {
      fail("Invalid Exception type:" + e.getType());
    }
  }
Exemplo n.º 12
0
 private void decideOnAction() {
   // we may get called via multiple paths so need to guard
   synchronized (decisionGuard) {
     if (signal == BACKUP_ACTIVATION.FAIL_OVER) {
       return;
     }
     if (!isLiveDown()) {
       try {
         // no point in repeating all the reconnection logic
         sessionFactory.connect(RECONNECT_ATTEMPTS, false);
         return;
       } catch (HornetQException e) {
         if (e.getType() != HornetQExceptionType.NOT_CONNECTED)
           HornetQServerLogger.LOGGER.errorReConnecting(e);
       }
     }
     // live is assumed to be down, backup fails-over
     signal = BACKUP_ACTIVATION.FAIL_OVER;
   }
   latch.countDown();
 }
Exemplo n.º 13
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;
    }
  }