minor refactoring

This commit is contained in:
iliax 2023-05-05 13:01:31 +04:00
parent f4c5d3a804
commit a8f1f20dd0

View file

@ -39,13 +39,14 @@ public class MessageFilters {
} }
static Predicate<TopicMessageDTO> groovyScriptFilter(String script) { static Predicate<TopicMessageDTO> groovyScriptFilter(String script) {
var compiledScript = compileScript(script); var engine = getGroovyEngine();
var compiledScript = compileScript(engine, script);
var jsonSlurper = new JsonSlurper(); var jsonSlurper = new JsonSlurper();
return new Predicate<TopicMessageDTO>() { return new Predicate<TopicMessageDTO>() {
@SneakyThrows @SneakyThrows
@Override @Override
public boolean test(TopicMessageDTO msg) { public boolean test(TopicMessageDTO msg) {
var bindings = getGroovyEngine().createBindings(); var bindings = engine.createBindings();
bindings.put("partition", msg.getPartition()); bindings.put("partition", msg.getPartition());
bindings.put("offset", msg.getOffset()); bindings.put("offset", msg.getOffset());
bindings.put("timestampMs", msg.getTimestamp().toInstant().toEpochMilli()); bindings.put("timestampMs", msg.getTimestamp().toInstant().toEpochMilli());
@ -59,7 +60,7 @@ public class MessageFilters {
return (Boolean) result; return (Boolean) result;
} else { } else {
throw new ValidationException( throw new ValidationException(
String.format("Unexpected script result: %s, Boolean should be returned instead", result)); "Unexpected script result: %s, Boolean should be returned instead".formatted(result));
} }
} }
}; };
@ -86,9 +87,9 @@ public class MessageFilters {
return GROOVY_ENGINE; return GROOVY_ENGINE;
} }
private static CompiledScript compileScript(String script) { private static CompiledScript compileScript(GroovyScriptEngineImpl engine, String script) {
try { try {
return getGroovyEngine().compile(script); return engine.compile(script);
} catch (ScriptException e) { } catch (ScriptException e) {
throw new ValidationException("Script syntax error: " + e.getMessage()); throw new ValidationException("Script syntax error: " + e.getMessage());
} }