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