Search in sources :

Example 1 with StdSchedulerFactory

use of org.apache.openejb.quartz.impl.StdSchedulerFactory in project tomee by apache.

the class EjbTimerServiceImpl method getDefaultScheduler.

public static synchronized Scheduler getDefaultScheduler(final BeanContext deployment) {
    Scheduler scheduler = deployment.get(Scheduler.class);
    if (scheduler != null) {
        boolean valid;
        try {
            valid = !scheduler.isShutdown();
        } catch (final Exception ignored) {
            valid = false;
        }
        if (valid) {
            return scheduler;
        }
    }
    Scheduler thisScheduler;
    synchronized (deployment.getId()) {
        // should be done only once so no perf issues
        scheduler = deployment.get(Scheduler.class);
        if (scheduler != null) {
            return scheduler;
        }
        final Properties properties = new Properties();
        int quartzProps = 0;
        quartzProps += putAll(properties, SystemInstance.get().getProperties());
        quartzProps += putAll(properties, deployment.getModuleContext().getAppContext().getProperties());
        quartzProps += putAll(properties, deployment.getModuleContext().getProperties());
        quartzProps += putAll(properties, deployment.getProperties());
        // custom config -> don't use default/global scheduler
        // if one day we want to keep a global config for a global scheduler (SystemInstance.get().getProperties()) we'll need to manage resume/pause etc correctly by app
        // since we have a scheduler by ejb today in such a case we don't need
        final boolean newInstance = quartzProps > 0;
        final SystemInstance systemInstance = SystemInstance.get();
        scheduler = systemInstance.getComponent(Scheduler.class);
        if (scheduler == null || newInstance) {
            final boolean useTccl = "true".equalsIgnoreCase(properties.getProperty(OPENEJB_QUARTZ_USE_TCCL, "false"));
            defaultQuartzConfiguration(properties, deployment, newInstance, useTccl);
            try {
                // start in container context to avoid thread leaks
                final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
                if (useTccl) {
                    Thread.currentThread().setContextClassLoader(deployment.getClassLoader());
                } else {
                    Thread.currentThread().setContextClassLoader(EjbTimerServiceImpl.class.getClassLoader());
                }
                try {
                    thisScheduler = new StdSchedulerFactory(properties).getScheduler();
                    thisScheduler.start();
                } finally {
                    Thread.currentThread().setContextClassLoader(oldCl);
                }
                //durability is configured with true, which means that the job will be kept in the store even if no trigger is attached to it.
                //Currently, all the EJB beans share with the same job instance
                final JobDetail job = JobBuilder.newJob(EjbTimeoutJob.class).withIdentity(OPENEJB_TIMEOUT_JOB_NAME, OPENEJB_TIMEOUT_JOB_GROUP_NAME).storeDurably(true).requestRecovery(false).build();
                thisScheduler.addJob(job, true);
            } catch (final SchedulerException e) {
                throw new OpenEJBRuntimeException("Fail to initialize the default scheduler", e);
            }
            if (!newInstance) {
                systemInstance.setComponent(Scheduler.class, thisScheduler);
            }
        } else {
            thisScheduler = scheduler;
        }
        deployment.set(Scheduler.class, thisScheduler);
    }
    return thisScheduler;
}
Also used : StdSchedulerFactory(org.apache.openejb.quartz.impl.StdSchedulerFactory) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) JobDetail(org.apache.openejb.quartz.JobDetail) SchedulerException(org.apache.openejb.quartz.SchedulerException) Scheduler(org.apache.openejb.quartz.Scheduler) SystemInstance(org.apache.openejb.loader.SystemInstance) Properties(java.util.Properties) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException) SchedulerException(org.apache.openejb.quartz.SchedulerException) ApplicationException(org.apache.openejb.ApplicationException) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

IOException (java.io.IOException)1 Properties (java.util.Properties)1 EJBException (javax.ejb.EJBException)1 SystemException (javax.transaction.SystemException)1 ApplicationException (org.apache.openejb.ApplicationException)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 SystemInstance (org.apache.openejb.loader.SystemInstance)1 JobDetail (org.apache.openejb.quartz.JobDetail)1 Scheduler (org.apache.openejb.quartz.Scheduler)1 SchedulerException (org.apache.openejb.quartz.SchedulerException)1 StdSchedulerFactory (org.apache.openejb.quartz.impl.StdSchedulerFactory)1