Пример #1
0
  @Override
  public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception {
    // If Camel has already started and then user add a route dynamically, we need to ensure
    // to create and init the scheduler first.
    if (scheduler == null) {
      createAndInitScheduler();
    }

    // Now scheduler is ready, let see how we should start it.
    if (!autoStartScheduler) {
      LOG.info("Not starting scheduler because autoStartScheduler is set to false.");
    } else {
      if (startDelayedSeconds > 0) {
        if (scheduler.isStarted()) {
          LOG.warn(
              "The scheduler has already started. Cannot apply the 'startDelayedSeconds' configuration!");
        } else {
          LOG.info("Starting scheduler with startDelayedSeconds={}", startDelayedSeconds);
          scheduler.startDelayed(startDelayedSeconds);
        }
      } else {
        if (scheduler.isStarted()) {
          LOG.info("The scheduler has already been started.");
        } else {
          LOG.info("Starting scheduler.");
          scheduler.start();
        }
      }
    }
  }
Пример #2
0
 @Override
 public void afterPropertiesSet() throws Exception {
   if (scheduler == null) {
     scheduler = StdSchedulerFactory.getDefaultScheduler();
   }
   if (!scheduler.isStarted()) {
     scheduler.start();
   }
   LOG.info("Scheduler started:" + scheduler.isStarted());
   initJobs();
 }
Пример #3
0
  public void init() {

    try {
      schedFact = new org.quartz.impl.StdSchedulerFactory();
      sched = schedFact.getScheduler();
      if (sched.isStarted() == false) sched.start();

      MongoCollection<BsonDocument> collection =
          Configuration.mongoDatabase.getCollection("Subscription", BsonDocument.class);

      Iterator<BsonDocument> subIterator = collection.find(BsonDocument.class).iterator();
      MongoQueryService queryService = new MongoQueryService();
      while (subIterator.hasNext()) {
        BsonDocument sub = subIterator.next();
        SubscriptionType subscription = new SubscriptionType(sub);
        if (subscription.getSchedule() != null && subscription.getTrigger() == null) {
          queryService.addScheduleToQuartz(subscription);
        } else if (subscription.getSchedule() == null && subscription.getTrigger() != null) {
          TriggerEngine.addTriggerSubscription(
              sub.getString("subscriptionID").getValue(), subscription);
        }
      }

    } catch (SchedulerException e) {
      Configuration.logger.log(Level.ERROR, e.toString());
    }
  }
  @Override
  protected Integer doExecute() throws Exception {
    final Map<String, Scheduler> schedulerMap =
        (Map<String, Scheduler>) appCtx.getBean("schedulerMap");
    System.out.println(
        ansi()
            .render(
                "@|negative_on %3s|%-20s|%-15s|%-3s|%-3s|%-3s|%-25s|@",
                "№", "Name/Tenant", "Instance ID", "Cur", "Job", "Trg", "Store"));
    int i = 0;
    for (Entry<String, Scheduler> entry : schedulerMap.entrySet()) {
      //			final String tenantId = entry.getKey();
      final Scheduler scheduler = entry.getValue();
      final Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.anyJobGroup());
      final List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
      final Set<TriggerKey> triggerKeys = scheduler.getTriggerKeys(GroupMatcher.anyTriggerGroup());

      final String startedAnsi = scheduler.isStarted() ? "@|bold,green ▶|@‎" : "@|bold,red ◼|@";
      System.out.println(
          ansi()
              .render(
                  "@|bold,black %3d||@"
                      + startedAnsi
                      + "%-19s@|bold,black ||@%-15s@|bold,black ||@%3d@|bold,black ||@%3d@|bold,black ||@%3d@|bold,black ||@%-25s",
                  ++i,
                  scheduler.getSchedulerName(),
                  scheduler.getSchedulerInstanceId(),
                  executingJobs.size(),
                  jobKeys.size(),
                  triggerKeys.size(),
                  scheduler.getMetaData().getJobStoreClass().getSimpleName()));
    }
    System.out.println(ansi().render("@|bold,yellow %d|@ Schedulers", schedulerMap.size()));
    return schedulerMap.size();
  }
  /** 启动定时调度程序 */
  public synchronized void start() {
    if (started) {
      return;
    }
    try {
      Scheduler scheduler = this.getSyncScheduler();
      if (!scheduler.isStarted()) {
        scheduler.start();
      }

      CronTrigger trigger = new CronTrigger();
      trigger.setName(this.getName() + "SynchronizerTrigger");
      trigger.setCronExpression("0/" + this.repeatInterval + " * * * * ?");
      trigger.setGroup(this.getName() + "SynchronizerGroup");

      String jobDetailName = this.getName() + "SynchronizeJob";
      String jobDetailGroup = this.getName() + "SynchronizeSchduler";
      JobDetail job = new JobDetail(jobDetailName, jobDetailGroup, this.getClass());
      job.getJobDataMap().put(JobDataMapKey.RUN_OBJECT, this);
      job.getJobDataMap().put(JobDataMapKey.RUN_MAGIC, appName);

      JobDetail j = scheduler.getJobDetail(jobDetailName, jobDetailGroup);
      if (j == null) {
        scheduler.scheduleJob(job, trigger);
        logger.info(this.getName() + "SynchronizeJob started");
      } else {
        logger.warn("{}SynchronizeJob already started.", this.getName());
      }
    } catch (Exception e) {
      logger.error("启动时间程序同步程序失败: ", e);
    }
  }
