Search in sources :

Example 1 with Scheduler

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

the class QuartzResourceAdapter method stop.

@Override
public void stop() {
    final Scheduler s = scheduler.getAndSet(null);
    if (null != s) {
        if (null != startThread.get()) {
            startThread.get().interrupt();
        }
        long timeout = SystemInstance.get().getOptions().get(QuartzResourceAdapter.OPENEJB_QUARTZ_TIMEOUT, 10000L);
        if (timeout < 1000L) {
            timeout = 1000L;
        }
        if (timeout > 60000L) {
            timeout = 60000L;
        }
        final CountDownLatch shutdownWait = new CountDownLatch(1);
        Thread stopThread = new Thread("Quartz Scheduler Requested Stop") {

            @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) {
                    QuartzResourceAdapter.this.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("Quartz Scheduler Forced Stop") {

                    @Override
                    public void run() {
                        try {
                            // Force a shutdown without waiting for jobs to complete.
                            s.shutdown(false);
                            Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").warning("Forced Quartz stop - Jobs may be incomplete");
                        } catch (final Throwable e) {
                            QuartzResourceAdapter.this.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);
        }
    }
    this.bootstrapContext.set(null);
    if (null != ex.get()) {
        Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").warning("Error stopping Quartz Scheduler", ex.get());
    } else {
        Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").info("Stopped Quartz Scheduler");
    }
}
Also used : Scheduler(org.apache.openejb.quartz.Scheduler) SchedulerListenerSupport(org.apache.openejb.quartz.listeners.SchedulerListenerSupport) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with Scheduler

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

the class QuartzResourceAdapter method endpointActivation.

@Override
public void endpointActivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) throws ResourceException {
    final Scheduler s = scheduler.get();
    if (null == s) {
        throw new ResourceException("Quartz Scheduler is not available");
    }
    try {
        final JobSpec spec = (JobSpec) activationSpec;
        final MessageEndpoint endpoint = messageEndpointFactory.createEndpoint(null);
        spec.setEndpoint(endpoint);
        final Job job = (Job) endpoint;
        final JobDataMap jobDataMap = spec.getDetail().getJobDataMap();
        jobDataMap.put(Data.class.getName(), new Data(job));
        s.scheduleJob(spec.getDetail(), spec.getTrigger());
    } catch (final SchedulerException e) {
        throw new ResourceException("Failed to schedule job", e);
    }
}
Also used : MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint) JobDataMap(org.apache.openejb.quartz.JobDataMap) SchedulerException(org.apache.openejb.quartz.SchedulerException) Scheduler(org.apache.openejb.quartz.Scheduler) ResourceException(javax.resource.ResourceException) Job(org.apache.openejb.quartz.Job)

Example 3 with Scheduler

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

the class QuartzResourceAdapter method endpointDeactivation.

@Override
public void endpointDeactivation(final MessageEndpointFactory messageEndpointFactory, final ActivationSpec activationSpec) {
    final Scheduler s = scheduler.get();
    if (null == s) {
        throw new IllegalStateException("Quartz Scheduler is not available");
    }
    JobSpec spec = null;
    try {
        spec = (JobSpec) activationSpec;
        s.deleteJob(spec.jobKey());
    } catch (final SchedulerException e) {
        throw new IllegalStateException("Failed to delete job", e);
    } finally {
        if (null != spec) {
            spec.getEndpoint().release();
        }
    }
}
Also used : SchedulerException(org.apache.openejb.quartz.SchedulerException) Scheduler(org.apache.openejb.quartz.Scheduler)

Example 4 with Scheduler

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

the class TimerData method stop.

public void stop() {
    if (trigger != null) {
        try {
            final Scheduler s = timerService.getScheduler();
            if (!s.isShutdown()) {
                if (!isPersistent()) {
                    s.unscheduleJob(trigger.getKey());
                } else {
                    s.pauseTrigger(trigger.getKey());
                }
            }
        } catch (final SchedulerException e) {
            throw new EJBException("fail to cancel the timer", e);
        }
    }
    cancelled = true;
    stopped = true;
}
Also used : SchedulerException(org.apache.openejb.quartz.SchedulerException) Scheduler(org.apache.openejb.quartz.Scheduler) EJBException(javax.ejb.EJBException)

Example 5 with Scheduler

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

the class TimerData method cancel.

public void cancel() {
    if (stopped) {
        return;
    }
    timerService.cancelled(TimerData.this);
    if (trigger != null) {
        try {
            final Scheduler s = timerService.getScheduler();
            if (!s.isShutdown()) {
                s.unscheduleJob(trigger.getKey());
            }
        } catch (final SchedulerException e) {
            throw new EJBException("fail to cancel the timer", e);
        }
    }
    cancelled = true;
    try {
        registerTimerDataSynchronization();
    } catch (final TimerStoreException e) {
        throw new EJBException("Failed to register timer data synchronization on cancel", e);
    }
}
Also used : SchedulerException(org.apache.openejb.quartz.SchedulerException) Scheduler(org.apache.openejb.quartz.Scheduler) EJBException(javax.ejb.EJBException)

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