* 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();
running.set(true);
while (!lifecycleManager.getState().isStopped())
{
try
{
// Wait if the service is paused
if (lifecycleManager.isPhaseComplete(Pausable.PHASE_NAME))
{
waitIfPaused();
// If service is resumed as part of stopping
if (lifecycleManager.getState().isStopping())
{
if (!isQueuePersistent() && (queueSession != null && getQueueSize() > 0))
{
// Any messages in a non-persistent queue when paused
// service is stopped are lost
logger.warn(CoreMessages.stopPausedSedaStageNonPeristentQueueMessageLoss(
getQueueSize(), getQueueName()));
}
break;
}
}
// If we're doing a draining stop, read all events from the queue
// before stopping
if (lifecycleManager.getState().isStopping())
{
if (isQueuePersistent() || queueSession == null || getQueueSize() <= 0)
{
break;
}
}
event = (DefaultMuleEvent) dequeue();
}
catch (InterruptedException ie)
{
break;
}
catch (Exception e)
{
muleContext.getExceptionListener().handleException(e);
}
if (event != null)
{
if (isStatsEnabled())
{
queueStatistics.decQueuedEvent();
}
if (logger.isDebugEnabled())
{
logger.debug(MessageFormat.format("{0}: Dequeued event from {1}",
getStageDescription(), getQueueName()));
}
SedaStageWorker work = new SedaStageWorker(event);
if (doThreading)
{
try
{
// TODO Remove this thread handoff to ensure Zero Message Loss
workManagerSource.getWorkManager().scheduleWork(work, WorkManager.INDEFINITE, null,
new AsyncWorkListener(next));
}
catch (Exception e)
{
// This is a work manager exception, not a Mule event-related exception
event.getFlowConstruct().getExceptionListener().handleException(e, event);
}
}
else
{
try
{
work.doWork();
}
catch (MuleException e)
{
MessagingExceptionHandler exceptionListener = event.getFlowConstruct().getExceptionListener();
if (e instanceof MessagingException)
{
exceptionListener.handleException(e, event);
}
else