Skip to content

Commit

Permalink
add configurable default and maximum values for query workflow instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Edvard Fonsell committed Nov 24, 2014
1 parent fbec648 commit 96f4370
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.apache.commons.lang3.mutable.MutableInt;
import org.joda.time.DateTime;
import org.springframework.core.env.Environment;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -61,6 +62,8 @@ public class WorkflowInstanceDao {
private JdbcTemplate jdbc;
private NamedParameterJdbcTemplate namedJdbc;
ExecutorDao executorInfo;
private long workflowInstanceQueryMaxResults;
private long workflowInstanceQueryMaxResultsDefault;

/**
* Use setter injection because having the dataSource in constructor may not work
Expand All @@ -78,6 +81,12 @@ public void setExecutorDao(ExecutorDao executorDao) {
this.executorInfo = executorDao;
}

@Inject
public void setEnvironment(Environment env) {
workflowInstanceQueryMaxResults = env.getRequiredProperty("nflow.workflow.instance.query.max.results", Long.class);
workflowInstanceQueryMaxResultsDefault = env.getRequiredProperty("nflow.workflow.instance.query.max.results.default", Long.class);
}

public int insertWorkflowInstance(WorkflowInstance instance) {
try {
return insertWorkflowInstanceImpl(instance);
Expand Down Expand Up @@ -234,10 +243,8 @@ public List<WorkflowInstance> queryWorkflowInstances(QueryWorkflowInstances quer
if (!isEmpty(conditions)) {
sql += " where " + collectionToDelimitedString(conditions, " and ");
}
if (query.maxResults < Long.MAX_VALUE) {
sql += " limit :limit";
params.addValue("limit", query.maxResults);
}
sql += " limit :limit";
params.addValue("limit", getMaxResults(query.maxResults));
List<WorkflowInstance> ret = namedJdbc.query(sql, params, new WorkflowInstanceRowMapper());
for (WorkflowInstance instance : ret) {
fillState(instance);
Expand All @@ -250,6 +257,16 @@ public List<WorkflowInstance> queryWorkflowInstances(QueryWorkflowInstances quer
return ret;
}

private long getMaxResults(Long maxResults) {
if (maxResults == null) {
return workflowInstanceQueryMaxResultsDefault;
}
if (maxResults.longValue() > workflowInstanceQueryMaxResults) {
return workflowInstanceQueryMaxResults;
}
return maxResults.longValue();
}

private void fillActions(WorkflowInstance instance, boolean includeStateVariables) {
Map<Integer, Map<String, String>> actionStates = includeStateVariables ? fetchActionStateVariables(instance) :
EMPTY_ACTION_STATE_MAP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public class QueryWorkflowInstances {
public final boolean includeActionStateVariables;

/**
* The maximum number of instances to be returned by the query.
* The maximum number of instances to be returned by the query. If null, uses
* default value configured for the nFlow engine. The maximum value may also
* be limited by nFlow engine configuration.
*/
public final long maxResults;
public final Long maxResults;

QueryWorkflowInstances(Builder builder) {
super();
this.ids = new ArrayList<>(builder.ids);
this.types = new ArrayList<>(builder.types);
this.states = new ArrayList<>(builder.states);
Expand All @@ -80,7 +81,7 @@ public static class Builder {
boolean includeActions;
boolean includeCurrentStateVariables;
boolean includeActionStateVariables;
long maxResults = Long.MAX_VALUE;
Long maxResults;

/**
* Create a workflow instance query builder.
Expand Down Expand Up @@ -173,7 +174,7 @@ public Builder setIncludeActionStateVariables(boolean includeActionStateVariable
* @param maxResults
* @return this.
*/
public Builder setMaxResults(long maxResults) {
public Builder setMaxResults(Long maxResults) {
this.maxResults = maxResults;
return this;
}
Expand Down
3 changes: 3 additions & 0 deletions nflow-engine/src/main/resources/nflow-engine.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ nflow.transition.delay.waiterror.ms=7200000

nflow.max.state.retries=3

nflow.workflow.instance.query.max.results=10000
nflow.workflow.instance.query.max.results.default=100

nflow.db.user=nflow
nflow.db.password=nflow

Expand Down
3 changes: 3 additions & 0 deletions nflow-engine/src/test/resources/junit.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
nflow.executor.group=junit

nflow.workflow.instance.query.max.results=10000
nflow.workflow.instance.query.max.results.default=100

nflow.db.h2.driver=org.h2.jdbcx.JdbcDataSource
nflow.db.h2.url=jdbc:h2:mem:test;TRACE_LEVEL_FILE=4
nflow.db.h2.user=sa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Collection<ListWorkflowInstanceResponse> listWorkflowInstances(
.setBusinessKey(businessKey).setExternalId(externalId)
.setIncludeCurrentStateVariables(includes.contains(currentStateVariables)).setIncludeActions(includes.contains(actions))
.setIncludeActionStateVariables(includes.contains(actionStateVariables))
.setMaxResults(maxResults == null ? Long.MAX_VALUE : maxResults.longValue()).build();
.setMaxResults(maxResults).build();
Collection<WorkflowInstance> instances = workflowInstances.listWorkflowInstances(q);
List<ListWorkflowInstanceResponse> resp = new ArrayList<>();
for (WorkflowInstance instance : instances) {
Expand Down

0 comments on commit 96f4370

Please sign in to comment.