Search in sources :

Example 1 with SynapseTaskException

use of org.apache.synapse.task.SynapseTaskException in project wso2-synapse by wso2.

the class QuartzTaskManager method delete.

@Override
public boolean delete(String nameGroup) {
    if (nameGroup == null) {
        return false;
    }
    assertInitialized();
    assertStarted();
    String[] list = nameGroup.split("::");
    String name = list[0];
    String group = list[1];
    if (name == null || "".equals(name)) {
        throw new SynapseTaskException("Task name is null", logger);
    }
    if (group == null || "".equals(group)) {
        group = TaskDescription.DEFAULT_GROUP;
        if (logger.isDebugEnabled()) {
            logger.debug("Task group is null or empty , using default group :" + TaskDescription.DEFAULT_GROUP);
        }
    }
    boolean deleteResult;
    try {
        synchronized (lock) {
            deleteResult = scheduler.deleteJob(new JobKey(name, group));
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Cannot delete task [" + name + "::" + group + "]");
    }
    logger.debug("Deleted task [" + name + "::" + group + "] [" + deleteResult + "]");
    return true;
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) SynapseTaskException(org.apache.synapse.task.SynapseTaskException)

Example 2 with SynapseTaskException

use of org.apache.synapse.task.SynapseTaskException in project wso2-synapse by wso2.

the class QuartzTaskManager method schedule.

@Override
public boolean schedule(TaskDescription taskDescription) {
    assertInitialized();
    assertStarted();
    if (taskDescription == null) {
        throw new SynapseTaskException("Task Description cannot be found", logger);
    }
    Trigger trigger;
    JobDetail jobDetail;
    synchronized (lock) {
        if (triggerFactory == null) {
            throw new SynapseTaskException("TriggerFactory cannot be found", logger);
        }
        if (jobDetailFactory == null) {
            throw new SynapseTaskException("JobDetailFactory cannot be found", logger);
        }
        trigger = triggerFactory.createTrigger(taskDescription);
        if (trigger == null) {
            throw new SynapseTaskException("Trigger cannot be created from : " + taskDescription, logger);
        }
        jobDetail = jobDetailFactory.createJobDetail(taskDescription, taskDescription.getResources(), SimpleQuartzJob.class);
        if (jobDetail == null) {
            throw new SynapseTaskException("JobDetail cannot be created from : " + taskDescription + " and job class " + taskDescription.getTaskImplClassName(), logger);
        }
    }
    Object clsInstance = taskDescription.getResource(TaskDescription.INSTANCE);
    if (clsInstance == null) {
        String className = (String) taskDescription.getProperty(TaskDescription.CLASSNAME);
        try {
            clsInstance = Class.forName(className).newInstance();
            if (clsInstance instanceof ManagedLifecycle) {
                Object se = properties.get(TaskConstants.SYNAPSE_ENV);
                if (!(se instanceof SynapseEnvironment)) {
                    return false;
                }
                ((ManagedLifecycle) clsInstance).init((SynapseEnvironment) se);
            }
            for (Object property : taskDescription.getXmlProperties()) {
                OMElement prop = (OMElement) property;
                logger.debug("Found Property : " + prop.toString());
                PropertyHelper.setStaticProperty(prop, clsInstance);
            }
        } catch (ClassNotFoundException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        } catch (InstantiationException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        } catch (IllegalAccessException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        }
    }
    if (!(clsInstance instanceof Task)) {
        logger.error("Could not schedule task[" + name + "]. Cannot load class " + "org.apache.synapse.startup.quartz.SimpleQuartzJob");
        return false;
    }
    jobDetail.getJobDataMap().put(TaskDescription.INSTANCE, clsInstance);
    jobDetail.getJobDataMap().put(TaskDescription.CLASSNAME, clsInstance.getClass().toString());
    jobDetail.getJobDataMap().put(TaskConstants.SYNAPSE_ENV, getProperty(TaskConstants.SYNAPSE_ENV));
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("scheduling job : " + jobDetail + " with trigger " + trigger);
        }
        if (taskDescription.getCount() != 0 && !isTaskRunning(jobDetail.getKey())) {
            try {
                synchronized (lock) {
                    scheduler.scheduleJob(jobDetail, trigger);
                }
            } catch (ObjectAlreadyExistsException e) {
                logger.warn("did not schedule the job : " + jobDetail + ". the job is already running.");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("did not schedule the job : " + jobDetail + ". count is zero.");
            }
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Error scheduling job : " + jobDetail + " with trigger " + trigger);
    }
    logger.info("Scheduled task [" + taskDescription.getName() + "::" + taskDescription.getTaskGroup() + "]");
    return true;
}
Also used : Task(org.apache.synapse.task.Task) SchedulerException(org.quartz.SchedulerException) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) SynapseTaskException(org.apache.synapse.task.SynapseTaskException) ManagedLifecycle(org.apache.synapse.ManagedLifecycle) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 3 with SynapseTaskException

use of org.apache.synapse.task.SynapseTaskException in project wso2-synapse by wso2.

the class QuartzTaskManager method resumeAll.

@Override
public boolean resumeAll() {
    try {
        assertInitialized();
        assertStarted();
        synchronized (lock) {
            scheduler.resumeAll();
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Error resuming tasks ", e, logger);
    }
    return true;
}
Also used : SchedulerException(org.quartz.SchedulerException) SynapseTaskException(org.apache.synapse.task.SynapseTaskException)

Example 4 with SynapseTaskException

use of org.apache.synapse.task.SynapseTaskException in project wso2-synapse by wso2.

the class QuartzTaskManager method init.

@Override
public boolean init(Properties properties) {
    StdSchedulerFactory sf = new StdSchedulerFactory();
    if (properties != null) {
        String quartzConf = properties.getProperty(QUARTZ_CONF);
        try {
            if (quartzConf != null && !"".equals(quartzConf)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Initiating a Scheduler with configuration : " + quartzConf);
                }
                sf.initialize(quartzConf);
            }
        } catch (SchedulerException e) {
            throw new SynapseTaskException("Error initiating scheduler factory " + sf + "with configuration loaded from " + quartzConf, e, logger);
        }
    }
    try {
        synchronized (lock) {
            if (name != null) {
                scheduler = sf.getScheduler(name);
            }
            if (scheduler == null) {
                scheduler = sf.getScheduler();
            }
            initialized = true;
            logger.info("initialized");
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Error getting a  scheduler instance form scheduler" + " factory " + sf, e, logger);
    }
    return true;
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerException(org.quartz.SchedulerException) SynapseTaskException(org.apache.synapse.task.SynapseTaskException)

Example 5 with SynapseTaskException

use of org.apache.synapse.task.SynapseTaskException in project wso2-synapse by wso2.

the class QuartzTaskManager method pauseAll.

@Override
public boolean pauseAll() {
    try {
        assertInitialized();
        assertStarted();
        synchronized (lock) {
            scheduler.pauseAll();
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Error pausing tasks ", e, logger);
    }
    return true;
}
Also used : SchedulerException(org.quartz.SchedulerException) SynapseTaskException(org.apache.synapse.task.SynapseTaskException)

Aggregations

SynapseTaskException (org.apache.synapse.task.SynapseTaskException)5 SchedulerException (org.quartz.SchedulerException)5 OMElement (org.apache.axiom.om.OMElement)1 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Task (org.apache.synapse.task.Task)1 JobDetail (org.quartz.JobDetail)1 JobKey (org.quartz.JobKey)1 ObjectAlreadyExistsException (org.quartz.ObjectAlreadyExistsException)1 Trigger (org.quartz.Trigger)1 StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)1