docs: public final void flush() {
          if (eventQueue.isEmpty()) {
              // No events to flush
              logger.trace("No events to flush, returning.");
              return;
          }
  
          // Only one thread should be flushing at a time, try to get the flush lock and if it
          // is already held just return.
          if (!this.flushLock.tryLock()) {
              logger.trace("FlushLock already held, returning.");
              return;
          }
          try {
              while (!this.eventQueue.isEmpty()) {
                  // Clear the buffer for re-use
                  eventBuffer.clear();
  
                  // Pop events off the queue into the buffer
                  while (!this.eventQueue.isEmpty() && eventBuffer.size() < this.batchSize) {
                      final E event = eventQueue.poll();
                      eventBuffer.add(event);
                  }
  
                  if (this.logger.isDebugEnabled()) {
                      this.logger.debug("Flushing " + eventBuffer.size() + " events");
                  }
  
                  // Write events out to batching listener
                  try {
                      this.onApplicationEvents(eventBuffer);
                  } catch (Throwable t) {
                      this.logger.error(
                              "An exception was thrown while trying to flush "
                                      + eventBuffer.size()
                                      + " events",
                              t);
  
                      final StringBuilder failedEvents = new StringBuilder();
                      failedEvents.append(
                              "The following events that were being flushed, some may have been persisted correctly");
  
                      for (final E portalEvent : eventBuffer) {
                          failedEvents.append("\n\t");
                          try {
                              failedEvents.append(portalEvent.toString());
                          } catch (Exception e) {
                              failedEvents
                                      .append("toString failed on a PortalEvent of type '")
                                      .append(portalEvent.getClass())
                                      .append("': ")
                                      .append(e);
                          }
                      }
  
                      this.logger.error(failedEvents.toString(), t);
                  }
              }
  
          } finally {
              // Clear the buffer to avoid memory leaks
              eventBuffer.clear();
  
              this.flushLock.unlock();
          }
      }