Search in sources :

Example 6 with SchedulerException

use of org.apache.openejb.quartz.SchedulerException in project tomee by apache.

the class JobSpec method validate.

// -- ActivationSpec methods
@SuppressWarnings("unchecked")
@Override
public void validate() throws InvalidPropertyException {
    if (invalidProperty != null) {
        throw invalidProperty;
    }
    final int i = hashCode();
    detail = JobBuilder.newJob(QuartzResourceAdapter.JobEndpoint.class).withIdentity("Job" + i, Scheduler.DEFAULT_GROUP).withDescription(description).requestRecovery(recoverable).storeDurably(durable).build();
    final TriggerBuilder tb = TriggerBuilder.newTrigger().forJob(detail).withIdentity("Trigger" + i, Scheduler.DEFAULT_GROUP).withDescription(description);
    if (startTime != null) {
        tb.startAt(parse(startTime));
    }
    if (endTime != null) {
        tb.endAt(parse(endTime));
    }
    if (calendarName != null) {
        tb.modifiedByCalendar(calendarName);
    }
    final CronScheduleBuilder csb = CronScheduleBuilder.cronSchedule(getCronExpression());
    if (timeZone != null) {
        csb.inTimeZone(TimeZone.getTimeZone(timeZone));
    }
    trigger = tb.withSchedule(csb).build();
    try {
        ((CronTriggerImpl) trigger).validate();
    } catch (final SchedulerException e) {
        throw new InvalidPropertyException(e);
    }
}
Also used : CronScheduleBuilder(org.apache.openejb.quartz.CronScheduleBuilder) SchedulerException(org.apache.openejb.quartz.SchedulerException) InvalidPropertyException(javax.resource.spi.InvalidPropertyException) CronTriggerImpl(org.apache.openejb.quartz.impl.triggers.CronTriggerImpl) TriggerBuilder(org.apache.openejb.quartz.TriggerBuilder) MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint)

Example 7 with SchedulerException

use of org.apache.openejb.quartz.SchedulerException 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)

Example 8 with SchedulerException

use of org.apache.openejb.quartz.SchedulerException in project tomee by apache.

the class EjbTimerServiceImpl method shutdown.

private static void shutdown(final Scheduler s) throws OpenEJBRuntimeException {
    try {
        if (null != s && !s.isShutdown() && s.isStarted()) {
            try {
                s.pauseAll();
            } catch (final SchedulerException e) {
            // no-op
            }
            long timeout = SystemInstance.get().getOptions().get(QuartzResourceAdapter.OPENEJB_QUARTZ_TIMEOUT, 10000L);
            if (timeout < 1000L) {
                timeout = 1000L;
            }
            final CountDownLatch shutdownWait = new CountDownLatch(1);
            final AtomicReference<Throwable> ex = new AtomicReference<Throwable>();
            String n = "Unknown";
            try {
                n = s.getSchedulerName();
            } catch (final SchedulerException e) {
                log.warning("EjbTimerService scheduler has no name");
            }
            final String name = n;
            Thread stopThread = new Thread(name + " shutdown wait") {

                @Override
                public void run() {
                    try {
                        s.getListenerManager().addSchedulerListener(new SchedulerListenerSupport() {

                            @Override
                            public void schedulerShutdown() {
                                shutdownWait.countDown();
                            }
                        });
                        //Shutdown, but give running jobs a chance to complete.
                        //User scheduled jobs should really implement InterruptableJob
                        s.shutdown(true);
                    } catch (final Throwable e) {
                        ex.set(e);
                        shutdownWait.countDown();
                    }
                }
            };
            stopThread.setDaemon(true);
            stopThread.start();
            boolean stopped = false;
            try {
                stopped = shutdownWait.await(timeout, TimeUnit.MILLISECONDS);
            } catch (final InterruptedException e) {
            //Ignore
            }
            try {
                if (!stopped || !s.isShutdown()) {
                    stopThread = new Thread(name + " shutdown forced") {

                        @Override
                        public void run() {
                            try {
                                //Force a shutdown without waiting for jobs to complete.
                                s.shutdown(false);
                                log.warning("Forced " + name + " shutdown - Jobs may be incomplete");
                            } catch (final Throwable e) {
                                ex.set(e);
                            }
                        }
                    };
                    stopThread.setDaemon(true);
                    stopThread.start();
                    try {
                        //Give the forced shutdown a chance to complete
                        stopThread.join(timeout);
                    } catch (final InterruptedException e) {
                    //Ignore
                    }
                }
            } catch (final Throwable e) {
                ex.set(e);
            }
            final Throwable t = ex.get();
            if (null != t) {
                throw new OpenEJBRuntimeException("Unable to shutdown " + name + " scheduler", t);
            }
        }
    } catch (final SchedulerException e) {
    //Ignore - This can only be a shutdown issue that we have no control over.
    }
}
Also used : SchedulerException(org.apache.openejb.quartz.SchedulerException) SchedulerListenerSupport(org.apache.openejb.quartz.listeners.SchedulerListenerSupport) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

SchedulerException (org.apache.openejb.quartz.SchedulerException)8 Scheduler (org.apache.openejb.quartz.Scheduler)5 EJBException (javax.ejb.EJBException)4 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)3 IOException (java.io.IOException)2 MessageEndpoint (javax.resource.spi.endpoint.MessageEndpoint)2 SystemException (javax.transaction.SystemException)2 ApplicationException (org.apache.openejb.ApplicationException)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 JobDataMap (org.apache.openejb.quartz.JobDataMap)2 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ResourceException (javax.resource.ResourceException)1 InvalidPropertyException (javax.resource.spi.InvalidPropertyException)1 SystemInstance (org.apache.openejb.loader.SystemInstance)1 CronScheduleBuilder (org.apache.openejb.quartz.CronScheduleBuilder)1 Job (org.apache.openejb.quartz.Job)1 JobDetail (org.apache.openejb.quartz.JobDetail)1 Trigger (org.apache.openejb.quartz.Trigger)1