0

我已经使用配置文件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

谁能推荐一种使用 MongoDB 使用 Quartz.properties 来支持 Quartz 的方法,或者是 Quartz 的简单替代方案?

4

1 回答 1

1

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

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>  
于 2015-10-22T13:46:25.410 回答