Search in sources :

Example 6 with Scheduler

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

the class JobBean method createJob.

@Override
public Date createJob() throws Exception {
    final QuartzResourceAdapter ra = (QuartzResourceAdapter) new InitialContext().lookup("java:openejb/Resource/QuartzResourceAdapter");
    final Scheduler s = ra.getScheduler();
    // Add a job type
    final JobDetail jd = JobBuilder.newJob(MyTestJob.class).withIdentity("job1", "group1").build();
    jd.getJobDataMap().put("MyJobKey", "MyJobValue");
    // Schedule my 'test' job to run now
    final SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").forJob(jd).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0).withIntervalInSeconds(0)).build();
    return s.scheduleJob(jd, trigger);
}
Also used : QuartzResourceAdapter(org.apache.openejb.resource.quartz.QuartzResourceAdapter) JobDetail(org.apache.openejb.quartz.JobDetail) Scheduler(org.apache.openejb.quartz.Scheduler) SimpleTrigger(org.apache.openejb.quartz.SimpleTrigger) InitialContext(javax.naming.InitialContext)

Example 7 with Scheduler

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

the class Assembler method resumePersistentSchedulers.

private void resumePersistentSchedulers(final AppContext appContext) {
    try {
        // if quartz is missing
        final Scheduler globalScheduler = SystemInstance.get().getComponent(Scheduler.class);
        final Collection<Scheduler> schedulers = new ArrayList<>();
        for (final BeanContext ejb : appContext.getBeanContexts()) {
            final Scheduler scheduler = ejb.get(Scheduler.class);
            if (scheduler == null || scheduler == globalScheduler || schedulers.contains(scheduler)) {
                continue;
            }
            schedulers.add(scheduler);
            try {
                scheduler.resumeAll();
            } catch (final Exception e) {
                logger.warning("Can't resume scheduler for " + ejb.getEjbName(), e);
            }
        }
    } catch (final NoClassDefFoundError ncdfe) {
    // no-op
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) Scheduler(org.apache.openejb.quartz.Scheduler) ArrayList(java.util.ArrayList) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 8 with Scheduler

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

the class EjbTimerServiceImpl method shutdownMyScheduler.

private void shutdownMyScheduler() {
    if (scheduler == null) {
        return;
    }
    boolean defaultScheduler = false;
    final Scheduler ds = SystemInstance.get().getComponent(Scheduler.class);
    try {
        // == is the faster way to test, we rely on name (key in quartz registry) only for serialization
        defaultScheduler = ds == scheduler || scheduler.getSchedulerName().equals(ds.getSchedulerName());
    } catch (final Exception e) {
    // no-op: default should be fine
    }
    // if specific instance
    if (!defaultScheduler) {
        shutdown(scheduler);
    }
}
Also used : Scheduler(org.apache.openejb.quartz.Scheduler) 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 9 with Scheduler

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

Scheduler (org.apache.openejb.quartz.Scheduler)9 SchedulerException (org.apache.openejb.quartz.SchedulerException)6 EJBException (javax.ejb.EJBException)4 IOException (java.io.IOException)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)3 SystemException (javax.transaction.SystemException)2 ApplicationException (org.apache.openejb.ApplicationException)2 JobDetail (org.apache.openejb.quartz.JobDetail)2 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1