Example #1
0
  /**
   * While the service isn't stopped this runs a continuous loop checking for new events in the
   * queue.
   */
  public void run() {
    DefaultMuleEvent event = null;
    QueueSession queueSession = muleContext.getQueueManager().getQueueSession();

    while (!stopped.get()) {
      try {
        // Wait if the service is paused
        paused.whenFalse(null);

        // If we're doing a draining stop, read all events from the queue
        // before stopping
        if (stopping.get()) {
          if (queueSession == null || getQueueSize() <= 0) {
            stopping.set(false);
            break;
          }
        }

        event = (DefaultMuleEvent) dequeue();
        if (event != null) {
          if (stats.isEnabled()) {
            stats.decQueuedEvent();
          }

          if (logger.isDebugEnabled()) {
            logger.debug(
                "Service: " + name + " dequeued event on: " + event.getEndpoint().getEndpointURI());
          }
          workManager.scheduleWork(
              new ComponentStageWorker(event), WorkManager.INDEFINITE, null, this);
        }
      } catch (Exception e) {
        if (isStopped() || isStopping()) {
          break;
        }

        if (e instanceof InterruptedException) {
          stopping.set(false);
          break;
        } else if (e instanceof NoSuchElementException) {
          handleException(
              new ServiceException(
                  CoreMessages.proxyPoolTimedOut(),
                  (event == null ? null : event.getMessage()),
                  this,
                  e));
        } else if (e instanceof MuleException) {
          handleException(e);
        } else if (e instanceof WorkException) {
          handleException(
              new ServiceException(
                  CoreMessages.eventProcessingFailedFor(name),
                  (event == null ? null : event.getMessage()),
                  this,
                  e));
        } else {
          handleException(
              new ServiceException(
                  CoreMessages.failedToGetPooledObject(),
                  (event == null ? null : event.getMessage()),
                  this,
                  e));
        }
      } finally {
        stopping.set(false);
      }
    }
  }
Example #2
0
  /**
   * While the service isn't stopped this runs a continuous loop checking for new events in the
   * queue.
   */
  public void run() {
    DefaultMuleEvent event = null;
    QueueSession queueSession = muleContext.getQueueManager().getQueueSession();

    while (!stopped.get()) {
      try {
        // Wait if the service is paused
        if (paused.get()) {
          paused.whenFalse(null);

          // If service is resumed as part of stopping
          if (stopping.get()) {
            if (!queueProfile.isPersistent() && (queueSession != null && getQueueSize() > 0)) {
              // Any messages in a non-persistent queue went paused service is stopped are lost
              logger.warn(
                  CoreMessages.stopPausedSedaServiceNonPeristentQueueMessageLoss(
                      getQueueSize(), this));
            }
            stopping.set(false);
            break;
          }
        }

        // If we're doing a draining stop, read all events from the queue
        // before stopping
        if (stopping.get()) {
          if (queueProfile.isPersistent() || (queueSession == null || getQueueSize() <= 0)) {
            stopping.set(false);
            break;
          }
        }

        if (stats.isEnabled()) {
          synchronized (queueStatsGuard) {
            event = (DefaultMuleEvent) dequeue();
            if (event != null) {
              stats.decQueuedEvent();
            }
          }
        } else {
          // just dequeue without any hit for synchronization
          event = (DefaultMuleEvent) dequeue();
        }

        if (event != null) {
          if (logger.isDebugEnabled()) {
            logger.debug(
                MessageFormat.format(
                    "Service: {0} dequeued event on: {1}",
                    name, event.getEndpoint().getEndpointURI()));
          }
          workManager.scheduleWork(
              new ComponentStageWorker(event), WorkManager.INDEFINITE, null, this);
        }
      } catch (Exception e) {
        if (e instanceof InterruptedException) {
          stopping.set(false);
          break;
        }
        if (e instanceof MuleException) {
          handleException(e);
        } else {
          handleException(
              new ServiceException(
                  CoreMessages.eventProcessingFailedFor(name),
                  (event == null ? null : event.getMessage()),
                  this,
                  e));
        }
      }
    }
  }