use of org.jaffa.modules.scheduler.services.ScheduledTask in project jaffa-framework by jaffa-projects.
the class TaskFinderTx method find.
/**
* Searches for Task objects.
* @param taskType taskType of tasks that should be returned.
* @throws FrameworkException in case any internal error occurs.
* @throws ApplicationExceptions Indicates application error(s).
* @return The search results.
*/
public TaskFinderOutDto find(String taskType) throws FrameworkException, ApplicationExceptions {
UOW uow = null;
try {
if (log.isDebugEnabled())
log.debug("Retrieving all scheduled tasks");
uow = new UOW();
SchedulerHelper sh = SchedulerHelperFactory.instance();
TaskFinderOutDto output = new TaskFinderOutDto();
output.setSchedulerStatus(sh.getSchedulerStatus());
if (output.getSchedulerStatus() == SchedulerHelper.SchedulerStatusEnumeration.STOPPED) {
if (log.isDebugEnabled())
log.debug("The Scheduler has been shutdown. The Task list cannot be obtained");
} else {
ScheduledTask[] tasks = null;
if (SecurityManager.checkFunctionAccess("Jaffa.Scheduler.Task.InquiryAll"))
tasks = sh.getTasks(null, taskType != null ? taskType : null);
else
tasks = sh.getTasks(org.jaffa.security.SecurityManager.getPrincipal().getName(), taskType != null ? taskType : null);
if (tasks != null) {
Collection<TaskFinderOutRowDto> rows = new LinkedList<TaskFinderOutRowDto>();
for (ScheduledTask task : tasks) {
if (SchedulerBrowser.hasBrowseTaskAccess(task)) {
TaskFinderOutRowDto row = new TaskFinderOutRowDto(task);
if (row.getFailedTaskCount() != null && row.getFailedTaskCount() > 0)
row.setLastError(findLastError(uow, task.getScheduledTaskId()));
row.setHasAdminTaskAccess(SchedulerBrowser.hasAdminTaskAccess(task.getTaskType()));
rows.add(row);
}
}
output.setRows(rows.toArray(new TaskFinderOutRowDto[rows.size()]));
}
}
if (log.isDebugEnabled())
log.debug("Output: " + output);
return output;
} catch (Exception e) {
throw ExceptionHelper.throwAFR(e);
} finally {
if (uow != null)
uow.rollback();
}
}
use of org.jaffa.modules.scheduler.services.ScheduledTask in project jaffa-framework by jaffa-projects.
the class TransactionInvokerJob method execute.
/**
* Called by the <code>{@link org.quartz.Scheduler}</code> when a
* <code>{@link org.quartz.Trigger}</code> fires that is associated with
* the <code>Job</code>.
*
* @param context The Quartz JobExecutionContext structure used to retrieve job specific information
* @throws JobExecutionException if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context) throws JobExecutionException {
String scheduledTaskId = null;
String userId = null;
Object businessObject = null;
UserContextWrapper ucw = null;
try {
List<JobExecutionContext> jobs = context.getScheduler().getCurrentlyExecutingJobs();
if (log.isDebugEnabled())
log.debug("Job Size:- " + jobs.size());
if (log.isDebugEnabled())
log.debug("Starting Checking Scheduled Tasks");
for (JobExecutionContext job : jobs) {
if (log.isDebugEnabled()) {
log.debug("Job Trigger: " + job.getTrigger());
log.debug("Context Trigger: " + context.getTrigger());
}
if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(this)) {
if (log.isDebugEnabled())
log.debug("There's another instance running, so leaving" + this);
return;
}
}
// Obtain the Task from the Job details
ScheduledTask task = QuartzSchedulerHelper.jobDataMapToTask(context.getJobDetail().getJobDataMap());
if (log.isDebugEnabled())
log.debug("Executing " + task);
scheduledTaskId = task.getScheduledTaskId();
userId = task.getRunAs();
businessObject = task.getBusinessObject();
// Switch the thread to use the context the "RunAs" is set to, so this propergates when creating the JaffaTransaction/JMS records to process
if (log.isDebugEnabled())
log.debug("Set up use context. RunAs = " + userId);
ucw = UserContextWrapperFactory.instance(userId);
// Sets Log4J's MDC to enable BusinessEventLogging
MDC.put(BusinessEventLogMeta.SCHEDULED_TASK_ID, scheduledTaskId);
MDC.put(BusinessEventLogMeta.LOGGED_BY, userId);
// Set up some Thread Level variables for the Jaffa Transaction. This will help distinguish between a Schedule invoke transaction and any others
ContextManagerFactory.instance().setProperty(CONTEXT_SCHEDULED_TASK_ID, scheduledTaskId);
// Send a Jaffa Transaction message
if (log.isInfoEnabled())
log.info(MessageHelper.findMessage("label.Jaffa.Scheduler.JaffaTransactionInvokerJob.start", new Object[] { businessObject, userId, scheduledTaskId }));
UOWHelper.addMessage(businessObject);
if (log.isInfoEnabled())
log.info(MessageHelper.findMessage("label.Jaffa.Scheduler.JaffaTransactionInvokerJob.success", new Object[] { businessObject, userId, scheduledTaskId }));
} catch (Exception e) {
log.error(MessageHelper.findMessage("error.Jaffa.Scheduler.JaffaTransactionInvokerJob.error", new Object[] { businessObject, userId, scheduledTaskId }), e);
throw new JobExecutionException(e);
} finally {
// Unset the Logging context
MDC.remove(BusinessEventLogMeta.SCHEDULED_TASK_ID);
MDC.remove(BusinessEventLogMeta.LOGGED_BY);
// Clear context for this user
if (ucw != null) {
if (log.isDebugEnabled())
log.debug("Unset user context");
ucw.unsetContext();
}
}
}
use of org.jaffa.modules.scheduler.services.ScheduledTask in project jaffa-framework by jaffa-projects.
the class TaskMaintenanceTx method retrieve.
/**
* Returns the details for Task.
* @param input The criteria based on which an object will be retrieved.
* @throws ApplicationExceptions This will be thrown if the criteria contains invalid data.
* @throws FrameworkException Indicates some system error.
* @return The object details. A null indicates, the object was not found.
*/
public TaskMaintenanceOutDto retrieve(TaskMaintenanceDto input) throws FrameworkException, ApplicationExceptions {
if (log.isDebugEnabled())
log.debug("Input to retrieve(): " + input);
// Retrieve the task
ScheduledTask task = SchedulerHelperFactory.instance().getTask(input.getScheduledTaskId());
if (task == null)
throw new ApplicationExceptions(new JaffaSchedulerApplicationException(JaffaSchedulerApplicationException.TASK_NOT_FOUND, new Object[] { input.getScheduledTaskId() }));
// Mold the task into the TaskMaintenanceDto
TaskMaintenanceOutDto output = buildRetrieveOutDto(task);
if (log.isDebugEnabled())
log.debug("Output from retrieve(): " + output);
return output;
}
Aggregations