- 浏览: 2290125 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (357)
- J2EE (49)
- JavaScript (40)
- Spring (19)
- Struts (5)
- CSS (8)
- Hibernate (16)
- Java (67)
- DWR (4)
- JSON (3)
- XFIRE (1)
- Tomcat (1)
- Ant (2)
- 设计模式 (2)
- 经典收藏 (2)
- JSP (10)
- Linux (0)
- WebLogic (11)
- myeclipse (13)
- Buffalo (4)
- 文件上传相关 (1)
- oracle (33)
- html (6)
- JSTL (3)
- SVN (2)
- GIT (1)
- 孙卫琴(Java网络编程精解) (1)
- DOM4J (2)
- Swing (1)
- AJAX (1)
- Eclipse (5)
- 日志组件 (3)
- PowerDesigner (1)
- Jquery (22)
- IT技术开发相关网址 (1)
- Nutz (1)
- 其它 (1)
- Velocity (3)
- WebService (1)
- MySql (2)
- Android (1)
- Maven (2)
- Quartz (11)
- Lucene (1)
- springsource (1)
- Junit (1)
- Activiti (0)
最新评论
-
yzlseu:
拼凑,没有营养
Activiti进阶—分配组任务 -
zhangsenhao:
非常赞!代码很清楚
SpringMVC3.0+MyIbatis3.0(分页示例) -
xiamw2000:
分页写得不对,应该是 : order by ${orderNa ...
SpringMVC3.0+MyIbatis3.0(分页示例) -
sheertewtw:
...
SpringMVC:上传与下载 -
kingtoon:
...
XSS之xssprotect
问题1 如果你的任务执行发生错误了怎么办呀!
Quartz提供了二种解决方法
1 立即重新执行任务
2 立即停止所有相关这个任务的触发器
问题2 怎么去执行呢
Quartz的解决方式是
在你的程序出错时,用Quartz提供的JobExecutionException类相关方法很好的解决
1 立即重新执行任务
请注意其中作者写的注释:
// this job will refire immediately
e2.setRefireImmediately(true);
2 立即停止所有相关这个任务的触发器
请注意其中作者写的注释:
// Quartz will automatically unschedule
// all triggers associated with this job
// so that it does not run again
e2.setUnscheduleAllTriggers(true);
具体代码如下:
BadJob1.java
BadJob2 .java
JobExceptionExample.java
Quartz提供了二种解决方法
1 立即重新执行任务
2 立即停止所有相关这个任务的触发器
问题2 怎么去执行呢
Quartz的解决方式是
在你的程序出错时,用Quartz提供的JobExecutionException类相关方法很好的解决
1 立即重新执行任务
try { int zero = 0; @SuppressWarnings("unused") int calculation = 4815 / zero; } catch (Exception e) { _log.error("执行任务出错了..."); JobExecutionException e2 = new JobExecutionException(e); // this job will refire immediately e2.setRefireImmediately(true); throw e2; }
请注意其中作者写的注释:
// this job will refire immediately
e2.setRefireImmediately(true);
2 立即停止所有相关这个任务的触发器
try { int zero = 0; @SuppressWarnings("unused") int calculation = 4815 / zero; } catch (Exception e) { _log.info("--- Error in job!"); JobExecutionException e2 = new JobExecutionException(e); // Quartz will automatically unschedule // all triggers associated with this job // so that it does not run again e2.setUnscheduleAllTriggers(true); throw e2; }
请注意其中作者写的注释:
// Quartz will automatically unschedule
// all triggers associated with this job
// so that it does not run again
e2.setUnscheduleAllTriggers(true);
具体代码如下:
BadJob1.java
package org.quartz.examples.example6; import java.util.Date; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.PersistJobDataAfterExecution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * <p> * A job dumb job that will throw a job execution exception * </p> * * @author Bill Kratzer */ @PersistJobDataAfterExecution @DisallowConcurrentExecution public class BadJob1 implements Job { private static Logger _log = LoggerFactory.getLogger(BadJob1.class); public BadJob1() { } public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); _log.error("任务key: " + jobKey + " ,执行时间: " + new Date()); try { int zero = 0; @SuppressWarnings("unused") int calculation = 4815 / zero; } catch (Exception e) { _log.error("执行任务出错了..."); JobExecutionException e2 = new JobExecutionException(e); // this job will refire immediately e2.setRefireImmediately(true); throw e2; } _log.error("---" + jobKey + " completed at " + new Date()); } }
BadJob2 .java
package org.quartz.examples.example6; import java.util.Date; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.PersistJobDataAfterExecution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * <p> * A job dumb job that will throw a job execution exception * </p> * * @author Bill Kratzer */ @PersistJobDataAfterExecution @DisallowConcurrentExecution public class BadJob2 implements Job { private static Logger _log = LoggerFactory.getLogger(BadJob2.class); public BadJob2() { } public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); _log.info("---" + jobKey + " executing at " + new Date()); try { int zero = 0; @SuppressWarnings("unused") int calculation = 4815 / zero; } catch (Exception e) { _log.info("--- Error in job!"); JobExecutionException e2 = new JobExecutionException(e); // Quartz will automatically unschedule // all triggers associated with this job // so that it does not run again e2.setUnscheduleAllTriggers(true); throw e2; } _log.info("---" + jobKey + " completed at " + new Date()); } }
JobExceptionExample.java
package org.quartz.examples.example6; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import static org.quartz.DateBuilder.*; import java.util.Date; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * This job demonstrates how Quartz can handle JobExecutionExceptions that are * thrown by jobs. * * @author Bill Kratzer */ public class JobExceptionExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(JobExceptionExample.class); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); Date startTime = nextGivenSecondDate(null, 15); JobDetail job = newJob(BadJob1.class) .withIdentity("badJob1", "group1") .build(); SimpleTrigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startAt(startTime) .withSchedule(simpleSchedule()) .build(); Date ft = sched.scheduleJob(job, trigger); log.error(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); job = newJob(BadJob2.class) .withIdentity("badJob2", "group1") .build(); trigger = newTrigger() .withIdentity("trigger2", "group1") .startAt(startTime) .withSchedule(simpleSchedule() .withIntervalInSeconds(3) .repeatForever()) .build(); ft = sched.scheduleJob(job, trigger); log.error(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); sched.start(); try { // sleep for 60 seconds Thread.sleep(60L * 1000L); } catch (Exception e) { } sched.shutdown(true); SchedulerMetaData metaData = sched.getMetaData(); log.error("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { JobExceptionExample example = new JobExceptionExample(); example.run(); } }
发表评论
-
java json-lib & jQuery & jsonp
2016-06-30 11:31 831参考链接: 1、http://hanqunfeng.iteye ... -
随机分配算法(Intege)
2016-01-08 13:05 5202参考资料: http://blog.csdn.net/yaer ... -
XSS之xssprotect
2012-12-07 23:03 29495参考资料 1 跨网站脚本 ... -
Java之Thread(线程题)
2011-10-31 12:33 1977特别说明:java线程相关的参考了张孝祥老师的代码 参考资料 ... -
Java之ThreadLocal(Synchronized)
2011-10-31 10:10 2696参考资料 1 深入研究java.lang.ThreadLoca ... -
Java之synchronized,wait,notify结合运用示例
2011-10-26 11:54 5939场景一:要求二个线程各 ... -
Java之synchronized深入应用
2011-10-23 08:03 1949参考资料 1 java synchronized详解 http ... -
Java之Timer入门
2011-10-11 15:32 1699参考资料 1 java中timer用 ... -
Java之线程(匿名内部类)
2011-10-10 16:36 164381 Thread对象 Thread t = new Thr ... -
Java之线程(join)
2011-10-10 15:39 1485运用场景:多个线程之间 ... -
Java之线程(synchronized,wait(),notify(),notifyAll())
2011-10-10 15:03 3256参考资料 Java多线程入门大全(适用于有一定基础者) htt ... -
Java之线程(sleep,线程名)
2011-10-10 13:09 9719参考资料 1 Java线程:创建与启动 http://lava ... -
Java之线程入门
2011-10-10 11:11 1538参考资料 1 Java线程:概 ... -
java之表反向生成类
2011-09-23 16:41 3487环境:XP+JDK1.5+Oracle10g 注意事项 1 请 ... -
java之线程, NIO学习资料
2011-09-19 12:13 1135Concurrent In Java 6 第一部分 集合 ... -
Java之BigDecimal
2011-09-15 10:12 18036参考资料 1 java.math.BigDecimal的用法 ... -
java.lang.OutOfMemoryError异常解决方法
2011-08-27 16:47 2114转载:java.lang.OutOfMemoryError异常 ... -
Jcom之jp.ne.so_net.ga2.no_ji.jcom.JComException: createInstance() failed HRESULT=
2011-08-25 12:33 11748参考资料 java应用jcom将word转pdf http:/ ... -
Quartz之一个任务绑定多个触发器
2011-08-15 16:27 5023参见官方示例: SchedulerFactory sf = ... -
Quartz之QuartzInitializerListener
2011-08-15 15:12 7922问题:我想在WEB容器启动时就执行任务怎么办呢 Quartz: ...
相关推荐
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的任务逻辑 //...
import org.quartz.JobExecutionException; @DisallowConcurrentExecution public class MyTask implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { ...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的定时任务逻辑...
import org.quartz.JobExecutionException; import org.springframework.stereotype.Component; @Component @DisallowConcurrentExecution public class MyJob implements Job { @Override public void execute...
调度器不关注 `execute()` 方法的返回结果,除非 Job 在执行过程中抛出 `org.quartz.JobExecutionException` 异常。 以下是一个简单的 Quartz Job 示例,这个 Job 会扫描指定目录中的文件并打印文件的详细信息: `...
import org.quartz.JobExecutionException; public class FileScannerJob implements Job { private static final Log log = LogFactory.getLog(FileScannerJob.class); @Override public void execute...
import org.quartz.JobExecutionException; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component @DisallowConcurrentExecution @...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 任务逻辑代码 } } ``` ...
import org.quartz.JobExecutionException; public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello,...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写你的任务逻辑 ...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写你的任务逻辑 } } ...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里编写你的任务逻辑 } } ...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你要执行的业务...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("My Job is...
import org.quartz.JobExecutionException; @Component public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 执行你的业务...
import org.quartz.JobExecutionException; public class SimpleJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的任务逻辑...
- 通过实现`org.springframework.scheduling.quartz.JobExecutionException`,可以捕获Job执行过程中的异常,进行统一处理。 8. **事务支持** - 如果Job需要在数据库事务中执行,可以利用Spring的事务管理功能,...
public void execute(JobExecutionContext context) throws JobExecutionException { // 在这里编写你的任务逻辑 } } ``` 3. **定义Trigger** Trigger是触发Job执行的时间规则,可以是简单触发器...
import org.quartz.JobExecutionException; public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 这里写你的任务代码 } } ``...
import org.quartz.JobExecutionException; public class ScanDirectoryJob implements Job { static Log logger = LogFactory.getLog(ScanDirectoryJob.class); public void execute(JobExecutionContext ...