com.github.lontime.extcopper.impl.AbstractRdbmsEngineFactory Maven / Gradle / Ivy
package com.github.lontime.extcopper.impl;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.base.commonj.utils.CommonManager;
import com.github.lontime.extcopper.configuration.PersitenceOption;
import com.github.lontime.extdatasource.DatasourceInstance;
import com.github.lontime.shaded.com.google.common.base.Suppliers;
import org.copperengine.core.DependencyInjector;
import org.copperengine.core.audit.BatchingAuditTrail;
import org.copperengine.core.audit.CompressedBase64PostProcessor;
import org.copperengine.core.audit.DummyPostProcessor;
import org.copperengine.core.audit.MessagePostProcessor;
import org.copperengine.core.audit.ScottyAuditTrailQueryEngine;
import org.copperengine.core.common.DefaultProcessorPoolManager;
import org.copperengine.core.common.ProcessorPoolManager;
import org.copperengine.core.persistent.PersistentPriorityProcessorPool;
import org.copperengine.core.persistent.PersistentProcessorPool;
import org.copperengine.core.persistent.txn.CopperTransactionController;
import org.copperengine.core.persistent.txn.TransactionController;
import org.copperengine.ext.persistent.RdbmsEngineFactory;
/**
* AbstractRdbmsEngineFactory.
*
* @author lontime
* @since 1.0
*/
public abstract class AbstractRdbmsEngineFactory extends RdbmsEngineFactory {
private static final Logger logger = LoggerFactory.getLogger(AbstractRdbmsEngineFactory.class);
private PersitenceOption option;
protected final Supplier messagePostProcessor;
protected MessagePostProcessor createMessagePostProcessor() {
if (option.getAuditTrail().getCompressed()) {
return new CompressedBase64PostProcessor();
}
return new DummyPostProcessor();
}
public AbstractRdbmsEngineFactory(PersitenceOption option1) {
super(option1.getWfClasspathes());
if (option1.getNumberOfBatcherThreads() != null) {
setNumberOfBatcherThreads(option1.getNumberOfBatcherThreads());
}
if (option1.getStatLoggerIntervalSeconds() != null) {
setStatLoggerIntervalSeconds(option1.getStatLoggerIntervalSeconds());
}
if (option1.getEngineId() != null) {
setEngineId(option1.getEngineId());
}
messagePostProcessor = Suppliers.memoize(() -> {
logger.info("Creating MessagePostProcessor...");
return createMessagePostProcessor();
});
this.option = option1;
}
@Override
protected DataSource createDataSource() {
return DatasourceInstance.get().getDataSource(option.getDatasource());
}
@Override
protected ProcessorPoolManager createProcessorPoolManager() {
final List pools = option.getProcessorPools()
.stream().map(this::createPersistentPriorityProcessorPool).collect(Collectors.toList());
if (CollectionHelper.isEmpty(pools)) {
return super.createProcessorPoolManager();
}
final DefaultProcessorPoolManager processorPoolManager
= new DefaultProcessorPoolManager<>();
processorPoolManager.setProcessorPools(pools);
return processorPoolManager;
}
@Override
protected TransactionController createTransactionController() {
CopperTransactionController txnController = new CopperTransactionController();
txnController.setDataSource(dataSource.get());
if (option.getMaxConnectRetries() != null) {
txnController.setMaxConnectRetries(option.getMaxConnectRetries());
}
return txnController;
}
@Override
protected ExecutorService createExecutorService() {
if (option.getExecutorService() != null) {
return CommonManager.getInstance().getExecutorService(option.getExecutorService());
}
return super.createExecutorService();
}
public BatchingAuditTrail getAuditTrail() {
if (option.getAuditTrail() == null) {
return null;
}
final PersitenceOption.AuditTrail opt = option.getAuditTrail();
final BatchingAuditTrail auditTrail = new BatchingAuditTrail();
auditTrail.setBatcher(getBatcher());
auditTrail.setDataSource(dataSource.get());
auditTrail.setMessagePostProcessor(messagePostProcessor.get());
if (opt.getLevel() != null) {
auditTrail.setLevel(opt.getLevel());
}
if (opt.getDbTable() != null) {
auditTrail.setDbTable(opt.getDbTable());
}
if (opt.getMapping() != null) {
auditTrail.setMapping(opt.getMapping());
}
return auditTrail;
}
public ScottyAuditTrailQueryEngine getScottyAuditTrailQueryEngine() {
if (option.getAuditTrail() == null) {
return null;
}
final ScottyAuditTrailQueryEngine queryEngine = new ScottyAuditTrailQueryEngine();
queryEngine.setDbStorage(dbStorage.get());
queryEngine.setMessagePostProcessor(messagePostProcessor.get());
return queryEngine;
}
private PersistentPriorityProcessorPool createPersistentPriorityProcessorPool(PersitenceOption.ProcessorPool opt) {
final PersistentPriorityProcessorPool pool =
new PersistentPriorityProcessorPool(opt.getId(), transactionController.get());
if (opt.getLowerThreshold() != null) {
pool.setLowerThreshold(opt.getLowerThreshold());
}
if (opt.getUpperThreshold() != null) {
pool.setUpperThreshold(opt.getUpperThreshold());
}
if (opt.getUpperThresholdReachedWaitMSec() != null) {
pool.setUpperThresholdReachedWaitMSec(opt.getUpperThresholdReachedWaitMSec());
}
if (opt.getEmptyQueueWaitMSec() != null) {
pool.setEmptyQueueWaitMSec(opt.getEmptyQueueWaitMSec());
}
if (opt.getDequeueBulkSize() != null) {
pool.setDequeueBulkSize(opt.getDequeueBulkSize());
}
if (opt.getShutdownWaitIntervalMSec() != null) {
pool.setShutdownWaitIntervalMSec(opt.getShutdownWaitIntervalMSec());
}
if (opt.getThreadPriority() != null) {
pool.setThreadPriority(opt.getThreadPriority());
}
if (opt.getNumberOfThreads() != null) {
pool.setNumberOfThreads(opt.getNumberOfThreads());
}
return pool;
}
}