use of org.apache.openejb.quartz.listeners.SchedulerListenerSupport in project tomee by apache.
the class QuartzResourceAdapter method start.
@Override
public void start(final BootstrapContext bootstrapContext) throws ResourceAdapterInternalException {
if (null != this.bootstrapContext.getAndSet(bootstrapContext)) {
Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").warning("QuartzResourceAdapter is already starting");
return;
}
final CountDownLatch signal = new CountDownLatch(1);
long timeout = SystemInstance.get().getOptions().get(QuartzResourceAdapter.OPENEJB_QUARTZ_TIMEOUT, 10000L);
if (timeout < 1000L) {
timeout = 1000L;
}
if (timeout > 60000L) {
timeout = 60000L;
}
//Allow org.apache.openejb.quartz.InterruptableJob implementors to be interrupted on shutdown
JavaSecurityManagers.setSystemProperty(StdSchedulerFactory.PROP_SCHED_INTERRUPT_JOBS_ON_SHUTDOWN, JavaSecurityManagers.getSystemProperty(StdSchedulerFactory.PROP_SCHED_INTERRUPT_JOBS_ON_SHUTDOWN, "true"));
JavaSecurityManagers.setSystemProperty(StdSchedulerFactory.PROP_SCHED_INTERRUPT_JOBS_ON_SHUTDOWN_WITH_WAIT, JavaSecurityManagers.getSystemProperty(StdSchedulerFactory.PROP_SCHED_INTERRUPT_JOBS_ON_SHUTDOWN_WITH_WAIT, "true"));
//Let the user enable this if they really want it
JavaSecurityManagers.setSystemProperty(StdSchedulerFactory.PROP_SCHED_SKIP_UPDATE_CHECK, JavaSecurityManagers.getSystemProperty(StdSchedulerFactory.PROP_SCHED_SKIP_UPDATE_CHECK, "true"));
JavaSecurityManagers.setSystemProperty("org.terracotta.quartz.skipUpdateCheck", JavaSecurityManagers.getSystemProperty("org.terracotta.quartz.skipUpdateCheck", "true"));
startThread.set(new Thread("Quartz Scheduler Start") {
@Override
public void run() {
try {
QuartzResourceAdapter.this.scheduler.set(StdSchedulerFactory.getDefaultScheduler());
} catch (final Exception e) {
QuartzResourceAdapter.this.ex.set(e);
return;
}
try {
QuartzResourceAdapter.this.scheduler.get().getListenerManager().addSchedulerListener(new SchedulerListenerSupport() {
@Override
public void schedulerStarted() {
signal.countDown();
}
});
QuartzResourceAdapter.this.scheduler.get().start();
} catch (final Throwable e) {
QuartzResourceAdapter.this.ex.set(e);
signal.countDown();
}
}
});
startThread.get().setDaemon(true);
startThread.get().start();
boolean started = false;
try {
started = signal.await(timeout, TimeUnit.MILLISECONDS);
} catch (final InterruptedException e) {
//Ignore
}
final Throwable exception = ex.get();
if (null != exception) {
final String err = "Error creating Quartz Scheduler";
Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").error(err, exception);
throw new ResourceAdapterInternalException(err, exception);
}
if (started) {
Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").info("Started Quartz Scheduler");
} else {
Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources").warning("Failed to start Quartz Scheduler within defined timeout, status unknown");
}
}
use of org.apache.openejb.quartz.listeners.SchedulerListenerSupport 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");
}
}
use of org.apache.openejb.quartz.listeners.SchedulerListenerSupport 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