Guest User

Untitled

a guest
Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. package com.lashpoint.quartz;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5.  
  6. import org.quartz.JobExecutionContext;
  7. import org.quartz.JobExecutionException;
  8. import org.quartz.SchedulerException;
  9. import org.quartz.SimpleTrigger;
  10.  
  11. import com.lashpoint.twitter.quartz.RescheduleException;
  12.  
  13. public abstract class RetriableTrigger extends SimpleTrigger {
  14. /**
  15. *
  16. */
  17. private static final long serialVersionUID = 2986291750485640660L;
  18.  
  19. private static final int DELAY_CEILING = 600; // 15 minutes
  20. private static final int DELAY_EXP_BASE = 2; // Delays will be a power of 2
  21.  
  22. public RetriableTrigger(String name, Date startDate) {
  23. super(name, startDate, null, 0, 0);
  24. }
  25.  
  26. @Override
  27. public boolean hasAdditionalProperties() {
  28. return true;
  29. }
  30.  
  31. @Override
  32. public int executionComplete(JobExecutionContext context,
  33. JobExecutionException result) {
  34. if (shouldReschedule(context, result)) {
  35. try {
  36. RetriableTrigger nextTrigger = createTriggerForReschedule(context,
  37. result);
  38. if (nextTrigger != null) {
  39. context.getScheduler().rescheduleJob(getName(), getGroup(),
  40. nextTrigger);
  41.  
  42. return INSTRUCTION_NOOP;
  43. }
  44. }
  45. catch (SchedulerException e) {
  46. throw new RescheduleException(e);
  47. }
  48. }
  49.  
  50. return INSTRUCTION_DELETE_TRIGGER;
  51. }
  52.  
  53. protected abstract boolean shouldReschedule(JobExecutionContext context,
  54. JobExecutionException result);
  55.  
  56. protected RetriableTrigger createTriggerForReschedule(JobExecutionContext context,
  57. JobExecutionException result) {
  58. try {
  59. RetriableTrigger trigger = getClass().newInstance();
  60.  
  61. Calendar nextFireTime = Calendar.getInstance();
  62. nextFireTime.add(Calendar.SECOND, Math.min(DELAY_CEILING,
  63. (int)Math.pow(DELAY_EXP_BASE, getTimesTriggered())));
  64.  
  65. trigger.setStartTime(nextFireTime.getTime());
  66. trigger.setJobName(getJobName());
  67. trigger.setJobGroup(getJobGroup());
  68. trigger.setTimesTriggered(getTimesTriggered());
  69.  
  70. return trigger;
  71. }
  72. catch (InstantiationException e) {
  73. throw new RescheduleException(e);
  74. }
  75. catch (IllegalAccessException e) {
  76. throw new RescheduleException(e);
  77. }
  78. }
  79. }
Add Comment
Please, Sign In to add comment