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

时间:2013-09-20 03:06:53

标签: java mongodb quartz-scheduler

我已经使用配置文件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支持Quartz和Quartz的简单替代方法吗?

1 个答案:

答案 0 :(得分:1)

将此方法用于具有/不具有属性的get scheduler

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>