/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.dataservices.taskscheduler;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.task.Task;
import org.quartz.*;
import org.wso2.carbon.dataservices.taskscheduler.constants.TaskSchedulerConstants;
/**
* This class implements the execution of a particular job
*/
public class TaskSchedulingJob implements Job {
private static final Log log = LogFactory.getLog(TaskSchedulingJob.class);
/**
* This method implements the execution logic of a particular quartz job.
*
* @param jobExecutionContext jobExecutionContext that contains the details required for the
* job execution.
* @throws JobExecutionException JobExecutionException
*/
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Task task;
JobDetail jobDetail = jobExecutionContext.getJobDetail();
if (log.isDebugEnabled()) {
log.debug("Executing scheduling task :" + jobDetail.getFullName());
}
JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
String jobClassName = (String) jobDataMap.get(TaskSchedulerConstants.CLASS_NAME);
try {
task = (Task) getClass().getClassLoader().loadClass(jobClassName).newInstance();
if (task instanceof TaskSchedulerLifeCycleCallback) {
((TaskSchedulerLifeCycleCallback) task).init(jobDetail.getJobDataMap());
}
task.execute();
if (task instanceof TaskSchedulerLifeCycleCallback) {
((TaskSchedulerLifeCycleCallback) task).destroy();
}
ConfigurationContext configurationContext = (ConfigurationContext) jobDataMap.get(
TaskSchedulerConstants.CONFIGURATION_CONTEXT);
log.info("Task has been successfully executed");
/*
* Signalling to delete the Task Description from the Task Description Repository
* when the next fire time is null
*/
if (jobExecutionContext.getNextFireTime() == null) {
TaskSchedulingManager taskSchedulingManager =
TaskSchedulingManager.getInstance();
taskSchedulingManager.deleteTaskDescription((String) jobDetail.getJobDataMap().get(
TaskSchedulerConstants.TASK_NAME), (String) jobDetail.getJobDataMap().get(
TaskSchedulerConstants.JOB_GROUP), configurationContext);
}
} catch (ClassNotFoundException e) {
log.error("Job Class Not Found ", e);
} catch (ClassCastException e) {
log.error("Task class cannot be casted", e);
} catch (InstantiationException e) {
log.error("Cannot instantiate the Job Class", e);
} catch (IllegalAccessException e) {
log.error("", e);
}
}
}