@Override
  public void serverSend(
      final Receiver receiver,
      final Delivery delivery,
      String address,
      int messageFormat,
      ByteBuf messageEncoded)
      throws Exception {
    EncodedMessage encodedMessage =
        new EncodedMessage(
            messageFormat,
            messageEncoded.array(),
            messageEncoded.arrayOffset(),
            messageEncoded.writerIndex());

    ServerMessage message = manager.getConverter().inbound(encodedMessage);
    // use the address on the receiver if not null, if null let's hope it was set correctly on the
    // message
    if (address != null) {
      message.setAddress(new SimpleString(address));
    }

    recoverContext();

    try {
      serverSession.send(message, false);

      manager
          .getServer()
          .getStorageManager()
          .afterCompleteOperations(
              new IOCallback() {
                @Override
                public void done() {
                  synchronized (connection.getLock()) {
                    delivery.settle();
                    connection.flush();
                  }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                  synchronized (connection.getLock()) {
                    receiver.setCondition(
                        new ErrorCondition(
                            AmqpError.ILLEGAL_STATE, errorCode + ":" + errorMessage));
                    connection.flush();
                  }
                }
              });
    } finally {
      resetContext();
    }
  }
  @Override
  public void init(AMQPSessionContext protonSession, SASLResult saslResult) throws Exception {

    this.protonSession = protonSession;

    String name = UUIDGenerator.getInstance().generateStringUUID();

    String user = null;
    String passcode = null;
    if (saslResult != null) {
      user = saslResult.getUser();
      if (saslResult instanceof PlainSASLResult) {
        passcode = ((PlainSASLResult) saslResult).getPassword();
      }
    }

    serverSession =
        manager
            .getServer()
            .createSession(
                name,
                user,
                passcode,
                ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE,
                protonSPI.getProtonConnectionDelegate(), // RemotingConnection remotingConnection,
                false, // boolean autoCommitSends
                false, // boolean autoCommitAcks,
                false, // boolean preAcknowledge,
                true, // boolean xa,
                (String) null,
                this,
                null,
                true);
  }
 private void recoverContext() {
   manager.getServer().getStorageManager().setContext(serverSession.getSessionContext());
 }
 private void resetContext() {
   manager.getServer().getStorageManager().setContext(null);
 }
 @Override
 public ProtonJMessage encodeMessage(Object message, int deliveryCount) throws Exception {
   return (ProtonJMessage) manager.getConverter().outbound((ServerMessage) message, deliveryCount);
 }