Пример #1
0
 /** Initialisation. */
 @Before
 public void setUp() {
   Utils.initialize();
   javaInformationsList = Collections.singletonList(new JavaInformations(null, true));
   sqlCounter = new Counter("sql", "db.png");
   sqlCounter.setDisplayed(false);
   servicesCounter = new Counter("services", "beans.png", sqlCounter);
   jspCounter = new Counter(Counter.JSP_COUNTER_NAME, null);
   // counterName doit être http, sql ou ejb pour que les libellés de graph soient trouvés dans les
   // traductions
   counter = new Counter("http", "dbweb.png", sqlCounter);
   errorCounter = new Counter(Counter.ERROR_COUNTER_NAME, null);
   final Counter jobCounter = JobGlobalListener.getJobCounter();
   collector =
       new Collector(
           "test",
           Arrays.asList(
               counter, sqlCounter, servicesCounter, jspCounter, errorCounter, jobCounter));
   writer = new StringWriter();
 }
Пример #2
0
  /**
   * Test.
   *
   * @throws IOException e
   * @throws SchedulerException e
   */
  @Test
  public void testJob() throws IOException, SchedulerException {
    // job quartz
    JobGlobalListener.initJobGlobalListener();
    JobGlobalListener.getJobCounter().clear();

    // Grab the Scheduler instance from the Factory
    final Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

    try {
      // and start it off
      scheduler.start();

      final Random random = new Random();

      // Define a Trigger that will fire "later"
      final JobDetail job2 = new JobDetail("job" + random.nextInt(), null, JobTestImpl.class);
      final SimpleTrigger trigger2 =
          new SimpleTrigger(
              "trigger" + random.nextInt(), null, new Date(System.currentTimeMillis() + 60000));
      trigger2.setRepeatInterval(2 * 24L * 60 * 60 * 1000);
      scheduler.scheduleJob(job2, trigger2);
      scheduler.pauseJob(job2.getName(), job2.getGroup());
      try {
        final JobDetail job3 = new JobDetail("job" + random.nextInt(), null, JobTestImpl.class);
        // cron trigger that will never fire
        final Trigger trigger3 =
            new CronTrigger("crontrigger" + random.nextInt(), null, "0 0 0 * * ? 2030");
        scheduler.scheduleJob(job3, trigger3);

        // other trigger that will never fire
        final NthIncludedDayTrigger trigger4 =
            new NthIncludedDayTrigger("nth trigger" + random.nextInt(), null);
        trigger4.setN(1);
        trigger4.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_YEARLY);
        trigger4.setJobName(job3.getName());
        scheduler.scheduleJob(trigger4);
      } catch (final ParseException e) {
        throw new IllegalStateException(e);
      }

      // JavaInformations doit être réinstancié pour récupérer les jobs
      // (mais "Aucun job" dans le counter)
      final List<JavaInformations> javaInformationsList2 =
          Collections.singletonList(new JavaInformations(null, true));
      final HtmlReport htmlReport =
          new HtmlReport(collector, null, javaInformationsList2, Period.TOUT, writer);
      htmlReport.toHtml(null, null);
      assertNotEmptyAndClear(writer);

      // on lance 10 jobs pour être à peu près sûr qu'il y en a un qui fait une erreur
      // (aléatoirement il y en a 2/10 qui font une erreur)
      final Map<JobDetail, SimpleTrigger> triggersByJob =
          new LinkedHashMap<JobDetail, SimpleTrigger>();
      for (int i = 0; i < 10; i++) {
        // Define a Trigger that will fire "now"
        final JobDetail job = new JobDetail("job" + random.nextInt(), null, JobTestImpl.class);
        job.setDescription("description");

        final SimpleTrigger trigger =
            new SimpleTrigger("trigger" + random.nextInt(), null, new Date());
        // Schedule the job with the trigger
        scheduler.scheduleJob(job, trigger);
        triggersByJob.put(job, trigger);
      }
      // JobTestImpl fait un sleep de 2s au plus, donc on attend les jobs pour les compter
      try {
        Thread.sleep(3000);
      } catch (final InterruptedException e) {
        throw new IllegalStateException(e);
      }

      for (final Map.Entry<JobDetail, SimpleTrigger> entry : triggersByJob.entrySet()) {
        // et on les relance pour qu'ils soient en cours
        entry.getValue().setRepeatInterval(60000);
        scheduler.scheduleJob(entry.getKey(), entry.getValue());
      }

      // JavaInformations doit être réinstancié pour récupérer les jobs
      setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, Boolean.FALSE.toString());
      final List<JavaInformations> javaInformationsList3 =
          Collections.singletonList(new JavaInformations(null, true));
      final HtmlReport htmlReport3 =
          new HtmlReport(collector, null, javaInformationsList3, Period.TOUT, writer);
      htmlReport3.toHtml(null, null);
      assertNotEmptyAndClear(writer);
    } finally {
      scheduler.shutdown();
      JobGlobalListener.getJobCounter().clear();
      JobGlobalListener.destroyJobGlobalListener();
    }
  }