Skip to content

Commit

Permalink
Merge pull request #71 from bkiers/feature/possible-custom-executor-s…
Browse files Browse the repository at this point in the history
…ervice

Possible to provide custom executor service
  • Loading branch information
bkiers authored Jan 18, 2018
2 parents f463a15 + c9f1c12 commit 85fc9df
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/liqp/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,17 @@ public String render(boolean convertValueToMap, String key, Object value, Object
* @return a string denoting the rendered template.
*/
public String render(final Map<String, Object> variables) {
return render(variables, Executors.newSingleThreadExecutor(), true);
}

public String render(final Map<String, Object> variables, ExecutorService executorService, boolean shutdown) {

if (this.templateSize > this.protectionSettings.maxTemplateSizeBytes) {
throw new RuntimeException("template exceeds " + this.protectionSettings.maxTemplateSizeBytes + " bytes");
}

final LiquidWalker walker = new LiquidWalker(new CommonTreeNodeStream(root), this.tags, this.filters, this.parseSettings.flavor);

ExecutorService executorService = Executors.newSingleThreadExecutor();

Callable<String> task = new Callable<String>() {
public String call() throws Exception {
try {
Expand All @@ -276,8 +278,8 @@ public String call() throws Exception {
}
};

Future<String> future = executorService.submit(task);
try {
Future<String> future = executorService.submit(task);
return future.get(this.protectionSettings.maxRenderTimeMillis, TimeUnit.MILLISECONDS);
}
catch (TimeoutException e) {
Expand All @@ -288,7 +290,9 @@ public String call() throws Exception {
throw new RuntimeException("Oops, something unexpected happened: ", t);
}
finally {
executorService.shutdown();
if (shutdown) {
executorService.shutdown();
}
}
}

Expand Down

0 comments on commit 85fc9df

Please sign in to comment.