Пример #6
0
  public boolean stop() {
    try {
      if (scheduler != null && scheduler.isStarted()) {

        // This is to immediately stop the scheduler to avoid firing new services
        scheduler.standby();

        if (logger.isDebugEnabled()) {
          logger.debug("ShuttingDown Message Processor Scheduler : " + scheduler.getMetaData());
        }

        try {
          scheduler.interrupt(
              new JobKey(
                  name + "-job", MessageProcessorConstants.SCHEDULED_MESSAGE_PROCESSOR_GROUP));
        } catch (UnableToInterruptJobException e) {
          logger.info("Unable to interrupt job [" + name + "-job]");
        }

        // gracefully shutdown
        scheduler.shutdown(true);
      }

    } catch (SchedulerException e) {
      throw new SynapseException("Error ShuttingDown Message processor scheduler ", e);
    }

    if (logger.isDebugEnabled()) {
      logger.debug("Stopped message processor [" + getName() + "].");
    }

    return true;
  }
Пример #7
0
 /**
  * Check if the scheduler is started.
  *
  * @throws JobSchedulerException thrown if the scheduler status cannot be checked
  */
 public boolean isStarted() throws JobSchedulerException {
   try {
     return scheduler.isStarted();
   } catch (SchedulerException e) {
     throw new JobSchedulerException(
         "An exception occurred during checking if the scheduler is started", e);
   }
 }
Пример #8
0
 /**
  * Description:启动调度器
  *
  * @return true:启动成功|false:启动失败
  * @author lijw
  * @since 2013-5-31
  */
 public boolean startup() {
   try {
     if (scheduler.isStarted()) {
       return true;
     } else {
       scheduler.start();
       return true;
     }
   } catch (SchedulerException e) {
     logger.error("调度器启动异常:" + e.getMessage());
     return false;
   }
 }
Пример #9
0
  public boolean deactivate() {
    try {
      if (scheduler != null && scheduler.isStarted()) {
        if (logger.isDebugEnabled()) {
          logger.debug("Deactivating message processor [" + getName() + "]");
        }

        // This is to immediately stop the scheduler to avoid firing new services
        scheduler.standby();

        try {
          scheduler.interrupt(
              new JobKey(
                  name + "-job", MessageProcessorConstants.SCHEDULED_MESSAGE_PROCESSOR_GROUP));
        } catch (UnableToInterruptJobException e) {
          logger.info("Unable to interrupt job [" + name + "-job]");
        }

        // This is to remove the consumer from the queue.
        messageConsumer.cleanup();

        if (logger.isDebugEnabled()) {
          logger.debug("Successfully deactivated the message processor [" + getName() + "]");
        }

        setActivated(isActive());

        // This means the deactivation has happened automatically. So we have to persist the
        // deactivation manually.
        if (isPaused()) {
          try {
            // TODO: Need to make sure if this is the best way.
            String directory = configuration.getPathToConfigFile() + "/message-processors";
            DeploymentEngine deploymentEngine =
                (DeploymentEngine) configuration.getAxisConfiguration().getConfigurator();
            MessageProcessorDeployer dep =
                (MessageProcessorDeployer) deploymentEngine.getDeployer(directory, "xml");
            dep.restoreSynapseArtifact(name);
          } catch (Exception e) {
            logger.warn("Couldn't persist the state of the message processor [" + name + "]");
          }
        }

        return true;
      } else {
        return false;
      }
    } catch (SchedulerException e) {
      throw new SynapseException("Error Standing-by Message processor scheduler ", e);
    }
  }
Пример #10
0
 /**
  * Description:判断调度器状态
  *
  * @return 0:已关闭|1:已启动|-1:状态异常,可能未初始化
  * @author 李降伟
  * @since 2013-5-31
  */
 public int getState() {
   try {
     if (scheduler.isStarted()) {
       return 1; // 已启动
     } else if (scheduler.isShutdown()) {
       return 0; // 已关闭
     } else {
       return -1; // 未实现化
     }
   } catch (SchedulerException e) {
     logger.error("调度器状态获取异常:" + e.getMessage());
     return -1; // 异常
   }
 }