All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.turbospaces.quartz.QuartzJobStoreConfigurer Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package com.turbospaces.quartz;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;

import org.quartz.SchedulerFactory;
import org.quartz.impl.jdbcjobstore.JobStoreTX;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.PostgresqlServiceInfo;
import org.springframework.cloud.service.common.RelationalServiceInfo;

import com.turbospaces.boot.AbstractBootstrapAware;
import com.turbospaces.cfg.ApplicationProperties;
import com.turbospaces.ups.H2ServiceInfo;
import com.turbospaces.ups.UPSs;

public abstract class QuartzJobStoreConfigurer extends AbstractBootstrapAware implements QuartzConfigurer {
    @Override
    public void configureQuartz(Properties props, SchedulerFactory factory) throws Exception {
        ApplicationProperties appProps = bootstrap.props();
        String cpName = bootstrap.props().CLOUD_APP_ID.get();

        props.put("org.quartz.jobStore.class", JobStoreTX.class.getName());
        props.put("org.quartz.jobStore.tablePrefix", appProps.QUARTZ_JOBSTORE_TABLE_PREFIX.get());
        props.put("org.quartz.jobStore.useProperties", appProps.QUARTZ_JOBSTORE_USE_PROPS.get().toString());
        props.put("org.quartz.jobStore.acquireTriggersWithinLock", appProps.QUARTZ_JOBSTORE_ACQUIRE_TRIGGERS_WITHIN_LOCK.get().toString());
        props.put("org.quartz.jobStore.isClustered", appProps.QUARTZ_JOBSTORE_IS_CLUSTERED.get().toString());
        //
        props.put("org.quartz.jobStore.dataSource", cpName);
        props.put(String.format("org.quartz.dataSource.%s.minimumIdle", cpName), appProps.QUARTZ_CONNECTION_POOL_MIN.get().toString());
        props.put(String.format("org.quartz.dataSource.%s.maximumPoolSize", cpName), appProps.QUARTZ_CONNECTION_POOL_MAX.get().toString());
        props.put(String.format("org.quartz.dataSource.%s.provider", cpName), "hikaricp");

        if (appProps.QUARTZ_JOBSTORE_IS_CLUSTERED.get()) {
            String appId = bootstrap.appId();
            String slot = appProps.CLOUD_APP_INSTANCE_INDEX.get();
            props.put("org.quartz.scheduler.instanceId", String.format("%s-%s", appId, slot));
        }
    }
    protected PostgresqlServiceInfo postgresqlServiceInfo() {
        PostgresqlServiceInfo pasi = UPSs.findRequiredServiceInfoByName(bootstrap, UPSs.POSTGRES_APP);
        return (PostgresqlServiceInfo) appServiceInfo(pasi);
    }
    protected H2ServiceInfo h2ServiceInfo() {
        H2ServiceInfo pasi = UPSs.findRequiredServiceInfoByName(bootstrap, UPSs.H2_APP);
        return (H2ServiceInfo) appServiceInfo(pasi);
    }
    protected ServiceInfo appServiceInfo(ServiceInfo pasi) {
        Optional opt = UPSs.findServiceInfoByName(bootstrap, UPSs.QUARTZ_APP);
        if (opt.isPresent()) {
            ServiceInfo qasi = opt.get();
            if (pasi instanceof RelationalServiceInfo) {
                logger.debug("found quartz user {}", ((RelationalServiceInfo) qasi).getUserName());
            }
            return qasi;
        }
        if (pasi instanceof RelationalServiceInfo) {
            logger.debug("not found quartz user, will use default user {}", ((RelationalServiceInfo) pasi).getUserName());
        }
        return pasi;
    }
    protected void usePostgresql(Properties props, PostgresqlServiceInfo pasi, String jdbcURL) {
        String cpName = bootstrap.props().CLOUD_APP_ID.get();

        props.put("org.quartz.jobStore.driverDelegateClass", org.quartz.impl.jdbcjobstore.PostgreSQLDelegate.class.getName());
        props.put(String.format("org.quartz.dataSource.%s.driver", cpName), "org.postgresql.Driver");
        props.put(String.format("org.quartz.dataSource.%s.URL", cpName), jdbcURL);
        props.put(String.format("org.quartz.dataSource.%s.user", cpName), pasi.getUserName());
        props.put(String.format("org.quartz.dataSource.%s.password", cpName), pasi.getPassword());
    }
    protected void useH2(Properties props, H2ServiceInfo si) throws IOException {
        useH2(props, si.getJdbcUrl());
    }
    protected void useH2(Properties props, String jdbcURL) {
        String cpName = bootstrap.props().CLOUD_APP_ID.get();

        props.put(String.format("org.quartz.dataSource.%s.driver", cpName), "org.h2.Driver");
        props.put(String.format("org.quartz.dataSource.%s.URL", cpName), jdbcURL);
    }
    protected void useH2(Properties props, String jdbcURL, String username, String password) {
        String cpName = bootstrap.props().CLOUD_APP_ID.get();

        props.put(String.format("org.quartz.dataSource.%s.driver", cpName), "org.h2.Driver");
        props.put(String.format("org.quartz.dataSource.%s.URL", cpName), jdbcURL);
        props.put(String.format("org.quartz.dataSource.%s.user", cpName), username);
        props.put(String.format("org.quartz.dataSource.%s.password", cpName), password);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy