SandeshaMessageKeys.invalidMsgNumber, Long
.toString(nextMsgno));
throw new SandeshaException(message);
}
InvokerBean selector = new InvokerBean();
selector.setSequenceID(sequenceId);
selector.setMsgNo(nextMsgno);
List invokerBeans = storageMapMgr.find(selector);
//add any msgs that belong to out of order windows
addOutOfOrderInvokerBeansToList(sequenceId,
storageManager, invokerBeans);
// If there aren't any beans to process then move on to the next sequence
if (invokerBeans.size() == 0) {
if (log.isDebugEnabled()) log.debug("Exit: Invoker::internalRun, no beans to invoke on sequence " + sequenceId + ", sleep " + sleep);
if(transaction != null && transaction.isActive()) transaction.commit();
transaction = null;
return sleep;
}
Iterator stMapIt = invokerBeans.iterator();
//TODO correct the locking mechanism to have one lock per sequence.
//TODO should this be a while, not an if?
if (stMapIt.hasNext()) { //some invokation work is present
InvokerBean bean = (InvokerBean) stMapIt.next();
//see if this is an out of order msg
boolean beanIsOutOfOrderMsg = bean.getMsgNo()!=nextMsgno;
String workId = sequenceId + "::" + bean.getMsgNo();
//creating a workId to uniquely identify the
//piece of work that will be assigned to the Worker.
//check whether the bean is already assigned to a worker.
if (getWorkerLock().isWorkPresent(workId)) {
// As there is already a worker assigned we are probably dispatching
// messages too quickly, so we sleep before trying the next sequence.
sleep = true;
String message = SandeshaMessageHelper.getMessage(SandeshaMessageKeys.workAlreadyAssigned, workId);
if (log.isDebugEnabled()) log.debug("Exit: Invoker::internalRun, " + message + ", sleep " + sleep);
if(transaction != null) {
transaction.commit();
transaction = null;
}
return sleep;
}
String messageContextKey = bean.getMessageContextRefKey();
if(transaction != null) {
transaction.commit();
transaction = null;
}
// start a new worker thread and let it do the invocation.
InvokerWorker worker = new InvokerWorker(context,
messageContextKey,
beanIsOutOfOrderMsg); //only ignore nextMsgNumber if the bean is an
//out of order message
worker.setLock(getWorkerLock());
worker.setWorkId(workId);
// Wrap the invoker worker with the correct context, if needed.
Runnable work = worker;
ContextManager contextMgr = SandeshaUtil.getContextManager(context);
if(contextMgr != null) {
work = contextMgr.wrapWithContext(work, bean.getContext());
}
threadPool.execute(work);
//adding the workId to the lock after assigning it to a thread makes sure
//that all the workIds in the Lock are handled by threads.