com.github.lontime.extjobrunr.container.Container Maven / Gradle / Ivy
package com.github.lontime.extjobrunr.container;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import com.github.lontime.base.commonj.components.Lifecycle;
import com.github.lontime.base.commonj.constants.Consts;
import com.github.lontime.base.commonj.utils.LoggerHelper;
import com.github.lontime.extdatasource.DatasourceInstance;
import com.github.lontime.extjobrunr.common.StorageProviderKind;
import com.github.lontime.extjobrunr.configuration.BackgroundJobServerOption;
import com.github.lontime.extjobrunr.configuration.DashboardOption;
import com.github.lontime.extjobrunr.configuration.DatabaseOption;
import com.github.lontime.extjobrunr.configuration.OptionResolver;
import com.github.lontime.extjobrunr.configuration.Options;
import org.jobrunr.dashboard.JobRunrDashboardWebServerConfiguration;
import org.jobrunr.server.BackgroundJobServerConfiguration;
import org.jobrunr.storage.InMemoryStorageProvider;
import org.jobrunr.storage.StorageProvider;
import org.jobrunr.storage.StorageProviderUtils;
import org.jobrunr.storage.sql.h2.H2StorageProvider;
import org.jobrunr.storage.sql.mariadb.MariaDbStorageProvider;
import org.jobrunr.storage.sql.postgres.PostgresStorageProvider;
import static org.jobrunr.dashboard.JobRunrDashboardWebServerConfiguration.usingStandardDashboardConfiguration;
import static org.jobrunr.server.BackgroundJobServerConfiguration.usingStandardBackgroundJobServerConfiguration;
/**
* Container.
* @author lontime
* @since 1.0
*/
public class Container implements Lifecycle {
private Map map = new ConcurrentHashMap<>();
private Map map2 = new ConcurrentHashMap<>();
@Override
public void initialize() {
loadIfAbsent(OptionResolver.getInstance().getDefaultJobrunrOption());
OptionResolver.getInstance().getJobrunrOptions()
.stream().filter(s -> !Consts.DEFAULT_OBJECT_NAME.equals(s.getName()))
.forEach(opt -> loadIfAbsent(opt));
for (Map.Entry entry : map.entrySet()) {
final String name = entry.getKey();
final ExtJobrunrConfiguration configuration = entry.getValue();
LoggerHelper.infov("initialize@{0}: Starting Jobrunr for {1}", getClass().getName(), name);
map2.put(name, configuration.initialize());
}
}
@Override
public void destroy() {
for (Map.Entry entry : map.entrySet()) {
final String name = entry.getKey();
LoggerHelper.infov("destroy@{0}: Shutdowning Jobrunr for {1}", getClass().getName(), name);
final ExtJobrunrConfiguration configuration = entry.getValue();
if (configuration.backgroundJobServer != null) {
configuration.backgroundJobServer.stop();
}
if (configuration.dashboardWebServer != null) {
configuration.dashboardWebServer.stop();
}
if (configuration.storageProvider != null) {
configuration.storageProvider.close();
}
}
}
private void loadIfAbsent(Options options) {
map.computeIfAbsent(options.getName(), key -> load(options));
}
private ExtJobrunrConfiguration load(Options options) {
final ExtJobrunrConfiguration configuration = new ExtJobrunrConfiguration(options);
final StorageProvider storageProvider;
if (StorageProviderKind.isSql(options.getStorageKind())) {
final DatabaseOption database = options.getDatabase();
final StorageProviderUtils.DatabaseOptions createDatabase =
database.getSkipCreate().booleanValue() ? StorageProviderUtils.DatabaseOptions.SKIP_CREATE
: StorageProviderUtils.DatabaseOptions.CREATE;
final DataSource dataSource = DatasourceInstance.get().getDataSource(database.getDatasource());
if (options.getStorageKind() == StorageProviderKind.MYSQL) {
storageProvider = new MariaDbStorageProvider(dataSource,
database.getTablePrefix(), createDatabase);
} else if (options.getStorageKind() == StorageProviderKind.POSTGRE) {
storageProvider = new PostgresStorageProvider(dataSource,
database.getTablePrefix(), createDatabase);
} else if (options.getStorageKind() == StorageProviderKind.H2) {
storageProvider = new H2StorageProvider(dataSource,
database.getTablePrefix(), createDatabase);
} else {
storageProvider = new InMemoryStorageProvider();
}
} else {
storageProvider = new InMemoryStorageProvider();
}
configuration.useStorageProvider(storageProvider);
if (options.getGeneratorKind() != null) {
configuration.useJobDetailsGenerator(options.getGeneratorKind().getGenerator());
}
if (options.getServer() != null) {
final BackgroundJobServerOption serverOption = options.getServer();
final BackgroundJobServerConfiguration serverConfiguration
= usingStandardBackgroundJobServerConfiguration();
if (serverOption.getWorkerCount() != null) {
serverConfiguration.andWorkerCount(serverOption.getWorkerCount());
}
if (serverOption.getPollIntervalInSeconds() != null) {
serverConfiguration.andPollIntervalInSeconds(serverOption.getPollIntervalInSeconds());
}
if (serverOption.getDeleteSucceededJobsAfter() != null) {
serverConfiguration.andDeleteSucceededJobsAfter(serverOption.getDeleteSucceededJobsAfter());
}
if (serverOption.getPermanentlyDeleteDeletedJobsAfter() != null) {
serverConfiguration.andPermanentlyDeleteDeletedJobsAfter(
serverOption.getPermanentlyDeleteDeletedJobsAfter());
}
configuration.useBackgroundJobServer(serverConfiguration);
}
if (options.getEnableJmxExtensions() != null && options.getEnableJmxExtensions().equals(Boolean.TRUE)) {
configuration.useJmxExtensions();
}
if (options.getEnableDashboard() != null && options.getEnableDashboard().equals(Boolean.TRUE)) {
final DashboardOption dashboardOption = options.getDashboard();
final JobRunrDashboardWebServerConfiguration webServerConfiguration = usingStandardDashboardConfiguration();
if (dashboardOption != null) {
if (dashboardOption.getPort() != null) {
webServerConfiguration.andPort(dashboardOption.getPort());
}
if (dashboardOption.getUsername() != null || dashboardOption.getPassword() != null) {
webServerConfiguration.andBasicAuthentication(dashboardOption.getUsername(), dashboardOption.getPassword());
}
}
configuration.useDashboard(webServerConfiguration);
}
return configuration;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy