/**
   * the notification will be applied sync if the method invocation is done in with a {@link
   * IClientSession} in the {@link RunContext}. The sync execution is due to piggyback notifications
   * expected to be applied after a successful backendcall. In case no {@link IClientSession} is in
   * the current {@link RunContext} the notification is applied async.
   *
   * @param notification
   */
  public void dispatch(final Serializable notification) {
    if (IClientSession.CURRENT.get() != null) {
      // dispatch sync for piggyback notifications
      dispatchSync(notification);
    } else {
      // dispatch async
      IFuture<Void> future =
          Jobs.schedule(
              new IRunnable() {

                @Override
                public void run() throws Exception {
                  dispatchSync(notification);
                }
              },
              Jobs.newInput()
                  .withRunContext(ClientRunContexts.copyCurrent())
                  .withName("Dispatching client notification"));

      addPendingNotification(future);

      future.whenDone(new P_NotificationFutureCallback(future), null);
    }
  }
  /**
   * Dispatch notifications within the context of a session.<br>
   * Dispatching is always done asynchronously to ensure that it is not handled within a model
   * thread.
   *
   * @param session the session describes the runcontext in which the notification should be
   *     processed.
   * @param notification the notification to process.
   */
  public void dispatch(IClientSession session, final Serializable notification) {
    ISession currentSession = ISession.CURRENT.get();
    // sync dispatch if session is equal
    if (session == currentSession) {
      dispatchSync(notification);
    } else {
      IFuture<Void> future =
          Jobs.schedule(
              new IRunnable() {

                @Override
                public void run() throws Exception {
                  dispatchSync(notification);
                }
              },
              Jobs.newInput()
                  .withRunContext(ClientRunContexts.empty().withSession(session, true))
                  .withName("Dispatching client notification"));

      addPendingNotification(future);

      future.whenDone(new P_NotificationFutureCallback(future), null);
    }
  }