下面列出了怎么用org.quartz.simpl.SimpleThreadPool的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
// 设置数据源
DataSource job = dynamicRoutingDataSource.getDataSource("job");
factory.setDataSource(job);
Properties prop = new Properties();
// 任务调度实例名称,集群时多个实例名称保持一致
prop.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "FebsCloudScheduler");
// 任务调度实例ID,指定为AUTO时,将自动生成ID
prop.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_ID, StdSchedulerFactory.AUTO_GENERATE_INSTANCE_ID);
// quartz提供的简单线程池,适用于绝大部分场景
prop.put(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class);
// 并发执行任务的线程数,取决于服务器系统资源
prop.put("org.quartz.threadPool.threadCount", "20");
// 可以是Thread.MIN_PRIORITY(1)和Thread.MAX_PRIORITY(10)之间的任何int值 。
// 默认值为Thread.NORM_PRIORITY(5)
prop.put("org.quartz.threadPool.threadPriority", "5");
// 指定任务存储策略,这里使用关系型数据库
prop.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, JobStoreTX.class);
// 是否开启集群
prop.put("org.quartz.jobStore.isClustered", "true");
// 集群中任务调度实例失效的检查时间间隔,单位为毫秒
prop.put("org.quartz.jobStore.clusterCheckinInterval", "15000");
prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
prop.put("org.quartz.jobStore.misfireThreshold", "12000");
// 数据表前缀
prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
factory.setQuartzProperties(prop);
factory.setSchedulerName("FEBS_Cloud_Scheduler");
// 延时启动
factory.setStartupDelay(1);
factory.setApplicationContextSchedulerContextKey("applicationContextKey");
// 启动时更新己存在的 Job
factory.setOverwriteExistingJobs(true);
// 设置自动启动,默认为 true
factory.setAutoStartup(true);
return factory;
}
@BuildStep
public List<LogCleanupFilterBuildItem> logCleanup(QuartzBuildTimeConfig config) {
List<LogCleanupFilterBuildItem> logCleanUps = new ArrayList<>();
logCleanUps.add(new LogCleanupFilterBuildItem(StdSchedulerFactory.class.getName(),
"Quartz scheduler version:",
"Using default implementation for",
"Quartz scheduler 'QuarkusQuartzScheduler'"));
logCleanUps.add(new LogCleanupFilterBuildItem(org.quartz.core.QuartzScheduler.class.getName(),
"Quartz Scheduler v",
"JobFactory set to:",
"Scheduler meta-data:",
"Scheduler QuarkusQuartzScheduler"));
logCleanUps.add(new LogCleanupFilterBuildItem(config.storeType.clazz, config.storeType.simpleName
+ " initialized.", "Handling", "Using db table-based data access locking",
"JDBCJobStore threads will inherit ContextClassLoader of thread",
"Couldn't rollback jdbc connection", "Database connection shutdown unsuccessful"));
logCleanUps.add(new LogCleanupFilterBuildItem(SchedulerSignalerImpl.class.getName(),
"Initialized Scheduler Signaller of type"));
logCleanUps.add(new LogCleanupFilterBuildItem(QuartzSchedulerThread.class.getName(),
"QuartzSchedulerThread Inheriting ContextClassLoader"));
logCleanUps.add(new LogCleanupFilterBuildItem(SimpleThreadPool.class.getName(),
"Job execution threads will use class loader of thread"));
logCleanUps.add(new LogCleanupFilterBuildItem(AttributeRestoringConnectionInvocationHandler.class.getName(),
"Failed restore connection's original"));
return logCleanUps;
}
private Properties getQuartzProperties() {
Properties result = new Properties();
result.put("org.quartz.threadPool.class", SimpleThreadPool.class.getName());
result.put("org.quartz.threadPool.threadCount", Integer.toString(Runtime.getRuntime().availableProcessors() * 2));
result.put("org.quartz.scheduler.instanceName", "ELASTIC_JOB_CLOUD_TRANSIENT_PRODUCER");
result.put("org.quartz.plugin.shutdownhook.class", ShutdownHookPlugin.class.getName());
result.put("org.quartz.plugin.shutdownhook.cleanShutdown", Boolean.TRUE.toString());
return result;
}
private Properties getQuartzProperties() {
Properties result = new Properties();
result.put("org.quartz.threadPool.class", SimpleThreadPool.class.getName());
result.put("org.quartz.threadPool.threadCount", Integer.toString(1));
result.put("org.quartz.scheduler.instanceName", "ELASTIC_JOB_CLOUD_STATISTICS_SCHEDULER");
result.put("org.quartz.plugin.shutdownhook.class", ShutdownHookPlugin.class.getName());
result.put("org.quartz.plugin.shutdownhook.cleanShutdown", Boolean.TRUE.toString());
return result;
}
/**
* Creates an in memory job store (<code>{@link RAMJobStore}</code>)
* The thread priority is set to Thread.NORM_PRIORITY
*
* @param maxThreads
* The number of threads in the thread pool
* @throws SchedulerException
* if initialization failed.
*/
public void createVolatileScheduler(int maxThreads)
throws SchedulerException {
SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads,
Thread.NORM_PRIORITY);
threadPool.initialize();
JobStore jobStore = new RAMJobStore();
this.createScheduler(threadPool, jobStore);
}
private void initDetectionScheduler() throws Exception{
StdSchedulerFactory factory=new StdSchedulerFactory();
Properties mergedProps = new Properties();
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
mergedProps.setProperty("org.quartz.scheduler.makeSchedulerThreadDaemon", "true");
mergedProps.setProperty("org.quartz.scheduler.instanceName", "UfloClusterHeartbeatScheduler");
mergedProps.setProperty("org.quartz.scheduler.instanceId", "UfloHeartbeatDetectionScheduler");
mergedProps.setProperty("org.quartz.threadPool.threadCount","2");
factory.initialize(mergedProps);
scheduler=factory.getScheduler();
}
@Before
public void before() throws Exception {
try (ODatabaseDocumentTx db = database.getInstance().acquire()) {
OrientQuartzSchema.register(db);
dbUrl = db.getURL();
}
SimpleThreadPool threadPool = new SimpleThreadPool(3, Thread.NORM_PRIORITY);
threadPool.initialize();
OrientConnectionProvider connProvider = new OrientConnectionProvider();
connProvider.setConnectionString(dbUrl);
connProvider.setUser("admin");
connProvider.setPassword("admin");
connProvider.setUsePool(true);
connProvider.setPoolMin(3);
connProvider.setPoolMax(3);
connProvider.initialize();
DBConnectionManager.getInstance().addConnectionProvider("orientDS", connProvider);
DBConnectionManager.getInstance().getConnection("orientDS");
JobStoreTX jobStore = new JobStoreTX();
jobStore.setDataSource("orientDS");
jobStore.setDriverDelegateClass(OrientDelegate.class.getName());
DirectSchedulerFactory.getInstance().createScheduler("nexus", "1", threadPool, jobStore);
scheduler = DirectSchedulerFactory.getInstance().getScheduler("nexus");
scheduler.clear();
scheduler.start();
}
private Properties getQuartzProps() {
Properties result = new Properties();
result.put("org.quartz.threadPool.class", SimpleThreadPool.class.getName());
result.put("org.quartz.threadPool.threadCount", "1");
result.put("org.quartz.scheduler.instanceName", jobConfig.getJobName());
result.put("org.quartz.jobStore.misfireThreshold", "1");
result.put("org.quartz.plugin.shutdownhook.class", JobShutdownHookPlugin.class.getName());
result.put("org.quartz.plugin.shutdownhook.cleanShutdown", Boolean.TRUE.toString());
return result;
}
@Bean
public Scheduler quartzScheduler(ApplicationContext context) throws Exception {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setApplicationContext(context);
factory.setExposeSchedulerInRepository(true);
factory.setApplicationContextSchedulerContextKey(APPLICATION_CONTEXT_KEY);
factory.setJobFactory(glassJobFactory);
Properties properties = new Properties();
properties.setProperty("org.quartz.scheduler.skipUpdateCheck","true");
properties.setProperty("org.quartz.threadPool.class", SimpleThreadPool.class.getName());
properties.setProperty("org.quartz.threadPool.threadCount", "15");
properties.setProperty("org.quartz.threadPool.threadPriority", "4");
if (configuration().isInMemory()) {
properties.setProperty("org.quartz.jobStore.class", RAMJobStore.class.getName());
} else {
factory.setDataSource(dataSource());
properties.setProperty("org.quartz.jobStore.tablePrefix", configuration().getTablePrefix());
properties.setProperty("org.quartz.jobStore.isClustered", "false");
properties.setProperty("org.quartz.jobStore.driverDelegateClass", configuration().getDriverDelegateClass());
}
factory.setQuartzProperties(properties);
factory.afterPropertiesSet();
Scheduler scheduler = factory.getObject();
scheduler.getListenerManager().addJobListener(glassJobListener);
scheduler.getListenerManager().addSchedulerListener(glassSchedulerListener);
scheduler.start();
return scheduler;
}
/**
* Initialize the given SchedulerFactory, applying locally defined Quartz properties to it.
* @param schedulerFactory the SchedulerFactory to initialize
*/
private void initSchedulerFactory(StdSchedulerFactory schedulerFactory) throws SchedulerException, IOException {
Properties mergedProps = new Properties();
if (this.resourceLoader != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,
ResourceLoaderClassLoadHelper.class.getName());
}
if (this.taskExecutor != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
LocalTaskExecutorThreadPool.class.getName());
}
else {
// Set necessary default properties here, as Quartz will not apply
// its default configuration when explicitly given properties.
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
}
if (this.configLocation != null) {
if (logger.isDebugEnabled()) {
logger.debug("Loading Quartz config from [" + this.configLocation + "]");
}
PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);
}
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
if (this.dataSource != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
}
// Determine scheduler name across local settings and Quartz properties...
if (this.schedulerName != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
}
else {
String nameProp = mergedProps.getProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME);
if (nameProp != null) {
this.schedulerName = nameProp;
}
else if (this.beanName != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.beanName);
this.schedulerName = this.beanName;
}
}
schedulerFactory.initialize(mergedProps);
}
/**
* Initialize the given SchedulerFactory, applying locally defined Quartz properties to it.
* @param schedulerFactory the SchedulerFactory to initialize
*/
private void initSchedulerFactory(StdSchedulerFactory schedulerFactory) throws SchedulerException, IOException {
Properties mergedProps = new Properties();
if (this.resourceLoader != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,
ResourceLoaderClassLoadHelper.class.getName());
}
if (this.taskExecutor != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
LocalTaskExecutorThreadPool.class.getName());
}
else {
// Set necessary default properties here, as Quartz will not apply
// its default configuration when explicitly given properties.
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
}
if (this.configLocation != null) {
if (logger.isDebugEnabled()) {
logger.debug("Loading Quartz config from [" + this.configLocation + "]");
}
PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);
}
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
if (this.dataSource != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
}
// Determine scheduler name across local settings and Quartz properties...
if (this.schedulerName != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
}
else {
String nameProp = mergedProps.getProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME);
if (nameProp != null) {
this.schedulerName = nameProp;
}
else if (this.beanName != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.beanName);
this.schedulerName = this.beanName;
}
}
schedulerFactory.initialize(mergedProps);
}
/**
* Load and/or apply Quartz properties to the given SchedulerFactory.
* @param schedulerFactory the SchedulerFactory to initialize
*/
private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws SchedulerException, IOException {
if (!(schedulerFactory instanceof StdSchedulerFactory)) {
if (this.configLocation != null || this.quartzProperties != null ||
this.taskExecutor != null || this.dataSource != null) {
throw new IllegalArgumentException(
"StdSchedulerFactory required for applying Quartz properties: " + schedulerFactory);
}
// Otherwise assume that no initialization is necessary...
return;
}
Properties mergedProps = new Properties();
if (this.resourceLoader != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,
ResourceLoaderClassLoadHelper.class.getName());
}
if (this.taskExecutor != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
LocalTaskExecutorThreadPool.class.getName());
}
else {
// Set necessary default properties here, as Quartz will not apply
// its default configuration when explicitly given properties.
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
}
if (this.configLocation != null) {
if (logger.isInfoEnabled()) {
logger.info("Loading Quartz config from [" + this.configLocation + "]");
}
PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);
}
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
if (this.dataSource != null) {
mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
}
// Make sure to set the scheduler name as configured in the Spring configuration.
if (this.schedulerName != null) {
mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
}
((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);
}
/**
* Load and/or apply Quartz properties to the given SchedulerFactory.
* @param schedulerFactory the SchedulerFactory to initialize
*/
private void initSchedulerFactory(SchedulerFactory schedulerFactory) throws SchedulerException, IOException {
if (!(schedulerFactory instanceof StdSchedulerFactory)) {
if (this.configLocation != null || this.quartzProperties != null ||
this.taskExecutor != null || this.dataSource != null) {
throw new IllegalArgumentException(
"StdSchedulerFactory required for applying Quartz properties: " + schedulerFactory);
}
// Otherwise assume that no initialization is necessary...
return;
}
Properties mergedProps = new Properties();
if (this.resourceLoader != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_SCHED_CLASS_LOAD_HELPER_CLASS,
ResourceLoaderClassLoadHelper.class.getName());
}
if (this.taskExecutor != null) {
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS,
LocalTaskExecutorThreadPool.class.getName());
}
else {
// Set necessary default properties here, as Quartz will not apply
// its default configuration when explicitly given properties.
mergedProps.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, SimpleThreadPool.class.getName());
mergedProps.setProperty(PROP_THREAD_COUNT, Integer.toString(DEFAULT_THREAD_COUNT));
}
if (this.configLocation != null) {
if (logger.isInfoEnabled()) {
logger.info("Loading Quartz config from [" + this.configLocation + "]");
}
PropertiesLoaderUtils.fillProperties(mergedProps, this.configLocation);
}
CollectionUtils.mergePropertiesIntoMap(this.quartzProperties, mergedProps);
if (this.dataSource != null) {
mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
}
// Make sure to set the scheduler name as configured in the Spring configuration.
if (this.schedulerName != null) {
mergedProps.put(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, this.schedulerName);
}
((StdSchedulerFactory) schedulerFactory).initialize(mergedProps);
}
/**
* Creates an in memory job store (<code>{@link RAMJobStore}</code>)
* The thread priority is set to Thread.NORM_PRIORITY
*
* @param maxThreads
* The number of threads in the thread pool
* @throws SchedulerException
* if initialization failed.
*/
public void createVolatileScheduler(int maxThreads)
throws SchedulerException {
SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads,
Thread.NORM_PRIORITY);
threadPool.initialize();
JobStore jobStore = new RAMJobStore();
this.createScheduler(threadPool, jobStore);
}