Search in sources :

Example 1 with ScheduledTask

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();
    }
}
Also used : SchedulerHelper(org.jaffa.modules.scheduler.services.SchedulerHelper) ScheduledTask(org.jaffa.modules.scheduler.services.ScheduledTask) UOW(org.jaffa.persistence.UOW) TaskFinderOutDto(org.jaffa.modules.scheduler.components.taskfinder.dto.TaskFinderOutDto) LinkedList(java.util.LinkedList) FrameworkException(org.jaffa.exceptions.FrameworkException) TaskFinderOutRowDto(org.jaffa.modules.scheduler.components.taskfinder.dto.TaskFinderOutRowDto)

Example 2 with ScheduledTask

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();
        }
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) UserContextWrapper(org.jaffa.modules.user.services.UserContextWrapper) JobExecutionContext(org.quartz.JobExecutionContext) ScheduledTask(org.jaffa.modules.scheduler.services.ScheduledTask) JobExecutionException(org.quartz.JobExecutionException)

Example 3 with ScheduledTask

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;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) JaffaSchedulerApplicationException(org.jaffa.modules.scheduler.services.JaffaSchedulerApplicationException) ScheduledTask(org.jaffa.modules.scheduler.services.ScheduledTask) TaskMaintenanceOutDto(org.jaffa.modules.scheduler.components.taskmaintenance.dto.TaskMaintenanceOutDto)

Aggregations

ScheduledTask (org.jaffa.modules.scheduler.services.ScheduledTask)3 LinkedList (java.util.LinkedList)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 TaskFinderOutDto (org.jaffa.modules.scheduler.components.taskfinder.dto.TaskFinderOutDto)1 TaskFinderOutRowDto (org.jaffa.modules.scheduler.components.taskfinder.dto.TaskFinderOutRowDto)1 TaskMaintenanceOutDto (org.jaffa.modules.scheduler.components.taskmaintenance.dto.TaskMaintenanceOutDto)1 JaffaSchedulerApplicationException (org.jaffa.modules.scheduler.services.JaffaSchedulerApplicationException)1 SchedulerHelper (org.jaffa.modules.scheduler.services.SchedulerHelper)1 UserContextWrapper (org.jaffa.modules.user.services.UserContextWrapper)1 UOW (org.jaffa.persistence.UOW)1 JobExecutionContext (org.quartz.JobExecutionContext)1 JobExecutionException (org.quartz.JobExecutionException)1