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