@Override
public boolean scheduleTask(MessageTask task) {
String taskId = task.getTaskId();
Message message = task.getMessage();
NotificationContext notificationContext = task.getNotificationContext();
try {
if (message.getMsgType().equals(MessageType.STATE_TRANSITION.toString())) {
checkResourceConfig(message.getResourceId().toString(), notificationContext.getManager());
}
LOG.info("Scheduling message: " + taskId);
// System.out.println("sched msg: " + message.getPartitionName() + "-"
// + message.getTgtName() + "-" + message.getFromState() + "-"
// + message.getToState());
_statusUpdateUtil.logInfo(message, HelixTaskExecutor.class,
"Message handling task scheduled", notificationContext.getManager()
.getHelixDataAccessor());
// this sync guarantees that ExecutorService.submit() task and put taskInfo into map are
// sync'ed
synchronized (_lock) {
if (!_taskMap.containsKey(taskId)) {
ExecutorService exeSvc = findExecutorServiceForMsg(message);
Future<HelixTaskResult> future = exeSvc.submit(task);
TimerTask timerTask = null;
if (message.getExecutionTimeout() > 0) {
timerTask = new MessageTimeoutTask(this, task);
_timer.schedule(timerTask, message.getExecutionTimeout());
LOG.info("Message starts with timeout " + message.getExecutionTimeout() + " MsgId: "
+ task.getTaskId());
} else {
LOG.debug("Message does not have timeout. MsgId: " + task.getTaskId());
}
_taskMap.put(taskId, new MessageTaskInfo(task, future, timerTask));
LOG.info("Message: " + taskId + " handling task scheduled");
return true;
} else {
_statusUpdateUtil.logWarning(message, HelixTaskExecutor.class,
"Message handling task already sheduled for " + taskId, notificationContext
.getManager().getHelixDataAccessor());
}
}
} catch (Exception e) {
LOG.error("Error while executing task. " + message, e);
_statusUpdateUtil.logError(message, HelixTaskExecutor.class, e, "Error while executing task "
+ e, notificationContext.getManager().getHelixDataAccessor());
}
return false;
}