org.quartz.Scheduler#setJobFactory ( )源码实例Demo

下面列出了org.quartz.Scheduler#setJobFactory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: attic-aurora   文件: CronModule.java
@Provides
@Singleton
Scheduler provideScheduler(AuroraCronJobFactory jobFactory) throws SchedulerException {
  // There are several ways to create a quartz Scheduler instance.  This path was chosen as the
  // simplest to create a Scheduler that uses a *daemon* QuartzSchedulerThread instance.
  Properties props = new Properties();
  String name = "aurora-cron-" + ID_GENERATOR.incrementAndGet();
  props.setProperty(PROP_SCHED_NAME, name);
  props.setProperty(PROP_SCHED_INSTANCE_ID, name);
  props.setProperty(PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getCanonicalName());
  props.setProperty(
      PROP_THREAD_POOL_PREFIX + ".threadCount",
      String.valueOf(options.cronSchedulerNumThreads));
  props.setProperty(PROP_THREAD_POOL_PREFIX + ".makeThreadsDaemons", Boolean.TRUE.toString());

  props.setProperty(PROP_SCHED_MAKE_SCHEDULER_THREAD_DAEMON, Boolean.TRUE.toString());
  Scheduler scheduler = new StdSchedulerFactory(props).getScheduler();
  scheduler.setJobFactory(jobFactory);
  return scheduler;
}
 
private Scheduler prepareScheduler(SchedulerFactory schedulerFactory) throws SchedulerException {
	if (this.resourceLoader != null) {
		// Make given ResourceLoader available for SchedulerFactory configuration.
		configTimeResourceLoaderHolder.set(this.resourceLoader);
	}
	if (this.taskExecutor != null) {
		// Make given TaskExecutor available for SchedulerFactory configuration.
		configTimeTaskExecutorHolder.set(this.taskExecutor);
	}
	if (this.dataSource != null) {
		// Make given DataSource available for SchedulerFactory configuration.
		configTimeDataSourceHolder.set(this.dataSource);
	}
	if (this.nonTransactionalDataSource != null) {
		// Make given non-transactional DataSource available for SchedulerFactory configuration.
		configTimeNonTransactionalDataSourceHolder.set(this.nonTransactionalDataSource);
	}

	// Get Scheduler instance from SchedulerFactory.
	try {
		Scheduler scheduler = createScheduler(schedulerFactory, this.schedulerName);
		populateSchedulerContext(scheduler);

		if (!this.jobFactorySet && !(scheduler instanceof RemoteScheduler)) {
			// Use AdaptableJobFactory as default for a local Scheduler, unless when
			// explicitly given a null value through the "jobFactory" bean property.
			this.jobFactory = new AdaptableJobFactory();
		}
		if (this.jobFactory != null) {
			if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) {
				((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);
			}
			if (this.jobFactory instanceof SchedulerContextAware) {
				((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext());
			}
			scheduler.setJobFactory(this.jobFactory);
		}
		return scheduler;
	}

	finally {
		if (this.resourceLoader != null) {
			configTimeResourceLoaderHolder.remove();
		}
		if (this.taskExecutor != null) {
			configTimeTaskExecutorHolder.remove();
		}
		if (this.dataSource != null) {
			configTimeDataSourceHolder.remove();
		}
		if (this.nonTransactionalDataSource != null) {
			configTimeNonTransactionalDataSourceHolder.remove();
		}
	}
}
 
private Scheduler prepareScheduler(SchedulerFactory schedulerFactory) throws SchedulerException {
	if (this.resourceLoader != null) {
		// Make given ResourceLoader available for SchedulerFactory configuration.
		configTimeResourceLoaderHolder.set(this.resourceLoader);
	}
	if (this.taskExecutor != null) {
		// Make given TaskExecutor available for SchedulerFactory configuration.
		configTimeTaskExecutorHolder.set(this.taskExecutor);
	}
	if (this.dataSource != null) {
		// Make given DataSource available for SchedulerFactory configuration.
		configTimeDataSourceHolder.set(this.dataSource);
	}
	if (this.nonTransactionalDataSource != null) {
		// Make given non-transactional DataSource available for SchedulerFactory configuration.
		configTimeNonTransactionalDataSourceHolder.set(this.nonTransactionalDataSource);
	}

	// Get Scheduler instance from SchedulerFactory.
	try {
		Scheduler scheduler = createScheduler(schedulerFactory, this.schedulerName);
		populateSchedulerContext(scheduler);

		if (!this.jobFactorySet && !(scheduler instanceof RemoteScheduler)) {
			// Use AdaptableJobFactory as default for a local Scheduler, unless when
			// explicitly given a null value through the "jobFactory" bean property.
			this.jobFactory = new AdaptableJobFactory();
		}
		if (this.jobFactory != null) {
			if (this.applicationContext != null && this.jobFactory instanceof ApplicationContextAware) {
				((ApplicationContextAware) this.jobFactory).setApplicationContext(this.applicationContext);
			}
			if (this.jobFactory instanceof SchedulerContextAware) {
				((SchedulerContextAware) this.jobFactory).setSchedulerContext(scheduler.getContext());
			}
			scheduler.setJobFactory(this.jobFactory);
		}
		return scheduler;
	}

	finally {
		if (this.resourceLoader != null) {
			configTimeResourceLoaderHolder.remove();
		}
		if (this.taskExecutor != null) {
			configTimeTaskExecutorHolder.remove();
		}
		if (this.dataSource != null) {
			configTimeDataSourceHolder.remove();
		}
		if (this.nonTransactionalDataSource != null) {
			configTimeNonTransactionalDataSourceHolder.remove();
		}
	}
}
 
源代码4 项目: nexus-public   文件: QuartzSchedulerProvider.java
private Scheduler createScheduler() {
  try {
    // ensure executed threads have TCCL set
    ThreadExecutor threadExecutor = new DefaultThreadExecutor()
    {
      @Override
      public void execute(final Thread thread) {
        thread.setContextClassLoader(QuartzSchedulerProvider.class.getClassLoader());
        super.execute(thread);
      }
    };

    // create Scheduler (implicitly registers it with repository)
    DirectSchedulerFactory.getInstance().createScheduler(
        SCHEDULER_NAME,
        nodeAccess.getId(), // instance-id
        new QuartzThreadPool(threadPoolSize, threadPriority),
        threadExecutor,
        jobStore.get(),
        null, // scheduler plugin-map
        null, // rmi-registry host
        0,    // rmi-registry port
        -1,   // idle-wait time
        -1,   // db-failure retry-interval
        true, // jmx-export
        null, // custom jmx object-name, lets use the default
        1,    // max batch-size
        0L    // batch time-window
    );
    Scheduler s = DirectSchedulerFactory.getInstance().getScheduler(SCHEDULER_NAME);
    s.setJobFactory(jobFactory);

    // re-logging with version, as by default we limit quartz logging to WARN, hiding its default version logging
    log.info("Quartz Scheduler v{}", s.getMetaData().getVersion());

    s.standby();

    return s;
  }
  catch (Exception e) {
    throw new RuntimeException(e);
  }
}