1 17 18 21 package org.quartz.plugins.history; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.quartz.JobExecutionContext; 26 import org.quartz.JobExecutionException; 27 import org.quartz.Scheduler; 28 import org.quartz.SchedulerException; 29 import org.quartz.Trigger; 30 import org.quartz.JobListener; 31 import org.quartz.spi.SchedulerPlugin; 32 33 import java.text.MessageFormat ; 34 35 275 public class LoggingJobHistoryPlugin implements SchedulerPlugin, JobListener { 276 277 284 285 private String name; 286 287 private String jobToBeFiredMessage = "Job {1}.{0} fired (by trigger {4}.{3}) at: {2, date, HH:mm:ss MM/dd/yyyy}"; 288 289 private String jobSuccessMessage = "Job {1}.{0} execution complete at {2, date, HH:mm:ss MM/dd/yyyy} and reports: {8}"; 290 291 private String jobFailedMessage = "Job {1}.{0} execution failed at {2, date, HH:mm:ss MM/dd/yyyy} and reports: {8}"; 292 293 private String jobWasVetoedMessage = "Job {1}.{0} was vetoed. It was to be fired (by trigger {4}.{3}) at: {2, date, HH:mm:ss MM/dd/yyyy}"; 294 295 private final Log log = LogFactory.getLog(getClass()); 296 297 304 305 public LoggingJobHistoryPlugin() { 306 } 307 308 315 316 protected Log getLog() { 317 return log; 318 } 319 320 324 public String getJobSuccessMessage() { 325 return jobSuccessMessage; 326 } 327 328 332 public String getJobFailedMessage() { 333 return jobFailedMessage; 334 } 335 336 339 public String getJobToBeFiredMessage() { 340 return jobToBeFiredMessage; 341 } 342 343 350 public void setJobSuccessMessage(String jobSuccessMessage) { 351 this.jobSuccessMessage = jobSuccessMessage; 352 } 353 354 361 public void setJobFailedMessage(String jobFailedMessage) { 362 this.jobFailedMessage = jobFailedMessage; 363 } 364 365 371 public void setJobToBeFiredMessage(String jobToBeFiredMessage) { 372 this.jobToBeFiredMessage = jobToBeFiredMessage; 373 } 374 375 379 public String getJobWasVetoedMessage() { 380 return jobWasVetoedMessage; 381 } 382 383 390 public void setJobWasVetoedMessage(String jobWasVetoedMessage) { 391 this.jobWasVetoedMessage = jobWasVetoedMessage; 392 } 393 394 401 402 411 public void initialize(String name, Scheduler scheduler) 412 throws SchedulerException { 413 this.name = name; 414 scheduler.addGlobalJobListener(this); 415 } 416 417 public void start() { 418 } 420 421 428 public void shutdown() { 429 } 431 432 439 440 447 448 public String getName() { 449 return name; 450 } 451 452 455 public void jobToBeExecuted(JobExecutionContext context) { 456 if (!getLog().isInfoEnabled()) { 457 return; 458 } 459 460 Trigger trigger = context.getTrigger(); 461 462 Object [] args = { 463 context.getJobDetail().getName(), 464 context.getJobDetail().getGroup(), new java.util.Date (), 465 trigger.getName(), trigger.getGroup(), 466 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 467 new Integer (context.getRefireCount()) 468 }; 469 470 getLog().info(MessageFormat.format(getJobToBeFiredMessage(), args)); 471 } 472 473 476 public void jobWasExecuted(JobExecutionContext context, 477 JobExecutionException jobException) { 478 479 Trigger trigger = context.getTrigger(); 480 481 Object [] args = null; 482 483 if (jobException != null) { 484 if (!getLog().isWarnEnabled()) { 485 return; 486 } 487 488 String errMsg = jobException.getMessage(); 489 args = 490 new Object [] { 491 context.getJobDetail().getName(), 492 context.getJobDetail().getGroup(), new java.util.Date (), 493 trigger.getName(), trigger.getGroup(), 494 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 495 new Integer (context.getRefireCount()), errMsg 496 }; 497 498 getLog().warn(MessageFormat.format(getJobFailedMessage(), args), jobException); 499 } else { 500 if (!getLog().isInfoEnabled()) { 501 return; 502 } 503 504 String result = String.valueOf(context.getResult()); 505 args = 506 new Object [] { 507 context.getJobDetail().getName(), 508 context.getJobDetail().getGroup(), new java.util.Date (), 509 trigger.getName(), trigger.getGroup(), 510 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 511 new Integer (context.getRefireCount()), result 512 }; 513 514 getLog().info(MessageFormat.format(getJobSuccessMessage(), args)); 515 } 516 } 517 518 521 public void jobExecutionVetoed(JobExecutionContext context) { 522 523 if (!getLog().isInfoEnabled()) { 524 return; 525 } 526 527 Trigger trigger = context.getTrigger(); 528 529 Object [] args = { 530 context.getJobDetail().getName(), 531 context.getJobDetail().getGroup(), new java.util.Date (), 532 trigger.getName(), trigger.getGroup(), 533 trigger.getPreviousFireTime(), trigger.getNextFireTime(), 534 new Integer (context.getRefireCount()) 535 }; 536 537 getLog().info(MessageFormat.format(getJobWasVetoedMessage(), args)); 538 } 539 540 } 541 542 | Popular Tags |