Skip to content

Commit

Permalink
Possible to provide custom executor service
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiers committed Jan 18, 2018
1 parent f463a15 commit c9f1c12
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 c9f1c12

Please sign in to comment.