1 17 18 21 package org.quartz.plugins.history; 22 23 import java.text.MessageFormat ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.quartz.JobExecutionContext; 28 import org.quartz.Scheduler; 29 import org.quartz.SchedulerException; 30 import org.quartz.Trigger; 31 import org.quartz.TriggerListener; 32 import org.quartz.spi.SchedulerPlugin; 33 34 212 public class LoggingTriggerHistoryPlugin implements SchedulerPlugin, 213 TriggerListener { 214 215 222 223 private String name; 224 225 private String triggerFiredMessage = "Trigger {1}.{0} fired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}"; 226 227 private String triggerMisfiredMessage = "Trigger {1}.{0} misfired job {6}.{5} at: {4, date, HH:mm:ss MM/dd/yyyy}. Should have fired at: {3, date, HH:mm:ss MM/dd/yyyy}"; 228 229 private String triggerCompleteMessage = "Trigger {1}.{0} completed firing job {6}.{5} at {4, date, HH:mm:ss MM/dd/yyyy} with resulting trigger instruction code: {9}"; 230 231 private final Log log = LogFactory.getLog(getClass()); 232 233 240 241 public LoggingTriggerHistoryPlugin() { 242 } 243 244 251 252 protected Log getLog() { 253 return log; 254 } 255 256 262 public String getTriggerCompleteMessage() { 263 return triggerCompleteMessage; 264 } 265 266 271 public String getTriggerFiredMessage() { 272 return triggerFiredMessage; 273 } 274 275 280 public String getTriggerMisfiredMessage() { 281 return triggerMisfiredMessage; 282 } 283 284 291 public void setTriggerCompleteMessage(String triggerCompleteMessage) { 292 this.triggerCompleteMessage = triggerCompleteMessage; 293 } 294 295 301 public void setTriggerFiredMessage(String triggerFiredMessage) { 302 this.triggerFiredMessage = triggerFiredMessage; 303 } 304 305 311 public void setTriggerMisfiredMessage(String triggerMisfiredMessage) { 312 this.triggerMisfiredMessage = triggerMisfiredMessage; 313 } 314 315 322 323 332 public void initialize(String name, Scheduler scheduler) 333 throws SchedulerException { 334 this.name = name; 335 336 scheduler.addGlobalTriggerListener(this); 337 } 338 339 public void start() { 340 } 342 343 350 public void shutdown() { 351 } 353 354 361 362 369 370 public String getName() { 371 return name; 372 } 373 374 public void triggerFired(Trigger trigger, JobExecutionContext context) { 375 if (!getLog().isInfoEnabled()) { 376 return; 377 } 378 379 Object [] args = { 380 trigger.getName(), trigger.getGroup(), 381 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 382 new java.util.Date (), context.getJobDetail().getName(), 383 context.getJobDetail().getGroup(), 384 new Integer (context.getRefireCount()) 385 }; 386 387 getLog().info(MessageFormat.format(getTriggerFiredMessage(), args)); 388 } 389 390 public void triggerMisfired(Trigger trigger) { 391 if (!getLog().isInfoEnabled()) { 392 return; 393 } 394 395 Object [] args = { 396 trigger.getName(), trigger.getGroup(), 397 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 398 new java.util.Date (), trigger.getJobGroup(), 399 trigger.getJobGroup() 400 }; 401 402 getLog().info(MessageFormat.format(getTriggerMisfiredMessage(), args)); 403 } 404 405 public void triggerComplete(Trigger trigger, JobExecutionContext context, 406 int triggerInstructionCode) { 407 if (!getLog().isInfoEnabled()) { 408 return; 409 } 410 411 String instrCode = "UNKNOWN"; 412 if (triggerInstructionCode == Trigger.INSTRUCTION_DELETE_TRIGGER) { 413 instrCode = "DELETE TRIGGER"; 414 } else if (triggerInstructionCode == Trigger.INSTRUCTION_NOOP) { 415 instrCode = "DO NOTHING"; 416 } else if (triggerInstructionCode == Trigger.INSTRUCTION_RE_EXECUTE_JOB) { 417 instrCode = "RE-EXECUTE JOB"; 418 } else if (triggerInstructionCode == Trigger.INSTRUCTION_SET_ALL_JOB_TRIGGERS_COMPLETE) { 419 instrCode = "SET ALL OF JOB'S TRIGGERS COMPLETE"; 420 } else if (triggerInstructionCode == Trigger.INSTRUCTION_SET_TRIGGER_COMPLETE) { 421 instrCode = "SET THIS TRIGGER COMPLETE"; 422 } 423 424 Object [] args = { 425 trigger.getName(), trigger.getGroup(), 426 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 427 new java.util.Date (), context.getJobDetail().getName(), 428 context.getJobDetail().getGroup(), 429 new Integer (context.getRefireCount()), 430 new Integer (triggerInstructionCode), instrCode 431 }; 432 433 getLog().info(MessageFormat.format(getTriggerCompleteMessage(), args)); 434 } 435 436 public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { 437 return false; 438 } 439 440 } 441 | Popular Tags |