gpt4 book ai didi

java - 如何使用MongodbJobStore(通过文件quartz.properties)连接到项目java webapplication中的mongodb?

转载 作者:行者123 更新时间:2023-12-01 14:05:53 26 4
gpt4 key购买 nike

我使用配置文件quartz.properties连接到mongodb。这是我的文件quartz.properties

#specify the jobstore used
org.quartz.jobStore.class=com.novemberain.quartz.mongodb.MongoDBJobStore
org.quartz.jobStore.mongoUri=mongodb://localhost:27017
#The datasource for the jobstore that is to be used
org.quartz.jobStore.dbName=myds
org.quartz.jobStore.addresses=host1,host2
#quartz table prefixes in the database
org.quartz.jobStore.collectionPrefix=quartz_
org.quartz.threadPool.threadCount = 4

任何人都可以推荐一种使用quartz.properties 通过 MongoDB 支持 Quartz 的方法,或者 Quartz 的简单替代方案吗?

最佳答案

使用此方法获取具有/不具有属性的调度程序

public Scheduler getScheduler(Properties properties) {
SchedulerFactory factory;

if (!properties.isEmpty()) {
// with properties
factory = new StdSchedulerFactory(properties);
} else {
// without properties
factory = new StdSchedulerFactory();
}

return factory.getScheduler();
}

这个方法用于加载属性文件

public Scheduler load() throws SchedulerException {
Properties prop = new Properties();

try {
// file 'quartz.properties' in the 'src/main/resources/config' (Maven project structure)
properties.load(this.getClass().getResourceAsStream("/config/my-quartz.properties"));
} catch (IOException e) {
// process the exception, maybe load default properties
}

return getScheduler(properties);
}

您可以将属性文件放入“src/main/resources/config”文件夹中,

或设置$JAVA_OPTS=-Dorg.quartz.properties=/config/my-quartz.properties

您还可以使用 Spring 加载属性

@Component
public class SchedulerLoader {
@Value("${org.quartz.jobStore.class}")
private String quartzJobStoreClass;

@Value("${org.quartz.jobStore.mongoUri}")
private String quartzJobStoreMongoUri;

@Value("${org.quartz.jobStore.dbName}")
private String quartzJobStoreDbName;

@Value("${org.quartz.jobStore.collectionPrefix}")
private String quartzJobStoreCollectionPrefix;

@Value("${org.quartz.threadPool.threadCount}")
private String quartzThreadPoolThreadCount;

@Value("${org.quartz.jobStore.addresses}")
private String quartzJobStoreAddresses;

public Scheduler load() throws SchedulerException {
Properties properties = new Properties();

try {
properties.setProperty("org.quartz.jobStore.class", quartzJobStoreClass);
properties.setProperty("org.quartz.jobStore.mongoUri", quartzJobStoreMongoUri);
properties.setProperty("org.quartz.jobStore.dbName", quartzJobStoreDbName);

properties.setProperty("org.quartz.jobStore.collectionPrefix", quartzJobStoreCollectionPrefix);
properties.setProperty("org.quartz.threadPool.threadCount", quartzThreadPoolThreadCount);
properties.setProperty("org.quartz.jobStore.addresses", quartzJobStoreAddresses);
} catch (IOException e) {
// process the exception, maybe load default properties
}

return getScheduler(properties);
}
...

使用Spring配置

<beans ...>
...
<context:annotation-config/>
<context:property-placeholder location="classpath:config/*.properties"/>
...

有属性文件

# Use the MongoDB store
org.quartz.jobStore.class=com.novemberain.quartz.mongodb.MongoDBJobStore

# MongoDB URI (optional if 'org.quartz.jobStore.addresses' is set)
org.quartz.jobStore.mongoUri=mongodb://localhost:27020

# comma separated list of mongodb hosts/replica set seeds (optional if 'org.quartz.jobStore.mongoUri' is set)
org.quartz.jobStore.addresses=host1,host2

# database name
org.quartz.jobStore.dbName=quartz

# Will be used to create collections like mycol_jobs, mycol_triggers, mycol_calendars, mycol_locks
org.quartz.jobStore.collectionPrefix=mycol

# thread count setting is ignored by the MongoDB store but Quartz requries it
org.quartz.threadPool.threadCount=1

您还应该添加 Maven 依赖项(查看 https://github.com/michaelklishin/quartz-mongodb 了解详细信息)

<dependency>
<groupId>com.novemberain</groupId>
<artifactId>quartz-mongodb</artifactId>
<version>2.0.0-rc1</version>
</dependency>

关于java - 如何使用MongodbJobStore(通过文件quartz.properties)连接到项目java webapplication中的mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18908198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com