code: public Connection wrapConnectionWithBufferWriter(final Connection connectionToWrap) {
          return new Connection() {
              final Connection wrappedConnection = connectionToWrap;
  
              @Override
              public void send(Event event) throws ConnectionException {
                  try {
                      // buffer before we attempt to send
                      buffer.add(event);
                  } catch (Exception e) {
                      logger.error("Exception occurred while attempting to add Event to buffer: ", e);
                  }
  
                  wrappedConnection.send(event);
              }
  
              @Override
              public void addEventSendCallback(EventSendCallback eventSendCallback) {
                  wrappedConnection.addEventSendCallback(eventSendCallback);
              }
  
              @Override
              public void close() throws IOException {
                  wrappedConnection.close();
              }
          };
      }
docs: Wrap a connection so that {@link Event}s are buffered before being passed on to
  the underlying connection.
  
  This is important to ensure buffering happens synchronously with {@link Event} creation,
  before they are passed on to the (optional) asynchronous connection so that Events will
  be stored even if the application is about to exit due to a crash.
  
  @param connectionToWrap {@link Connection} to wrap with buffering logic.
  @return Connection that will write {@link Event}s to a buffer before passing along to
  a wrapped {@link Connection}.