/** Registers/subscribes the application for receiving BPS pushes */
  public void registerBPSPush(
      final String appId, final String BPDSUri, final String contentUri, final String listenUri) {
    NonBlockingReceiverDestination listenDest = null;
    NonBlockingSenderDestination bpsServerDest = null;
    try {
      final MessageListener bpsListener = new DestinationListener("BPS", true);

      final InboundDestinationConfiguration config1 =
          InboundDestinationConfigurationFactory.createBPSConfiguration(
              true, true, false, appId, BPDSUri); // serverUrl

      listenDest =
          DestinationFactory.createNonBlockingReceiverDestination(
              config1, URI.create(listenUri), bpsListener);
      final MessageListener subscriptionListener = new SubscriptionResponseListener("Subscription");
      bpsServerDest =
          DestinationFactory.createNonBlockingSenderDestination(
              _context, URI.create(contentUri), subscriptionListener);

      final ByteMessage subscrMsg =
          BpsSubscriptionMessageBuilder.createByteSubscriptionMessage(
              bpsServerDest, listenDest, "user", "pwd");

      ((HttpMessage) subscrMsg).setQueryParam("field1", "xxxxx");
      ((HttpMessage) subscrMsg).setQueryParam("osversion", "6.0");
      bpsServerDest.send(subscrMsg);
    } catch (final Exception e) {
      alertDialog(e.toString());
    }
  }
  /**
   * Sends cancellable/non-cancellable message and tries to cancel using destination.
   *
   * @param msgString Message string to be sent with the message
   * @param isCancellable True if message can be canceled, otherwise false
   */
  public void testMessageCancellable(final String msgString, final boolean isCancellable) {
    if (_cancellationDest == null) {
      // Check if destination is created
      alertDialog("Please register destination first!");
      return;
    }

    final ByteMessage msg = _cancellationDest.createByteMessage();

    try {
      msg.setStringPayload(msgString);
      msg.setCancellable(isCancellable); // Cancellable
      final int msgId = _cancellationDest.send(msg);
      _cancellationDest.cancel(msgId);
    } catch (final Exception e) {
      alertDialog(e.toString());
    }
  }
  /*
   * Sends non-cancellable and cancellable messages, then tries to cancel them
   * on best effort.
   */
  public void testCancelAllCancellable() {
    try {
      if (_cancellationDest == null) // check if destination is created
      {
        alertDialog("Please register destination first!");
        return;
      }

      final int numMsg = 5; // number of messages to be sent

      final ByteMessage[] messages = new ByteMessage[numMsg];
      final int[] msgIds = new int[numMsg];

      // Create messages
      for (int i = 0; i < messages.length; i++) {
        messages[i] = _cancellationDest.createByteMessage();
        messages[i].setCancellable(true);
        messages[i].setStringPayload("Message " + i + "[cancellable]");
      }

      // Reset two first messages as non-cancellable
      messages[0].setCancellable(false);
      messages[0].setStringPayload("Message 0 [non-cancellable]");
      messages[1].setCancellable(false);
      messages[1].setStringPayload("Message 1 [non-cancellable]");

      // Send messages and read IDs
      for (int i = 0; i < messages.length; i++) {
        msgIds[i] = _cancellationDest.send(messages[i]);
      }

      // Use another method to cancel all
      _cancellationDest.cancelAllCancellable();

      // NOTE: If a message has already been sent, then it
      // can not be canceled.

    } catch (final Exception e) {
      alertDialog(e.toString());
    }
  }
    public void run() {

      try {
        final Object lock = _responseListener.getLock();

        // Send message to retrieve the response
        if (_message == null) {

          ((NonBlockingSenderDestination) _destination).send();
        } else {
          ((NonBlockingSenderDestination) _destination).send(_message);
        }

        // Wait till a response is received by the message listener
        synchronized (lock) {
          try {
            lock.wait(TIMEOUT * 1000);
          } catch (final InterruptedException ex) {
            //
          }
        }

        if (_responseListener.isOnMessage()) {
          final Message responseMsg = _responseListener.getResponse();
          _responseCallback.updateUI(responseMsg);
        } else {
          _responseCallback.timeoutDialog(TIMEOUT);
        }

      } catch (final Exception e) {
        alertDialog(e.toString());
      } finally {
        if (_releaseDestination && _destination != null) {
          _destination.release();
        }
      }
    }