Skip to content

Commit

Permalink
Generic abstract service configuration example
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Jan 24, 2024
1 parent 93e003f commit 9cbde32
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
package org.flowable.common.engine.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

Expand All @@ -29,14 +33,17 @@
/**
* @author Tijs Rademakers
*/
public abstract class AbstractServiceConfiguration {
public abstract class AbstractServiceConfiguration<S> {

protected final Logger logger = LoggerFactory.getLogger(AbstractServiceConfiguration.class);

/** The tenant id indicating 'no tenant' */
public static final String NO_TENANT_ID = "";

protected String engineName;

protected Collection<ServiceConfigurator<S>> configurators;

protected boolean enableEventDispatcher = true;
protected FlowableEventDispatcher eventDispatcher;
protected List<FlowableEventListener> eventListeners;
Expand All @@ -53,7 +60,9 @@ public abstract class AbstractServiceConfiguration {
public AbstractServiceConfiguration(String engineName) {
this.engineName = engineName;
}


protected abstract S getService();

public boolean isHistoryLevelAtLeast(HistoryLevel level) {
if (logger.isDebugEnabled()) {
logger.debug("Current history level: {}, level required: {}", historyLevel, level);
Expand All @@ -69,6 +78,30 @@ public boolean isHistoryEnabled() {
return historyLevel != HistoryLevel.NONE;
}

protected void initConfigurators() {
List<ServiceConfigurator<S>> configurators;
if (this.configurators != null) {
configurators = new ArrayList<>(this.configurators);
configurators.sort(Comparator.comparingInt(ServiceConfigurator::getPriority));
} else {
this.configurators = Collections.emptyList();
}
}

protected void configuratorsBeforeInit() {
for (ServiceConfigurator<S> configurator : configurators) {
logger.info("Executing beforeInit() of {} (priority:{})", configurator.getClass(), configurator.getPriority());
configurator.beforeInit(getService());
}
}

protected void configuratorsAfterInit() {
for (ServiceConfigurator<S> configurator : configurators) {
logger.info("Executing configure() of {} (priority:{})", configurator.getClass(), configurator.getPriority());
configurator.afterInit(getService());
}
}

public String getEngineName() {
return engineName;
}
Expand All @@ -85,7 +118,7 @@ public boolean isEnableEventDispatcher() {
return enableEventDispatcher;
}

public AbstractServiceConfiguration setEnableEventDispatcher(boolean enableEventDispatcher) {
public AbstractServiceConfiguration<S> setEnableEventDispatcher(boolean enableEventDispatcher) {
this.enableEventDispatcher = enableEventDispatcher;
return this;
}
Expand All @@ -94,7 +127,7 @@ public FlowableEventDispatcher getEventDispatcher() {
return eventDispatcher;
}

public AbstractServiceConfiguration setEventDispatcher(FlowableEventDispatcher eventDispatcher) {
public AbstractServiceConfiguration<S> setEventDispatcher(FlowableEventDispatcher eventDispatcher) {
this.eventDispatcher = eventDispatcher;
return this;
}
Expand All @@ -103,7 +136,7 @@ public List<FlowableEventListener> getEventListeners() {
return eventListeners;
}

public AbstractServiceConfiguration setEventListeners(List<FlowableEventListener> eventListeners) {
public AbstractServiceConfiguration<S> setEventListeners(List<FlowableEventListener> eventListeners) {
this.eventListeners = eventListeners;
return this;
}
Expand All @@ -112,7 +145,7 @@ public Map<String, List<FlowableEventListener>> getTypedEventListeners() {
return typedEventListeners;
}

public AbstractServiceConfiguration setTypedEventListeners(Map<String, List<FlowableEventListener>> typedEventListeners) {
public AbstractServiceConfiguration<S> setTypedEventListeners(Map<String, List<FlowableEventListener>> typedEventListeners) {
this.typedEventListeners = typedEventListeners;
return this;
}
Expand All @@ -121,7 +154,7 @@ public List<EventDispatchAction> getAdditionalEventDispatchActions() {
return additionalEventDispatchActions;
}

public AbstractServiceConfiguration setAdditionalEventDispatchActions(List<EventDispatchAction> additionalEventDispatchActions) {
public AbstractServiceConfiguration<S> setAdditionalEventDispatchActions(List<EventDispatchAction> additionalEventDispatchActions) {
this.additionalEventDispatchActions = additionalEventDispatchActions;
return this;
}
Expand All @@ -130,16 +163,24 @@ public HistoryLevel getHistoryLevel() {
return historyLevel;
}

public AbstractServiceConfiguration setHistoryLevel(HistoryLevel historyLevel) {
public AbstractServiceConfiguration<S> setHistoryLevel(HistoryLevel historyLevel) {
this.historyLevel = historyLevel;
return this;
}


public Collection<ServiceConfigurator<S>> getConfigurators() {
return configurators;
}

public void setConfigurators(Collection<ServiceConfigurator<S>> configurators) {
this.configurators = configurators;
}

public ObjectMapper getObjectMapper() {
return objectMapper;
}

public AbstractServiceConfiguration setObjectMapper(ObjectMapper objectMapper) {
public AbstractServiceConfiguration<S> setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
Expand All @@ -148,7 +189,7 @@ public Clock getClock() {
return clock;
}

public AbstractServiceConfiguration setClock(Clock clock) {
public AbstractServiceConfiguration<S> setClock(Clock clock) {
this.clock = clock;
return this;
}
Expand All @@ -157,7 +198,7 @@ public IdGenerator getIdGenerator() {
return idGenerator;
}

public AbstractServiceConfiguration setIdGenerator(IdGenerator idGenerator) {
public AbstractServiceConfiguration<S> setIdGenerator(IdGenerator idGenerator) {
this.idGenerator = idGenerator;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
package org.flowable.job.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.flowable.common.engine.impl.AbstractServiceConfiguration;
import org.flowable.common.engine.impl.ServiceConfigurator;
import org.flowable.common.engine.impl.calendar.BusinessCalendarManager;
import org.flowable.common.engine.impl.el.ExpressionManager;
import org.flowable.common.engine.impl.history.HistoryLevel;
Expand Down Expand Up @@ -69,7 +66,7 @@
*
* @author Tijs Rademakers
*/
public class JobServiceConfiguration extends AbstractServiceConfiguration {
public class JobServiceConfiguration extends AbstractServiceConfiguration<JobServiceConfiguration> {

public static final String JOB_EXECUTION_SCOPE_ALL = "all";
public static final String JOB_EXECUTION_SCOPE_CMMN = "cmmn";
Expand All @@ -84,7 +81,6 @@ public class JobServiceConfiguration extends AbstractServiceConfiguration {
protected JobManager jobManager;

protected TimerJobScheduler timerJobScheduler;
protected Collection<ServiceConfigurator<JobServiceConfiguration>> configurators;

// DATA MANAGERS ///////////////////////////////////////////////////

Expand Down Expand Up @@ -136,30 +132,22 @@ public JobServiceConfiguration(String engineName) {
super(engineName);
}

@Override
protected JobServiceConfiguration getService() {
return this;
}

// init
// /////////////////////////////////////////////////////////////////////

public void init() {
List<ServiceConfigurator<JobServiceConfiguration>> configurators;
if (this.configurators != null) {
configurators = new ArrayList<>(this.configurators);
configurators.sort(Comparator.comparingInt(ServiceConfigurator::getPriority));
} else {
configurators = Collections.emptyList();
}

for (ServiceConfigurator<JobServiceConfiguration> configurator : configurators) {
configurator.beforeInit(this);
}

initConfigurators();
configuratorsBeforeInit();
initTimerJobScheduler();
initJobManager();
initDataManagers();
initEntityManagers();

for (ServiceConfigurator<JobServiceConfiguration> configurator : configurators) {
configurator.afterInit(this);
}
configuratorsAfterInit();
}

@Override
Expand Down Expand Up @@ -290,14 +278,6 @@ public void setTimerJobScheduler(TimerJobScheduler timerJobScheduler) {
this.timerJobScheduler = timerJobScheduler;
}

public Collection<ServiceConfigurator<JobServiceConfiguration>> getConfigurators() {
return configurators;
}

public void setConfigurators(Collection<ServiceConfigurator<JobServiceConfiguration>> configurators) {
this.configurators = configurators;
}

public JobDataManager getJobDataManager() {
return jobDataManager;
}
Expand Down

0 comments on commit 9cbde32

Please sign in to comment.