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);
}
}
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;
}
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.
}
}
Aggregations