Skip to content

Instantly share code, notes, and snippets.

@abstractj
Created January 18, 2012 01:33
Show Gist options
  • Save abstractj/1630228 to your computer and use it in GitHub Desktop.
Save abstractj/1630228 to your computer and use it in GitHub Desktop.
BaseTriggerListener
/*
* Copyright 2008-2011 Red Hat, Inc, and individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.projectodd.polyglot.jobs;
import org.jboss.logging.Logger;
import org.jboss.threads.JBossThreadFactory;
import org.jboss.vfs.TempFileProvider;
import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;
import java.security.AccessController;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class BaseTriggerListener implements TriggerListener {
public static final String TRIGGER_LISTENER_NAME = BaseTriggerListener.class.getSimpleName();
@Override
public String getName() {
return TRIGGER_LISTENER_NAME;
}
@Override
public void triggerFired(Trigger trigger, final JobExecutionContext jobExecutionContext) {
// TODO include some action here when trigger starts
}
@Override
public boolean vetoJobExecution(Trigger trigger, final JobExecutionContext jobExecutionContext) {
BaseTriggerListener.registerWatchDog(jobExecutionContext);
return true;
}
private static void registerWatchDog(final JobExecutionContext jobExecutionContext) {
long delay = (Long) jobExecutionContext.getJobDetail().getJobDataMap().get("timeout");
final InterruptableJob runningJob = ((InterruptableJob) jobExecutionContext.getJobInstance());
TempFileProvider tempFileProvider = TempFileProvider.create("temp", Executors.newScheduledThreadPool(2));
final JBossThreadFactory threadFactory = new JBossThreadFactory(new ThreadGroup("org.projectodd.polyglot.jobs"), Boolean.FALSE, null, "%G - %t", null, null, AccessController.getContext());
tempFileProvider = TempFileProvider.create("temp", Executors.newScheduledThreadPool(2, threadFactory));
if (delay > 0) {
//TODO Replace ExecutorService by JBossThreadPool
ScheduledExecutorService service = Executors.newScheduledThreadPool(1, threadFactory);
service.schedule(new Runnable() {
public void run() {
log.info("|||||||||||||||| Trying to interrupt the job |||||||||||||||| ");
try {
runningJob.interrupt();
} catch (Exception e) {
log.error("Interruption failed", e);
}
}
}, delay, TimeUnit.MILLISECONDS);
}
}
@Override
public void triggerMisfired(Trigger trigger) {
// TODO include some action here
}
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext jobExecutionContext, int i) {
// TODO include some action here
}
private static final Logger log = Logger.getLogger("org.projectodd.polyglot.jobs");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment