public void close() throws Exception {
    checkOpened();

    writeLock.lock();

    try {

      while (!pendingWrites.await(60000)) {
        HornetQJournalLogger.LOGGER.couldNotGetLock(fileName);
      }

      while (!maxIOSemaphore.tryAcquire(maxIO, 60, TimeUnit.SECONDS)) {
        HornetQJournalLogger.LOGGER.couldNotGetLock(fileName);
      }

      maxIOSemaphore = null;
      if (poller != null) {
        stopPoller();
      }

      if (handler != null) {
        AsynchronousFileImpl.closeInternal(handler);
        AsynchronousFileImpl.addMax(-maxIO);
      }
      opened = false;
      handler = null;
    } finally {
      writeLock.unlock();
    }
  }
  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();
    }
  }