Esempio n. 1
0
  /**
   * Acknowledges a received message.
   *
   * @param notification The received notification message
   * @param acceptRequest An AcceptRequest object used handling Accept messages.
   */
  public void acknowledge(NetNotification notification, AcceptRequest acceptRequest)
      throws Throwable {

    if ((notification != null)
        && (notification.getMessage() != null)
        && (StringUtils.isNotBlank(notification.getMessage().getMessageId()))) {
      NetBrokerMessage brkMsg = notification.getMessage();

      if (notification.getDestinationType() == DestinationType.TOPIC) {
        return;
      }

      String ackDestination = notification.getSubscription();

      NetAcknowledge ackMsg = new NetAcknowledge(ackDestination, brkMsg.getMessageId());
      if (acceptRequest != null) {
        ackMsg.setActionId(acceptRequest.getActionId());
        PendingAcceptRequestsManager.addAcceptRequest(acceptRequest);
      }

      NetAction action = new NetAction(ActionType.ACKNOWLEDGE);
      action.setAcknowledgeMessage(ackMsg);
      NetMessage msg = buildMessage(action);

      getNetHandler().sendMessage(msg);

    } else {
      throw new IllegalArgumentException("Can't acknowledge invalid message.");
    }
  }
Esempio n. 2
0
  protected void notifyListener(NetNotification notification) {
    Map<String, String> headers = notification.getHeaders();
    boolean ackRequired = true;
    if (headers != null) {
      String value = headers.get("ACK_REQUIRED");
      if (value != null) {
        if (value.equalsIgnoreCase("false")) {
          ackRequired = false; // ACK is not required
        }
      }
    }

    Map<String, BrokerAsyncConsumer> subscripions =
        _consumerList.get(
            notification.getDestinationType().equals(DestinationType.TOPIC)
                ? DestinationType.TOPIC
                : DestinationType.QUEUE);

    BrokerAsyncConsumer brokerAsyncConsumer = subscripions.get(notification.getSubscription());

    if (brokerAsyncConsumer == null) {
      log.error(
          String.format(
              "Unexpected notification. DestinationType: %s, Destination: %s, Subscription: %s.",
              notification.getDestinationType(),
              notification.getDestination(),
              notification.getSubscription()));
      return;
    }
    brokerAsyncConsumer.deliver(notification);

    BrokerListener listener = brokerAsyncConsumer.getListener();

    if (!ackRequired) {
      return;
    }

    if ((notification.getDestinationType() != DestinationType.TOPIC) && listener.isAutoAck()) {
      try {
        acknowledge(notification);
      } catch (Throwable t) {
        log.error(
            "Could not acknowledge message, messageId: '{}'",
            notification.getMessage().getMessageId());
        log.error(t.getMessage(), t);
      }
    }
  }