Beispiel #1
0
  @Override
  protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {

    // The job data map is available through the JobExecutionContext
    // (passed to you at execution time)
    JobDataMap jobDataMap = ctx.getJobDetail().getJobDataMap();

    try {
      // Retrieve the last date when the job was run
      Date lastDateRun = ctx.getPreviousFireTime();

      // Job was run previously
      if (lastDateRun != null) {
        //				logger.debug("Last date run: " + sdf.format(lastDateRun));
        System.out.println("Last date run: " + sdf.format(lastDateRun));
        // Retrieve the number of times this job has been attempted
        int refireCount = ctx.getRefireCount();

        if (refireCount > 0) {
          //					logger.debug("Total attempts: " + refireCount);
          System.out.println("Total attempts: " + refireCount);
        }
      } else {
        // Job is run for the first time
        //				logger.debug("Job is run for the first time");
        System.out.println("Job is run for the first time");
      }

      // Do the actual work
      //			logger.debug("Delegating work to worker");
      System.out.println("Delegating work to worker");
      worker.work();

      // Retrieve the next date when the job will be run
      String nextDateRun = sdf.format(ctx.getNextFireTime());

      //			logger.debug("Next date run: " + nextDateRun);
      System.out.println("Next date run: " + nextDateRun);

    } catch (Exception e) {
      //			logger.error("Unexpected exception" , e);
      e.printStackTrace();
      throw new JobExecutionException("Unexpected exception", e, true);
    }
  }
  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    try {

      log.info(
          "Execute: Details : "
              + " at : "
              + new Date()
              + " : Previous Fire time : "
              + context.getPreviousFireTime()
              + " : Next Fire time : "
              + context.getNextFireTime()
              + " : Scheduled Fire time : "
              + context.getScheduledFireTime()
              + " : Fire Instance ID : "
              + context.getFireInstanceId()
              + " : Job Run Time : "
              + context.getJobRunTime()
              + " : Refire Count : "
              + context.getRefireCount()
              + " : Fire Time : "
              + context.getFireTime());

      String fileName = context.getTrigger().getKey().getName();
      File file = new File(fileName + ".txt");

      // if file doesnt exists, then create it
      if (!file.exists()) {
        file.createNewFile();
      }

      // true = append file
      FileWriter fileWritter = new FileWriter(file.getName(), true);
      BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
      bufferWritter.write(new Date().toString() + "\n");
      bufferWritter.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }